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
randomRandom
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
randomRandomelementsIReadOnlyList<T>
Returns
- T
Type Parameters
T
Exceptions
- ArgumentException
elementsis empty.
NextFloat(Random)
Returns a random float in the range [0, 1).
public static float NextFloat(this Random random)
Parameters
randomRandom
Returns
ToSequence(Random)
Creates an infinite random sequence, lazily generated using Next().
public static IEnumerable<int> ToSequence(this Random random)
Parameters
randomRandom
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
randomRandomminValueintThe inclusive lower bound of the random numbers generated.
maxValueintThe exclusive upper bound of the random numbers generated.
maxValuemust 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
minValueis greater thanmaxValue.