none
char help! RRS feed

  • Question

  • i want to create a program that generates a random ascii symbol between 33-35 (!"#$ etc..) but this is how far i have gotten..

           

     char[] chars = new char[1];

                chars[0] = (char)33;
                for (int i = 0; i < chars.Length; i++)
                {
                    Console.WriteLine(chars[i]);
                }   
                Console.Read();

    //i don't know how i create a random and a range , i only know how to create one char..//

    Thursday, September 26, 2013 4:22 PM

Answers

  • try out this

                Random random = new Random();
                int randomNumber = random.Next(33, 36);
                char newchar = (char)randomNumber;
                Console.WriteLine(newchar);
                Console.Read();


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    • Marked as answer by Help2000 Thursday, September 26, 2013 4:35 PM
    • Edited by Ahmed-Samy Friday, September 27, 2013 12:54 PM
    Thursday, September 26, 2013 4:30 PM

All replies

  • try out this

                Random random = new Random();
                int randomNumber = random.Next(33, 36);
                char newchar = (char)randomNumber;
                Console.WriteLine(newchar);
                Console.Read();


    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    • Marked as answer by Help2000 Thursday, September 26, 2013 4:35 PM
    • Edited by Ahmed-Samy Friday, September 27, 2013 12:54 PM
    Thursday, September 26, 2013 4:30 PM
  • The code that is marked as correct will only generate 33 and 34.  The number 35 will never be generated.  To get numbers in the range from 33 to 35, as the OP (sort of) asked for, you need

    random.Next(33, 36);

    because the upper bound is exclusive


    Paul Linton

    Thursday, September 26, 2013 10:13 PM
  • Thanks for your note PaulLinton, the main idea was to give an example of how to generate a random number.

    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, September 27, 2013 12:54 PM