Docs / Randomness

Glossary: Rng

The members of Rng, in alphabetical order.

Color legend plain property method

A seeded PCG32 generator: a stream of numbers that is the same for a seed on every platform and .NET version.

A sealed class. The random streams page shows it in context, with code. It isn’t thread-safe: give each thread its own fork.

Names are colored by what they are: a property or a method; values and fields keep the plain color.

Properties

Seed →

ulong · Read-only. The seed the generator was created with. Its forks derive their seeds from it, and new Rng(rng.Seed) starts the same stream again from its first number.

Methods

Rng() →

Rng(ulong seed) · Creates a PCG32 generator for the seed.

Parameter Type Description
seed ulong Any value, 0 included. Different seeds give unrelated streams.

Fork() →

Fork(string key) · Returns a new generator seeded by this one’s Seed and the key. It doesn’t draw from this generator, and this generator’s draws don’t change it: the same key always gives the same stream. Throws ArgumentNullException for null.

Parameter Type Description
key string Names the stream, such as "maze". Part of your save format: renaming it changes the stream.

Fork(string key, int index) · The same, with an index for many streams from one key. Fork("maze", 0) differs from Fork("maze"). Throws ArgumentNullException for null.

Parameter Type Description
key string Names the streams, such as "maze".
index int Which stream of the key, such as a floor or a chunk coordinate. Negative values work.

Fork(ulong keyHash) · The same as Fork(string) with a key already hashed: Fork(StableHash.Of(key)) equals Fork(key). Hash the key once outside hot loops.

Parameter Type Description
keyHash ulong The key hashed by StableHash.Of, or any number of your own.

Fork(ulong keyHash, int index) · The same as Fork(string, int) with a key already hashed: Fork(StableHash.Of(key), index) equals Fork(key, index).

Parameter Type Description
keyHash ulong The key hashed by StableHash.Of, or any number of your own.
index int Which stream of the key. Negative values work.

NextBool() →

NextBool(double probability = 0.5) · true with the probability: never for 0 or less, always for 1 or more. Takes two numbers of the stream whatever the probability. Throws ArgumentOutOfRangeException for NaN.

Parameter Type Description
probability double The chance of true, from 0 to 1. Default: 0.5.

NextDouble() →

NextDouble() · A double from 0 to just under 1, with 53 random bits. Takes two numbers of the stream.

NextInt() →

NextInt(int maxExclusive) · An integer from 0 to maxExclusive - 1, each equally likely (Lemire’s multiply and reject, without the bias of %). Throws ArgumentOutOfRangeException when maxExclusive is 0 or less.

Parameter Type Description
maxExclusive int One more than the largest value.

NextInt(int minInclusive, int maxExclusive) · An integer from minInclusive to maxExclusive - 1, each equally likely. Any range of int works. Throws ArgumentOutOfRangeException when maxExclusive isn’t greater than minInclusive.

Parameter Type Description
minInclusive int The smallest value.
maxExclusive int One more than the largest value.

NextUInt() →

NextUInt() · The next 32-bit number of the stream, any uint equally likely.

Pick() →

Pick<T>(ReadOnlySpan<T> items) · An element of the span, each equally likely. Throws ArgumentException when it is empty.

Parameter Type Description
items ReadOnlySpan<T> The elements to pick from.

Pick<T>(T[] items) · The same, for an array; picks the same index as the span overload. Throws ArgumentNullException for null and ArgumentException when it is empty.

Parameter Type Description
items T[] The elements to pick from.

Pick<T>(IReadOnlyList<T> items) · The same, for a list such as a List<T>; picks the same index as the span overload. Throws ArgumentNullException for null and ArgumentException when it is empty.

Parameter Type Description
items IReadOnlyList<T> The elements to pick from.

Shuffle() →

Shuffle<T>(Span<T> items) · Puts the elements in a random order, in place, with a Fisher–Yates shuffle: every order is equally likely.

Parameter Type Description
items Span<T> The elements to shuffle.

Shuffle<T>(T[] items) · The same, for an array, in the same order as the span overload. Throws ArgumentNullException for null.

Parameter Type Description
items T[] The elements to shuffle.

Shuffle<T>(IList<T> items) · The same, for a list, in the same order as the span overload. Throws ArgumentNullException for null.

Parameter Type Description
items IList<T> The elements to shuffle.