Tuesday, October 14, 2008

Generating Random Numbers in c#

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

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();
Console.WriteLine(r.Next(11));

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();
Console.WriteLine(r.NextDouble());

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];
r.NextBytes(bytes);

No comments: