RandomNumberGenerator.GetInt32 方法

定义

重载

GetInt32(Int32)

使用加密型强随机数生成器生成介于 0(含)与指定上限(不含)之间的随机整数。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)

使用加密型强随机数生成器生成介于 0(含)与指定上限(不含)之间的随机整数。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

参数

toExclusive
Int32

随机范围的上限(不含)。The exclusive upper bound of the random range.

返回

Int32

介于 0(含)与 toExclusive(不含)之间的随机整数。A random integer between 0 (inclusive) and toExclusive (exclusive).

例外

toExclusive 参数小于或等于 0。The toExclusive parameter is less than or equal to 0.

注解

上限专用于从数组、范围或列表传入长度值。The upper bound is exclusive to enable passing in a length value from an array, span, or list. 因为它是一个独占上限,所以此方法永远不会生成 Int32.MaxValueBecause 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.

适用于

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.

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

参数

fromInclusive
Int32

随机范围的下限(含)。The inclusive lower bound of the random range.

toExclusive
Int32

随机范围的上限(不含)。The exclusive upper bound of the random range.

返回

Int32

介于 fromInclusive(含)与 toExclusive(不含)之间的随机整数。A random integer between fromInclusive (inclusive) and toExclusive (exclusive).

例外

toExclusive 参数小于或等于 fromInclusive 参数。The toExclusive parameter is less than or equal to the fromInclusive parameter.

注解

上限专用于从数组、范围或列表传入长度值。The upper bound is exclusive to enable passing in a length value from an array, span, or list. 因为它是一个独占上限,所以此方法永远不会生成 Int32.MaxValueBecause 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.

和都允许使用负值 fromInclusive toExclusiveNegative values are permitted for both fromInclusive and toExclusive.

适用于