This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Generating Random Numbers

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++)

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();


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++)


Limiting the Range of Random Numbers

Often you will want to limit the size of the range of random numbers that can be generated. In its parameterless version, the values are between zero and 2,147,483,647. To limit the range you can pass an integer to the method. The random number generated will be less than the supplied value.

For example, to generate numbers between zero and ten, you should pass eleven to the parameter, as follows:

Random r = new Random();

If you would like to change the lower boundary of the range of numbers that can be generated, you must pass two integers to the Next method. The first parameter specifies the lowest inclusive value in the range. The second parameter is the exclusive upper boundary of the range. Both boundaries may be negative.

To generate a random number between -10 and 10, you could use the following:

Random r = new Random();
Console.WriteLine(r.Next(-10, 11));

Generating Floating Point Random Numbers

If you wish to generate floating-point random numbers, you can use the NextDouble method. This method returns a double-precision floating-point number between zero and one.

Random r = new Random();

Filling a Byte Array with Random Numbers

The last Random method that we will review in this article is the NextBytes method. This method populates an array of bytes with a series of random numbers. The array is declared in advance of the call and passed as a parameter to the method.

Random r = new Random();
byte[] bytes = new byte[256];
11 October 2008