Export (0) Print
Expand All

Random Constructor (Int32)

Updated: March 2012

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

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

public function Random(
	Seed : int
)

Parameters

Seed
Type: System.Int32

A number used to calculate a starting value for the pseudo-random number sequence. If a negative number is specified, the absolute value of the number is used.

ExceptionCondition
OverflowException

Seed is Int32.MinValue, which causes an overflow when its absolute value is calculated.

Providing an identical seed value to different Random objects causes each instance to produce identical sequences of random numbers.

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, the system clock might not have sufficient resolution to provide different invocations of this constructor with a different seed value. This results in random number generators that generate identical sequences of pseudo-random numbers, as illustrated by the first two Random objects in the following example. To prevent this, apply an algorithm to differentiate the seed value in each invocation, or call the Thread.Sleep method to ensure that you provide each constructor with a different seed value.

No code example is currently available or this language may not be supported.

The following example creates Random objects with this class constructor and generates a sequence of random integers and doubles. The example illustrates that the same sequence is generated when the Random object is created again with the same seed parameter.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

March 2012

Replaced the example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft