Random Constructor

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the Random class, using a time-dependent default seed value.

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

Syntax

'Declaration
Public Sub New
public Random()

Remarks

The distribution of the generated numbers is uniform; each number is equally likely to be returned.

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers. You can also work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random(Int32) constructor. For more information, see the Random(Int32) constructor.

Examples

The following example uses the default constructor to instantiate three Random objects and displays a sequence of five random integers for each. Because the first two Random objects are created in close succession, they are instantiated using identical seed values based on the system clock and, therefore, they produce an identical sequence of random numbers. On the other hand, the default constructor of the thirds Random object is called after a two-second delay caused by calling the Thread.Sleep method. Because this produces a different seed value for the third Random object, it produces a different sequence of random numbers.

Dim randomInstancesToCreate As Integer = 4
Dim randomEngines(randomInstancesToCreate - 1) As Random
For ctr As Integer = 0 To randomInstancesToCreate - 1
   randomEngines(ctr) = New Random(CInt((DateTime.Now.Ticks >> 32) >> ctr))
Next
int randomInstancesToCreate = 4;
Random[] randomEngines = new Random[randomInstancesToCreate];
for (int ctr = 0; ctr < randomInstancesToCreate; ctr++)
{
   randomEngines[ctr] = new Random(unchecked((int)(DateTime.Now.Ticks >> ctr)));
}

Version Information

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

Platforms

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