Introduction
Before we dive into the details of the System.Random class, we should talk about randomness in discrete mathematics. The two most common scenarios for generating random numbers are generating integers uniformly between 1 and n and generating real numbers uniformly between 0 and 1. Most random-number-generation algorithms are not influenced by the outside universe; they are inherently pseudorandom: predictable and following a pattern (ideally not an obvious one). You should keep in mind the following two statements on pseudorandomness made by respected computer science masters.
- Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin. - John Von Neumann (1951).
- Random number generators should not be chosen at random. - Donald Knuth (1986).
Let's go back to our main focus now. We use the System.Random class to produce a random number. System.Random can return both decimal and integer random numbers. In addition, System.Random by design can seed its random number generator with a random value derived from the computer system clock.
System.Randomrnd=newSystem.Random();
You can initialize the seed with any Int32 value on demand at times when the system clock is not fast enough to refresh or when you need to synchronize your random number generators across a network. This is also the common use of a seed to synchronize two random number generators.
System.Randomrnd=newSystem.Random((int)DateTime.Now.Ticks);
The Random class includes a series of methods allowing you to retrieve the next number produced by the random number generator. The NextDouble method returns a double random number between 0.0 and 1.0. The Next method returns an integer random number between two integer values. The NextBytes method fills the elements of a specified array of bytes with random numbers. The NextDouble, Next, and NextBytes methods are instance methods; therefore, you must instantiate a System.Random object before using them.
The Next() method has three overloads
// A positive random number
public virtual int Next();
// A positive random number less than the specified maximum
public virtual int Next(int max);
// A random number within a specified range
public virtual int Next(int min, int max);
// You can use the Next method overloads as shown in Listing 21.37.
// Listing 21.37: Using the Next Property
// Any valid integer random number
Int32 dbl1 = rnd.Next();
// An integer between 0 and UpperBound where UpperBound + 1 >= 0
Int32 dbl1 = rnd.Next(UpperBound + 1);
// An integer between LowerBound and UpperBound where UpperBound + 1 >= 0
// and LowerBound <= UpperBound + 1
Int32 dbl1 = rnd.Next(LowerBound, UpperBound + 1);
Using the NextDouble method of the Random class produces the next pseudorandom double value that is greater than 0.0 and less than 1.0.

Join the conversation! Your thoughts help the community grow.