public class Random
Object
Random
mscorlib
BCL
Generates psuedo-random numbers.
Instances of this class are initialized using a "seed", or starting value. The series of numbers generated by instances of the class are repeatable: given the same seed value, all instances of this class generate the same series of numbers.[Note: The numbers generated by this class are chosen with equal probability from a finite set of numbers. The numbers are generated by a definite mathematical algorithm and are therefore not truly random, but are sufficiently random for practical purposes. For this reason, the numbers are considered to be psuedo-random. ]
System Namespace
Random Constructors
Random() Constructor
Random(int) Constructor
Random Methods
Random.Next(int) Method
Random.Next(int, int) Method
Random.Next() Method
Random.NextBytes Method
Random.NextDouble Method
public Random();
Constructs a new instance of theRandom
class using System.Environment.TickCount as the seed value.
This constructor is equivalent to Random(System.Environment.TickCount ).[Note: When generating random numbers on high performance systems, the system clock value might not produce the desired behavior. For details, see the Random(Int32 ) constructor.]
System.Random Class, System Namespace
public Random(int Seed);
Constructs a new instance of theRandom
class using the specified seed value.
- Seed
- A Int32 used as the starting value for the pseudo-random number sequence.
[Note: To construct instances that produce different random number sequences, invoke this constructor using different seed values such as might be produced by the system clock. Note, however that on high performance systems, the system clock might not change between invocations of the constructor, in which case the seed value will be the same for different instances ofRandom
. When this is the case, additional operations are required to have the seed values differ in each invocation. ]
The following example demonstrates using a bitwise complement operation to obtain different random numbers using a time-dependent seed value on high performance systems.
using System; class RandomTest { public static void Main() { Random rand1 = new Random(); Random rand2 = new Random(Environment.TickCount); Console.WriteLine("The random number is {0}",rand1.Next()); Console.WriteLine("The random number is {0}",rand2.Next()); Random rdm1 = new Random(unchecked(Environment.TickCount)); Random rdm2 = new Random(~unchecked(Environment.TickCount)); Console.WriteLine("The random number is {0}",rdm1.Next()); Console.WriteLine("The random number is {0}",rdm2.Next()); } }The output is
The random number is 1990211954
The random number is 1990211954
The random number is 1990211954
The random number is 964628126
System.Random Class, System Namespace
public virtual int Next(int maxValue);
Returns a psuedo-random positive number less than the specified maximum.
- maxValue
- The upper bound of the random number to be generated. maxValue is required to be greater than or equal to zero.
A Int32 set to a psuedo-random value greater than or equal to zero and less than maxValue. If maxValue is zero, returns zero.
Exception Type Condition ArgumentOutOfRangeException maxValue is less than zero.
[Behaviors: As described above.]
[Overrides: Override this method to customize the algorithm used to generate the return value.]
[Usage: Use this method to generate a psuedo-random number less than the specified maximum value.]
System.Random Class, System Namespace
public virtual int Next(int minValue, int maxValue);
Returns a psuedo-random number within a specified range.
- minValue
- The lower bound of the random number returned.
- maxValue
- The upper bound of the random number returned.
A psuedo-random number greater than or equal to minValue and less than maxValue. If minValue and maxValue are equal, this value is returned.
Exception Type Condition ArgumentOutOfRangeException minValue is greater than maxValue.
[Behaviors: As described above.]
[Overrides: Override this method to customize the algorithm used to generate the return value.]
[Usage: Use this method to generate psuedo-random numbers in a specified range.]
System.Random Class, System Namespace
public virtual int Next();
Returns a psuedo-random number between 0 and System.Int32.MaxValue.
A Int32 greater than or equal to zero and less than System.Int32.MaxValue.
[Behaviors: As described above.]
[Overrides: Override this method to customize the algorithm used to generate the return value.]
The following example demonstrates using theNext
method. The output generated by this example will vary.
using System; class RandomTest { public static void Main() { Random rand1 = new Random(); for (int i = 0; i<10;i++) Console.WriteLine("The random number is {0}",rand1.Next()); } }The output is
The random number is 1544196111
The random number is 181749919
The random number is 1045210087
The random number is 1073826097
The random number is 1533078806
The random number is 1083151645
The random number is 569083504
The random number is 1711370568
The random number is 578178313
The random number is 409444742
System.Random Class, System Namespace
public virtual void NextBytes(byte[] buffer);
Populates the elements of a specified array of bytes with random numbers.
- buffer
- An array of bytes to be populated with random numbers.
Exception Type Condition ArgumentNullException buffer is a null
reference.
[Behaviors: 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 System.Byte.MaxValue.]
[Overrides: Override this method to customize the algorithm used to generate the return value.]
[Usage: Use the
NextByte
method to populate a Byte array with random numbers.]
System.Random Class, System Namespace
public virtual double NextDouble();
Returns a random number between 0.0 and 1.0.
A Double greater than or equal to 0.0, and less than 1.0.
[Behaviors: As described above.]
[Usage: Use this method to generate a psuedo-random number greater than or equal to zero, and less than one.]
ExtendedNumerics
System.Random Class, System Namespace