Struct DeterministicRandom
Deterministic random number generator for multiplayer consistency Uses xorshift128+ algorithm for fast, high-quality random numbers Guarantees identical results across all platforms and clients
public struct DeterministicRandom
Constructors
DeterministicRandom(uint)
Create a new deterministic random generator with a seed
public DeterministicRandom(uint seed)
Parameters
seeduint
DeterministicRandom(uint4)
Create from an existing state
public DeterministicRandom(uint4 initialState)
Parameters
initialStateuint4
Properties
State
Current state of the random number generator Can be serialized for save/load or network synchronization
public uint4 State { get; set; }
Property Value
- uint4
Methods
Branch(uint)
Create a new random generator with different state (for sub-systems) This allows different systems to have independent random sequences while maintaining overall determinism
public DeterministicRandom Branch(uint offset = 1)
Parameters
offsetuint
Returns
FromSeedPhrase(string)
Create DeterministicRandom from seed phrase. Note: This is a simplified encoding - not full state recovery, but deterministic.
public static DeterministicRandom FromSeedPhrase(string phrase)
Parameters
phrasestring
Returns
GetStateHash()
Get a hash of the current state for verification
public uint GetStateHash()
Returns
- uint
HasSameState(DeterministicRandom)
Verify two random generators have the same state
public bool HasSameState(DeterministicRandom other)
Parameters
otherDeterministicRandom
Returns
- bool
NextBool()
Generate random boolean
public bool NextBool()
Returns
- bool
NextBool(FixedPoint32)
Generate random boolean with specified probability
public bool NextBool(FixedPoint32 probability)
Parameters
probabilityFixedPoint32
Returns
- bool
NextElementExceptIndices<T>(T[], params int[])
Select random element from array, excluding elements at specific indices.
public T NextElementExceptIndices<T>(T[] array, params int[] excludedIndices) where T : unmanaged
Parameters
arrayT[]excludedIndicesint[]
Returns
- T
Type Parameters
T
NextElementExcept<T>(T[], T)
Select random element from array, excluding a specific element.
public T NextElementExcept<T>(T[] array, T excluded) where T : unmanaged, IEquatable<T>
Parameters
arrayT[]excludedT
Returns
- T
Type Parameters
T
NextElement<T>(NativeArray<T>)
Select random element from NativeArray
public T NextElement<T>(NativeArray<T> array) where T : unmanaged
Parameters
arrayNativeArray<T>
Returns
- T
Type Parameters
T
NextElement<T>(T[])
Select random element from array
public T NextElement<T>(T[] array) where T : unmanaged
Parameters
arrayT[]
Returns
- T
Type Parameters
T
NextFixed()
Generate random float in range [0, 1) Uses fixed-point arithmetic for deterministic results
public FixedPoint32 NextFixed()
Returns
NextFixed(FixedPoint32)
Generate random fixed-point value in range [0, max)
public FixedPoint32 NextFixed(FixedPoint32 max)
Parameters
maxFixedPoint32
Returns
NextFixed(FixedPoint32, FixedPoint32)
Generate random fixed-point value in range [min, max)
public FixedPoint32 NextFixed(FixedPoint32 min, FixedPoint32 max)
Parameters
minFixedPoint32maxFixedPoint32
Returns
NextGaussian()
Generate random value from standard normal distribution (mean=0, stddev=1). Uses Box-Muller transform with fixed-point math for determinism.
public FixedPoint32 NextGaussian()
Returns
NextGaussian(FixedPoint32, FixedPoint32)
Generate random value from normal distribution with specified mean and standard deviation.
public FixedPoint32 NextGaussian(FixedPoint32 mean, FixedPoint32 stdDev)
Parameters
meanFixedPoint32stdDevFixedPoint32
Returns
NextInt(int)
Generate random integer in range [0, max)
public int NextInt(int max)
Parameters
maxint
Returns
- int
NextInt(int, int)
Generate random integer in range [min, max)
public int NextInt(int min, int max)
Parameters
minintmaxint
Returns
- int
NextPercent(int)
Generate random boolean with percentage chance (0-100)
public bool NextPercent(int percent)
Parameters
percentint
Returns
- bool
NextPoint(int2, int2)
Generate random 2D point within a rectangle
public int2 NextPoint(int2 min, int2 max)
Parameters
minint2maxint2
Returns
- int2
NextPointInCircle(FixedPoint32)
Generate random 2D point within a circle (using rejection sampling)
public FixedPoint2 NextPointInCircle(FixedPoint32 radius)
Parameters
radiusFixedPoint32
Returns
NextUInt()
Generate next random uint32 using xorshift128+ algorithm
public uint NextUInt()
Returns
- uint
NextUInt(uint)
Generate random integer in range [0, max) Uses rejection sampling to avoid bias
public uint NextUInt(uint max)
Parameters
maxuint
Returns
- uint
NextUInt(uint, uint)
Generate random integer in range [min, max)
public uint NextUInt(uint min, uint max)
Parameters
minuintmaxuint
Returns
- uint
NextWeightedElement<T>(T[], FixedPoint32[])
Select random element from array using FixedPoint weights.
public T NextWeightedElement<T>(T[] elements, FixedPoint32[] weights) where T : unmanaged
Parameters
elementsT[]weightsFixedPoint32[]
Returns
- T
Type Parameters
T
NextWeightedElement<T>(T[], int[])
Select random element from array using weights. Higher weight = more likely to be selected.
public T NextWeightedElement<T>(T[] elements, int[] weights) where T : unmanaged
Parameters
elementsT[]weightsint[]
Returns
- T
Type Parameters
T
NextWeightedIndex(int[])
Select random index using weights (useful when you need the index, not the element).
public int NextWeightedIndex(int[] weights)
Parameters
weightsint[]
Returns
- int
SetSeed(uint)
Reset to a known state (for testing or save/load)
public void SetSeed(uint seed)
Parameters
seeduint
Shuffle<T>(NativeArray<T>)
Shuffle NativeArray in-place using Fisher-Yates algorithm
public void Shuffle<T>(NativeArray<T> array) where T : unmanaged
Parameters
arrayNativeArray<T>
Type Parameters
T
Shuffle<T>(T[])
Shuffle array in-place using Fisher-Yates algorithm
public void Shuffle<T>(T[] array) where T : unmanaged
Parameters
arrayT[]
Type Parameters
T
ToSeedPhrase()
Export current state as human-readable seed phrase. Format: 8 words encoding the full 128-bit state.
public string ToSeedPhrase()
Returns
- string
ToString()
public override string ToString()
Returns
- string