Random Number Generation

We use the System.Random class to generate random numbers. The Random class provides a method for generating random numbers. Each number has an equal chance of getting picked. We   use the Next method to generate a non-negative random number as seen in the following code:

Random generator = new Random();

//Generate 10 random numbers
for (int i = 1; i <= 10; i++)
{
    Console.WriteLine(generator.Next());
}
673131583
2075827769
530790962
853400196
1181061071
1657679493
1459501829
452543008
1814178911
1933670708

Your output will surely be different because each number is random. Another   version of the Next method allows you to specify the maximum number that can be   generated. For example, what if you want to only generate numbers not greater   than 10.

Random generator = new Random();

//Generate 10 random numbers
for (int i = 1; i <= 10; i++)
{
    Console.WriteLine(generator.Next(11));
}
2
3
4
4
10
4
1
1
0
8

The Next method accepts an argument which is the exclusive maximum number.   Meaning, that the method will return a random value which is less than (but not   equal) to the given argument. The possible values are from 0 to 10.

We can also specify the range of numbers that the Next method can return. For example, if you want to generate numbers from 1 to 6 such as the faces of dice, you can modify the code like this:

Console.WriteLine(generator.Next(1, 7)); //Returns random value from 1 to 6

The first argument is the inclusive minimum number and the second argument specifies the exclusive maximum number that the method can return.

You can also use a seed value so that the program will generate the exact sequence of random numbers. You can use any numerical seed value. To specify a seed, use the other constructor of Random that accepts a seed as an argument.

Random generator = new Random(0);

//Generate 10 random numbers
for (int i = 1; i <= 10; i++)
{
    Console.WriteLine(generator.Next(11));
}
7
8
8
6
2
6
9
4
10
3

Run the program multiple times and you will obtain the same sequence of random   numbers.

Let’s now apply what we have learn to a simple program. This program shows you a   different message everytime you run it.

using System;

namespace RandomMessage
{
    class Program
    {
        static void Main()
        {
            Random generator = new Random();

            int messageNumber = generator.Next(1, 4);

            switch (messageNumber)
            {
                case 1:
                    Console.WriteLine("Hello to you my friend.");
                    break;
                case 2:
                    Console.WriteLine("Good day to you sir/mam.");
                    break;
                case 3:
                    Console.WriteLine("Have a happy day.");
                    break;
            }
        }
    }
}

The program generates a value from 1 to 3. We then use a switch statement to show a message for each of the possible values. Try running the program several   times and you will notice that the welcome message changes.