Table of Contents

Class MathHelper.Random

Namespace
Microsoft.Xna.Framework
Assembly
MonoGame.Framework.dll

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).

https://www.pcg-random.org/

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 ulong

State initializer (a.k.a. seed).

streamID ulong

Sequence 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

ulong

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 int

Exclusive 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

minValue int

Lower bound.

maxValue int

Upper bound.

Returns

int

A random number ranging from minValue to maxValue.

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 float

Lower bound. MUST be greater than zero.

maxValue float

Upper bound. MUST be greater than zero.

Returns

float

A random float ranging from minValue to maxValue.