Bewerken

Share via


RandomNumberGenerator.GetInt32 Method

Definition

Overloads

GetInt32(Int32)

Generates a random integer between 0 (inclusive) and a specified exclusive upper bound using a cryptographically strong random number generator.

GetInt32(Int32, Int32)

Generates a random integer between a specified inclusive lower bound and a specified exclusive upper bound using a cryptographically strong random number generator.

GetInt32(Int32)

Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs

Generates a random integer between 0 (inclusive) and a specified exclusive upper bound using a cryptographically strong random number generator.

public:
 static int GetInt32(int toExclusive);
public static int GetInt32 (int toExclusive);
static member GetInt32 : int -> int
Public Shared Function GetInt32 (toExclusive As Integer) As Integer

Parameters

toExclusive
Int32

The exclusive upper bound of the random range.

Returns

A random integer between 0 (inclusive) and toExclusive (exclusive).

Exceptions

The toExclusive parameter is less than or equal to 0.

Remarks

The upper bound is exclusive to enable passing in a length value from an array, span, or list. Because it is an exclusive upper bound this method can never generate Int32.MaxValue.

This method uses a discard-and-retry strategy to avoid the low value bias that a simple modular arithmetic operation would produce.

Applies to

GetInt32(Int32, Int32)

Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs
Source:
RandomNumberGenerator.cs

Generates a random integer between a specified inclusive lower bound and a specified exclusive upper bound using a cryptographically strong random number generator.

public:
 static int GetInt32(int fromInclusive, int toExclusive);
public static int GetInt32 (int fromInclusive, int toExclusive);
static member GetInt32 : int * int -> int
Public Shared Function GetInt32 (fromInclusive As Integer, toExclusive As Integer) As Integer

Parameters

fromInclusive
Int32

The inclusive lower bound of the random range.

toExclusive
Int32

The exclusive upper bound of the random range.

Returns

A random integer between fromInclusive (inclusive) and toExclusive (exclusive).

Exceptions

The toExclusive parameter is less than or equal to the fromInclusive parameter.

Remarks

The upper bound is exclusive to enable passing in a length value from an array, span, or list. Because it is an exclusive upper bound this method can never generate Int32.MaxValue.

This method uses a discard-and-retry strategy to avoid the low value bias that a simple modular arithmetic operation would produce.

Negative values are permitted for both fromInclusive and toExclusive.

Applies to