System.Random Class

Assembly: Mscorlib.dll
Namespace: System
Summary
Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.
C# Syntax:
[Serializable]
public class Random
Remarks
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 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.

See also:
System Namespace

System.Random Member List:

Public Constructors
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.
Public Methods
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.
Protected Methods
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:


System.Random Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the Random class, using a time-dependent default seed value.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public Random();
Remarks
The distribution of the generated numbers is uniform; each number is equally likely to be returned.

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.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the Random class, using the specified seed value.
C# Syntax:
public Random(
   int Seed
);
Parameters:

Seed

A number used to calculate a starting value for the pseudo-random number sequence.

Remarks
If your application requires different random number sequences, invoke this constructor repeatedly with different seed values. One way to produce a unique seed value is to make it time-dependent. For example, derive the seed value from the system clock.

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

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~Random();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: Next()
Summary
Returns a positive random number.
C# Syntax:
public virtual int Next();
Return Value:
A number greater than or equal to zero and less than Int32.MaxValue.
See also:
Int32

Return to top


Overloaded Method: Next(
   int maxValue
)
Summary
Returns a positive random number less than the specified maximum.
C# Syntax:
public virtual int Next(
   int maxValue
);
Parameters:

maxValue

The upper bound of the random number to be generated.maxValue must be greater than or equal to zero.

Return Value:
A number greater than or equal to zero, and less than maxValue.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException maxValue is less than zero.
See also:
Int32

Return to top


Overloaded Method: Next(
   int minValue,
   int maxValue
)
Summary
Returns a random number within a specified range.
C# Syntax:
public virtual int Next(
   int minValue,
   int maxValue
);
Parameters:

minValue

The lower bound of the random number returned.

maxValue

The upper bound of the random number returned.maxValue must be greater than or equal to minValue.

Return Value:
A number greater than or equal to minValue and less than maxValue. If minValue equals maxValue, minValue is returned.
Exceptions
Exception Type Condition
ArgumentOutOfRangeException minValue is greater than maxValue.
See also:
Int32

Return to top


Method: NextBytes(
   byte[] buffer
)
Summary
Fills the elements of a specified array of bytes with random numbers.
C# Syntax:
public virtual void NextBytes(
   byte[] buffer
);
Parameters:

buffer

An array of bytes to contain random numbers.

Exceptions
Exception Type Condition
ArgumentNullException buffer is null.
Remarks
Each element of the array of bytes is set to a random number greater than or equal to zero, and less than or equal to Byte.MaxValue.
Example
        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]);
        }

    
See also:
Byte

Return to top


Method: NextDouble()
Summary
Returns a random number between 0.0 and 1.0.
C# Syntax:
public virtual double NextDouble();
Return Value:
A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
Remarks
This method is the public version of the protected method, Random.Sample.

Return to top


Method: Sample()
Summary
Returns a random number between 0.0 and 1.0.
C# Syntax:
protected virtual double Sample();
Return Value:
A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
Remarks
Create a derived class of Random to override this method and produce a different distribution.
See also:
Random.NextDouble

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.