Skip to content

MockRandom

ClassMockMagiccom.wetzold.mock-magic
public static class MockRandom

Seedable random facade that all MockMagic generators route through. Wraps a System.Random so results are reproducible across runs, AOT/IL2CPP platforms and Edit/Play mode. Call MockMagic.MockRandom.Seed(System.Int32) before generating data to get deterministic output; MockMagic.MockRandom.ResetSeed restores the default wall-clock-seeded behaviour.

public static void Seed(int seed)

Seed the random generator for reproducible output. Also re-seeds UnityEngine.Random so Unity-internal randomness stays in sync.

Parameters

Name Type
seed System.Int32
public static void ResetSeed()

Reset to the default time-seeded behaviour and clear the unique index counter.

public static int Range(int minInclusive, int maxExclusive)

Integer in [minInclusive, maxExclusive), matching UnityEngine.Random.Range(System.Int32%2cSystem.Int32).

Parameters

Name Type
minInclusive System.Int32
maxExclusive System.Int32

Returns

System.Int32

public static float Range(float min, float max)

Float in [min, max], matching UnityEngine.Random.Range(System.Single%2cSystem.Single).

Parameters

Name Type
min System.Single
max System.Single

Returns

System.Single

public static float Value01()

Float in [0, 1).

Returns

System.Single

public static double NextDouble()

Double in [0, 1).

Returns

System.Double

public static bool Bool()

Random boolean.

Returns

System.Boolean

public static bool Chance(float probability)

Random boolean biased by probability in [0, 1].

Parameters

Name Type
probability System.Single

Returns

System.Boolean

public static double Gaussian(double mean, double standardDeviation)

Random double drawn from a normal distribution.

Parameters

Name Type
mean System.Double
standardDeviation System.Double

Returns

System.Double

public static float GaussianFloat(float mean, float standardDeviation)

Random float drawn from a normal distribution.

Parameters

Name Type
mean System.Single
standardDeviation System.Single

Returns

System.Single

public static int GaussianInt(double mean, double standardDeviation)

Random int drawn from a normal distribution (rounded).

Parameters

Name Type
mean System.Double
standardDeviation System.Double

Returns

System.Int32

public static T Pick<T>(params T[] items)

Pick a random element from an array.

Parameters

Name Type
items {T}[]

Returns

{T}

public static T Pick<T>(IReadOnlyList<T> items)

Pick a random element from a list.

Parameters

Name Type
items System.Collections.Generic.IReadOnlyList{{T}}

Returns

{T}

PickWeighted(params (float weight, T value)[])

Section titled “PickWeighted(params (float weight, T value)[])”
public static T PickWeighted<T>(params (float weight, T value)[] items)

Pick a random element biased by weights. Weights do not need to sum to 1.

Parameters

Name Type
items System.ValueTuple{System.Single,{T}}[]

Returns

{T}

public static void Shuffle<T>(IList<T> items)

Fisher-Yates shuffle in place.

Parameters

Name Type
items System.Collections.Generic.IList{{T}}
public static T[] Subset<T>(T[] items, int count)

Return a random subset of count unique items drawn from items.

Parameters

Name Type
items {T}[]
count System.Int32

Returns

{T}[]

public static bool IsSeeded { get; }

Whether MockMagic.MockRandom.Seed(System.Int32) has been called without a matching MockMagic.MockRandom.ResetSeed.

Returns

System.Boolean

public static int UniqueIndex { get; }

Ever-incrementing counter useful for producing collision-free values. Reset by MockMagic.MockRandom.Seed(System.Int32) and MockMagic.MockRandom.ResetSeed.

Returns

System.Int32

public static int PeekUniqueIndex { get; }

Peek at the next unique index without consuming it.

Returns

System.Int32