System.Random.Next Method

Returns a random integer that is within a specified range.

Syntax

public virtual int Next (int minValue, int maxValue)

Parameters

minValue
The inclusive lower bound of the random number returned.
maxValue
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.

Returns

A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.

Exceptions

TypeReason
ArgumentOutOfRangeException minValue is greater than maxValue.

Remarks

Unlike the other overloads of the Random.Next method, which return only non-negative values, this method can return a negative random integer.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0