BlackWaspTM
.NET Framework
.NET 1.1+

Generating Random Numbers

by Richard Carr, published at http://www.blackwasp.co.uk/RandomNumbers.aspx

There are many uses for pseudo-random numbers in software, including generating temporary passwords for web sites or networked software or positioning enemies in a game. The .NET framework permits the creation of random numbers using the Random class.

Pseudo-Random Numbers

A truly random number generator should produce values that are completely unpredictable. For example, when simulating the throw of a die, the resultant value should be a number between one and six, with the probability of each value being exactly the same. The next result should not be affected by the previous one and should have no effect on future outcomes.

Actual random numbers can be very difficult to produce using a computer. Instead, most systems include a pseudo-random number generator. This is an algorithm that generates a sequence of numbers that appear to be random and that give a reasonably uniform distribution of numbers across the range of possible values. The algorithm generally includes a seed state, which is a value that determines the next value in the series. A calculation is performed on each generated random number to create the next seed.

The use of an algorithm with a seed state means that the random number series is deterministic. If the random number generator has a known seed state, the next number in the series can be predicted. For this reason, values generated using a pseudo-random number generator should be used with care. You should not, for example, use these predictable values for security purposes or for generating poker hands in an on-line casino game. For these scenarios, a hardware random number generator may be more suitable.

The .NET framework's random number generator creates a pseudo-random number sequence. However, in this article I will refer to "random numbers" for brevity.

The Random Class

Random numbers can be generated with the .NET framework using the Random class. This class represents a pseudo-random number generator that uses a seed state. The class is found in the System namespace.

Generating a Random Integer

The simplest manner in which to use the Random class is for generating random integers. The Next method can be used without parameters to simply return the next integer in the series. The value returned is always a positive integer or zero. Try executing the following code in a console application to generate ten random numbers:

Random r = new Random();

for (int i = 0; i < 10; i++)
{
    Console.WriteLine(r.Next());
}

If you run the program several times the random numbers should be different on each occasion. This is because the seed for the random number series is derived from the computer's clock when the Random object is constructed. Unfortunately the system clock has a limited accuracy, leading to the drawback that when several Random objects are created in quick succession they could have the same seed and therefore create the same sequence of pseudo-random numbers.

In the sample code below, three random number generators are instantiated. When you execute the code, you should see that at least two of the objects generate the same number:

Random r1 = new Random();
Random r2 = new Random();
Random r3 = new Random();

Console.WriteLine(r1.Next());
Console.WriteLine(r2.Next());
Console.WriteLine(r3.Next());

Setting a Seed Value

Sometimes the pseudo-random nature of the random number generator can be helpful. If you want to repeat the same sequence of numbers, you can set the seed state during instantiation by passing an integer value to the constructor. The following sample shows this by setting the seed value to one and producing the same sequence several times.

Random r;

for (int i = 0; i < 4; i++)
{
    r = new Random(1);

    for (int j = 0; j < 4; j++)
    {
        Console.Write(r.Next()+"\t");
    }

    Console.WriteLine();
}
11 October 2008