MoreEnumerableRandom(Int32, Int32) Method

Returns an infinite sequence of random integers between a given minimum and a maximum.

Definition

Namespace: MoreLinq
Assembly: MoreLinq (in MoreLinq.dll) Version: 3.4.0+b99a6a8cc504caf2d48372fe54a2f8116c59cd0c
C#
public static IEnumerable<int> Random(
	int minValue,
	int maxValue
)

Parameters

minValue  Int32
Inclusive lower bound of the values returned
maxValue  Int32
Exclusive upper bound of the values returned

Return Value

IEnumerableInt32
An infinite sequence of random integers

Remarks

The implementation internally uses a shared, thread-local instance of Random to generate a random number on each iteration. The actual Random instance used therefore will depend on the thread on which a single iteration is taking place; that is the call to MoveNext. If the overall iteration takes place on different threads (e.g. via asynchronous awaits completing on different threads) then various different Random instances will be involved in the generation of the sequence of random numbers. Because the Random instance is shared, if multiple sequences are generated on the same thread, the order of enumeration affects the resulting sequences.

On .NET 6 or later, System.Random.Shared is used.

See Also