[Serializable] |
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.
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the Random class, using a time-dependent default seed value. |
ctor #2 | Overloaded:.ctor(int Seed) Initializes a new instance of the Random class, using the specified seed value. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Next | Overloaded:Next() Returns a positive random number. |
Next | Overloaded:Next(int maxValue) Returns a positive random number less than the specified maximum. |
Next | Overloaded:Next(int minValue, int maxValue) Returns a random number within a specified range. |
NextBytes | Fills the elements of a specified array of bytes with random numbers. |
NextDouble | Returns a random number between 0.0 and 1.0. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Sample | Returns a random number between 0.0 and 1.0. |
Hierarchy:
public Random(); |
When generating random numbers on high-performance systems, the system clock value might not produce the expected behavior. For details, see the Remarks section of the ( Int32) constructor.
public Random( |
Seed
However, if your application runs on a fast computer the system clock might not have time to change between invocations of this constructor; the seed value might be the same for different instances of Random. In that case, apply an algorithm to differentiate the seed value in each invocation.
For instance, the following C# expressions use a bitwise complement operation to generate two different seed values even if the system time value is the same.
Random rdm1 = new
Random(unchecked((int)DateTime.Now.Ticks));
Random rdm2 = new
Random(~unchecked((int)DateTime.Now.Ticks));
~Random(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual int Next(); |
maxValue
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | maxValue is less than zero. |
minValue
maxValue
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | minValue is greater than maxValue. |
public virtual void NextBytes( |
buffer
Exception Type | Condition |
---|---|
ArgumentNullException | buffer is null. |
Random rnd = new Random(); Byte[] b = new Byte[10]; rnd.NextBytes(b); Console.WriteLine("The Random bytes are: "); for (int i = 0; i < 10; i++) { Console.Write(i); Console.Write(":"); Console.WriteLine(b[i]); }
public virtual double NextDouble(); |
protected virtual double Sample(); |
public virtual string ToString(); |