Skip to content

Game.Common.RandomSeed

Assembly: Assembly-CSharp.dll
Namespace: Game.Common

Type: struct

Base: System.ValueType

Summary:
A small value-type wrapper that stores a 32-bit seed and provides deterministic Unity.Mathematics.Random instances derived from that seed. It also provides a static factory (Next) that uses a shared, time-seeded random generator to produce new seeds. Designed for lightweight, deterministic random streams keyed by an index.


Fields

  • private static Unity.Mathematics.Random m_Random
    This static, shared Unity.Mathematics.Random is used by RandomSeed.Next() to produce new 32-bit seeds. It is initialized using (uint)DateTime.Now.Ticks, so its initial state depends on the current time. Because it is a single static instance, concurrent access from multiple threads is not safe; consider synchronization or thread-local alternatives if you will call Next() from multiple threads.

  • private uint m_Seed
    The stored 32-bit seed value for this RandomSeed instance. This value is used by GetRandom(int index) to derive a Unity.Mathematics.Random. If the derived value would be zero, GetRandom replaces it with a fixed non-zero constant to avoid constructing a Random with a zero seed.

Properties

  • None. RandomSeed exposes no public properties; it is a simple value container with factory and accessor methods.

Constructors

  • No explicit constructors are defined. As a struct, RandomSeed has the implicit default constructor (all fields zero-initialized). Use RandomSeed.Next() to obtain a properly initialized seed.

Methods

  • public static RandomSeed Next()
    Returns a new RandomSeed with its internal m_Seed set to a pseudo-random unsigned integer produced by the shared static m_Random (m_Random.NextUInt()). This is the recommended way to obtain a new, non-deterministic seed for gameplay purposes.

  • public Unity.Mathematics.Random GetRandom(int index)
    Derives and returns a Unity.Mathematics.Random instance based on this RandomSeed's m_Seed and the supplied index. The derived uint is computed as:

  • uint num = m_Seed ^ (uint)(370248451 * index);
  • If num == 0 then num is replaced with the constant 1851936439u.
  • A new Unity.Mathematics.Random is constructed with the resulting num and returned. This provides a deterministic random stream per (seed, index) pair. Note that index can be negative (it is multiplied as an int then cast to uint), but for clarity and intent it is typically used with non-negative indices. Because multiplication and cast may wrap/overflow, the final uint is effectively a well-mixed 32-bit value based on seed and index.

Remarks and cautions: - Thread safety: the static m_Random used by Next() is not thread-safe. If multiple threads will call Next(), synchronize access or use thread-local generators. - Zero-seed handling: the code intentionally avoids using 0 as a Random constructor seed by substituting 1851936439u when the derived value is zero. - Determinism: given the same RandomSeed.m_Seed and the same index, GetRandom will always produce the same Unity.Mathematics.Random starting state, making it suitable for deterministic behaviors (e.g., reproducible procedural choices).

Usage Example

using Unity.Mathematics;
using Game.Common;

public class Example
{
    public void Demo()
    {
        // Create a new non-deterministic seed (based on time via the shared generator)
        RandomSeed seed = RandomSeed.Next();

        // Derive RNGs for different logical uses (index differentiates streams)
        Unity.Mathematics.Random rngA = seed.GetRandom(0); // stream A
        Unity.Mathematics.Random rngB = seed.GetRandom(1); // stream B

        // Use the RNG(s)
        uint r0 = rngA.NextUInt();
        float f = rngA.NextFloat();
        int i = rngB.NextInt(0, 100);

        // Because GetRandom(seed, index) is deterministic, calling GetRandom(0) again
        // with the same seed will produce the same sequence.
    }
}