Random Class

Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

No code example is currently available or this language may not be supported.

The Random type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Random()Initializes a new instance of the Random class, using a time-dependent default seed value.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Random(Int32)Initializes a new instance of the Random class, using the specified seed value.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Next()Returns a nonnegative random number.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Next(Int32)Returns a nonnegative random number less than the specified maximum.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Next(Int32, Int32)Returns a random number within a specified range.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360NextBytesFills the elements of a specified array of bytes with random numbers.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360NextDoubleReturns a random number between 0.0 and 1.0.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360SampleReturns a random number between 0.0 and 1.0.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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 Random class is based on 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 Random. By default, the parameterless constructor of the Random 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 Random objects in close succession creates random number generators that produce identical sequences of random numbers. The following example illustrates that two Random objects that are instantiated in close sucession generate an identical series of random numbers.

No code example is currently available or this language may not be supported.

This problem can be avoided by creating a single Random object rather than multiple ones.

To improve performance, create one Random object to generate many random numbers over time, instead of repeatedly creating new Random 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 Random class is not guaranteed to remain the same across major versions of the .NET Framework for Silverlight. 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 for Silverlight.

Notes to Inheritors

The Random::Next(), Random::Next(Int32, Int32), and NextBytes methods do not necessarily call the derived class implementation of the Sample method. As a result, classes derived from Random should override all three of these methods.

The following example creates a single random number generator and calls its NextBytes, Next, and NextDouble methods to generate sequences of random numbers within different ranges.

No code example is currently available or this language may not be supported.

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show: