System.Random Class

public class Random

Base Types

Object
  Random

Assembly

mscorlib

Library

BCL

Summary

Generates psuedo-random numbers.

Description

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. ]

See Also

System Namespace

Members

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


Random() Constructor

public Random();

Summary

Constructs a new instance of the Random class using System.Environment.TickCount as the seed value.

Description

This constructor is equivalent to Random(System.Environment.TickCount ).

[Note: When generating random numbers on high performance systems, the system clock value may not produce the desired behavior. For details, see the Random(Int32 ) constructor.]

See Also

System.Random Class, System Namespace

Random(int) Constructor

public Random(int Seed);

Summary

Constructs a new instance of the Random class using the specified seed value.

Parameters

Seed
A Int32 used as the starting value for the pseudo-random number sequence.

Description

[Note: To construct instances that produce different random number sequences, invoke this constructor using different seed values such as may be produced by the system clock. Note, however that on high performance systems, the system clock may not change between invocations of the constructor, in which case the seed value will be the same for different instances of Random . When this is the case, additional operations are required to have the seed values differ in each invocation. ]

Example

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

See Also

System.Random Class, System Namespace

Random.Next(int) Method

public virtual int Next(int maxValue);

Summary

Returns a psuedo-random positive number less than the specified maximum.

Parameters

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

Return Value

A Int32 set to a psuedo-random value greater than or equal to zero and less than maxValue. If maxValue is zero, returns zero.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionmaxValue is less than zero.

Description

[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.]

See Also

System.Random Class, System Namespace

Random.Next(int, int) Method

public virtual int Next(int minValue, int maxValue);

Summary

Returns a psuedo-random number within a specified range.

Parameters

minValue
The lower bound of the random number returned.
maxValue
The upper bound of the random number returned.

Return Value

A psuedo-random number greater than or equal to minValue and less than maxValue. If minValue and maxValue are equal, this value is returned.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionminValue is greater than maxValue.

Description

[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.]

See Also

System.Random Class, System Namespace

Random.Next() Method

public virtual int Next();

Summary

Returns a psuedo-random number between 0 and System.Int32.MaxValue.

Return Value

A Int32 greater than or equal to zero and less than System.Int32.MaxValue.

Description

[Behaviors: As described above.]

[Overrides: Override this method to customize the algorithm used to generate the return value.]

Example

The following example demonstrates using the Next 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

See Also

System.Random Class, System Namespace

Random.NextBytes Method

public virtual void NextBytes(byte[] buffer);

Summary

Populates the elements of a specified array of bytes with random numbers.

Parameters

buffer
An array of bytes to be populated with random numbers.

Exceptions

Exception TypeCondition
ArgumentNullExceptionbuffer is a null reference.

Description

[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.]

See Also

System.Random Class, System Namespace

Random.NextDouble Method

public virtual double NextDouble();

Summary

Returns a random number between 0.0 and 1.0.

Return Value

A Double greater than or equal to 0.0, and less than 1.0.

Description

[Behaviors: As described above.]

[Usage: Use this method to generate a psuedo-random number greater than or equal to zero, and less than one.]

Library

ExtendedNumerics

See Also

System.Random Class, System Namespace