Skip to content

Game.Common.PseudoRandomSeed

Assembly: Assembly-CSharp (game code; may vary by build)
Namespace: Game.Common

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary:
A small value-type helper used to produce deterministic pseudo-random sequences for various game subsystems. Stores a 16-bit seed (m_Seed) and exposes helper constants representing "reasons" or contexts. GetRandom(reason) returns a Unity.Mathematics.Random instance seeded from m_Seed xor reason (with a minimum seed of 1) and advanced twice to reduce correlation with simple seeds. Intended for use in ECS components and for consistent, reproducible randomness across game logic and serialization boundaries.


Fields

  • public static readonly ushort kEffectCondition Provides a unique reason id (24997) for effect-condition related randomness (use when selecting effect variations for conditions).

  • public static readonly ushort kSubObject Unique reason id (30691) for randomness scoped to sub-objects.

  • public static readonly ushort kSecondaryObject Unique reason id (6624) for secondary-object related randomness.

  • public static readonly ushort kSplitEdge Unique reason id (22175) used when randomness is needed for edge-splitting algorithms.

  • public static readonly ushort kEdgeNodes Unique reason id (43059) for randomness related to edge node processing.

  • public static readonly ushort kColorVariation Unique reason id (47969) commonly used to generate color variations (e.g., material tinting).

  • public static readonly ushort kBuildingState Unique reason id (61698) for building-state dependent randomness.

  • public static readonly ushort kSubLane Unique reason id (38092) for lane/sub-lane specific randomness.

  • public static readonly ushort kDummyPassengers Unique reason id (16686) used when generating dummy passenger data.

  • public static readonly ushort kLightState Unique reason id (2545) for light-state related randomness.

  • public static readonly ushort kBrightnessLimit Unique reason id (13328) used for brightness-limit variations.

  • public static readonly ushort kDrivingStyle Unique reason id (45236) to vary driving style related random choices.

  • public static readonly ushort kFlowOffset Unique reason id (8934) for flow offset variations (e.g., animation or traffic flow offsets).

  • public static readonly ushort kMeshGroup Unique reason id (60951) for mesh-group related randomness.

  • public static readonly ushort kCollapse Unique reason id (12473) for collapse-related randomness (structural failures etc).

  • public static readonly ushort kDummyName Unique reason id (29193) used when generating dummy names.

  • public static readonly ushort kTemperatureLimit Unique reason id (4505) for temperature-limit related randomness.

  • public static readonly ushort kAreaBorder Unique reason id (35490) used for area-border variation.

  • public static readonly ushort kParkedCars Unique reason id (49180) for parked-car related randomness.

  • public ushort m_Seed The stored 16-bit seed value used as the base for deriving Random instances. Typically serialized and persisted so the same seed produces the same sequences across saves/loads.

Properties

  • This type defines no properties.
    The struct exposes only fields and methods; all state is stored in m_Seed and the static reason constants.

Constructors

  • public PseudoRandomSeed(ushort seed)
    Creates a PseudoRandomSeed with the provided 16-bit seed. Use when you want a predictable seed value.

  • public PseudoRandomSeed(ref Random random)
    Creates a PseudoRandomSeed by sampling from an existing Unity.Mathematics.Random (calls NextUInt(65536) and casts to ushort). Useful for deriving a stored seed from a larger RNG (for example when establishing seeds per-entity from a global RNG).

Methods

  • public Random GetRandom(uint reason)
    Returns a Unity.Mathematics.Random instance seeded deterministically from this.m_Seed and the provided reason. Implementation details:
  • Computes seed = math.max(1u, m_Seed ^ reason) — ensures seed is at least 1 (Unity.Mathematics.Random requires non-zero seed).
  • Constructs new Random(seed).
  • Calls NextUInt() twice on the returned Random before returning it, advancing the internal state to reduce correlations for low-entropy seeds. Use different reason constants (the provided k* fields) to get independent sequences for different contexts while still deriving from the same m_Seed.

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes m_Seed to the given serializer writer. Used by the game's serialization pipeline to persist this component.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads m_Seed from the given reader. Restores the stored seed when loading.

Usage Example

// Create explicitly with a known seed:
var pseudo = new Game.Common.PseudoRandomSeed(12345);

// Or derive from an existing Random:
Unity.Mathematics.Random globalRandom = new Unity.Mathematics.Random(42u);
var pseudoFromRandom = new Game.Common.PseudoRandomSeed(ref globalRandom);

// Get a Random for a specific purpose, e.g., color variation:
Unity.Mathematics.Random colorRand = pseudo.GetRandom(Game.Common.PseudoRandomSeed.kColorVariation);
float hueOffset = colorRand.NextFloat(); // deterministic given m_Seed and reason

// Example in an ECS context (component stored on an entity):
// The component's m_Seed persists across saves/loads and can be serialized using the provided Serialize/Deserialize.
// When you need randomness in a system, call GetRandom with an appropriate reason constant to avoid sequence collisions.

Notes and tips: - The constants (k*) are intended to be used as "reason" identifiers so different parts of the code can derive independent RNG streams from the same base seed. - Because m_Seed is only 16 bits, GetRandom advances the RNG twice to decorrelate simple seeds; however, if you need high-quality entropy, consider using a wider upstream RNG and store larger seeds. - The struct implements IComponentData and can be attached to entities in DOTS/ECS-based logic, allowing per-entity reproducible randomness.