Class RandomExtensions
- Namespace
- PAC.Extensions.System
Extension methods for Random.
public static class RandomExtensions
- Inheritance
-
RandomExtensions
- Inherited Members
Methods
NextBool(Random)
Generates a uniformly random bool value.
public static bool NextBool(this Random random)
Parameters
random
Random
Returns
NextElement<T>(Random, IReadOnlyList<T>)
Selects a uniformly random element from elements
.
public static T NextElement<T>(this Random random, IReadOnlyList<T> elements)
Parameters
random
Randomelements
IReadOnlyList<T>
Returns
- T
Type Parameters
T
Exceptions
- ArgumentException
elements
is empty.
NextFloat(Random)
Returns a random float in the range [0, 1)
.
public static float NextFloat(this Random random)
Parameters
random
Random
Returns
ToSequence(Random)
Creates an infinite random sequence, lazily generated using Next().
public static IEnumerable<int> ToSequence(this Random random)
Parameters
random
Random
Returns
Remarks
This does not deep-copy the Random, so calling Next() on this Random between the generation of two elements will
affect the sequence.
This also means that iterating over the sequence more than once will most likely give a different sequence each time; you may want to 'save' a portion of the sequence using
new Random().ToSequence().Take(10).ToArray()
or similar.
ToSequence(Random, int, int)
Creates an infinite random sequence, lazily generated using Next(int, int).
public static IEnumerable<int> ToSequence(this Random random, int minValue, int maxValue)
Parameters
random
RandomminValue
intThe inclusive lower bound of the random numbers generated.
maxValue
intThe exclusive upper bound of the random numbers generated.
maxValue
must be greater than or equal tominValue
.
Returns
Remarks
This does not deep-copy the Random, so calling Next(int, int) on this Random between the generation of two elements
will affect the sequence.
This also means that iterating over the sequence more than once will most likely give a different sequence each time; you may want to 'save' a portion of the sequence using
new Random().ToSequence(minValue, maxValue).Take(10).ToArray()
or similar.
Exceptions
- ArgumentOutOfRangeException
minValue
is greater thanmaxValue
.