Class MathHelper.Random
Random Number Generator based on Permutation-Congruential-Generator (PCG), which is a fancy wording to describe a family of RNG which are simple, fast, statistically excellent, and hardly predictable.
More interestingly, PCG allows to generate multiple sequences with the same seed, which is very handy in game development to have a unique seed per game session while using different streams for each RNG which requires an isolated context (e.g. generating a procedural level, but we don't want the loot generation to affect subsequent level generations).
This code is derived from the minimal C implementation. https://github.com/imneme/pcg-c-basic
public class MathHelper.Random
- Inheritance
-
MathHelper.Random
- Inherited Members
Constructors
Random()
Initialize a random number generator.
public Random()
Random(ulong, ulong)
Initialize a random number generator. Specified in two parts, state initializer (a.k.a. seed) and a sequence selection constant (a.k.a. stream id).
public Random(ulong state, ulong streamID = 0)
Parameters
state
ulongState initializer (a.k.a. seed).
streamID
ulongSequence selection constant (a.k.a. stream id). Defaults to 0.
Properties
State
State of the random number generator.
public ulong State { get; set; }
Property Value
Methods
Next()
Generate a random positive number.
public int Next()
Returns
- int
A random positive number.
Next(int)
Generate a random number with an exclusive maxValue
upper bound.
public int Next(int maxValue)
Parameters
maxValue
intExclusive upper bound.
Returns
- int
A random number with an exclusive
maxValue
upper bound.
Next(int, int)
Generate a random number ranging from minValue
to maxValue
.
public int Next(int minValue, int maxValue)
Parameters
Returns
- int
A random number ranging from
minValue
tomaxValue
.
NextBool()
Generate a random bool.
public bool NextBool()
Returns
- bool
A random bool.
NextFloat()
Generate a random float ranging from 0.0f to 1.0f.
public float NextFloat()
Returns
- float
A random float ranging from 0.0f to 1.0f.
NextFloat(float, float)
Generate a random float ranging from minValue
to maxValue
.
public float NextFloat(float minValue, float maxValue)
Parameters
minValue
floatLower bound. MUST be greater than zero.
maxValue
floatUpper bound. MUST be greater than zero.
Returns
- float
A random float ranging from
minValue
tomaxValue
.