Updated: March 2011
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.
Assembly: mscorlib (in mscorlib.dll)
Pseudo-random numbers are chosen with equal probability from a finite set of numbers. The chosen numbers are not completely random because a definite mathematical algorithm is used to select them, but they are sufficiently random for practical purposes. The current implementation of the class is based on a modified version of Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.
The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of . By default, the parameterless constructor of the class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different objects in close succession creates random number generators that produce identical sequences of random numbers. The following example illustrates that two objects that are instantiated in close succession generate an identical series of random numbers.
This problem can be avoided by creating a single Random object rather than multiple ones.
To improve performance, create one object to generate many random numbers over time, instead of repeatedly creating a new objects to generate one random number.
To generate a cryptographically secure random number suitable for creating a random password, for example, use a class derived from System.Security.Cryptography::RandomNumberGenerator such as System.Security.Cryptography::RNGCryptoServiceProvider.Notes to Callers:
The implementation of the random number generator in the class is not guaranteed to remain the same across major versions of the .NET Framework. As a result, your application code should not assume that the same seed will result in the same pseudo-random sequence in different versions of the .NET Framework.Notes to Inheritors:
In the .NET Framework versions 1.0 and 1.1, a minimum implementation of a class derived from required overriding the Sample method to define a new or modified algorithm for generating random numbers. The derived class could then rely on the base class implementation of the Random::Next(), Random::Next(Int32), Random::Next(Int32, Int32), NextBytes, and NextDouble methods to call the derived class implementation of the Sample method.
In the .NET Framework version 2.0 and later, the behavior of the Random::Next(), Random::Next(Int32, Int32), and NextBytes methods have changed so that these methods do not necessarily call the derived class implementation of the Sample method. As a result, classes derived from that target the .NET Framework 2.0 and later should also override these three methods.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.