Runtime Spawner 1.5.0
Generic Runtime spawn and instance pooling system for spawning random AI agents around a map. Works with ANY AI system easily.
Loading...
Searching...
No Matches
MegaCrush.Spawner.Pcg32 Struct Reference

Minimal PCG32 pseudo-random number generator; fast and reproducible across platforms (including IL2CPP/AOT). More...

Public Member Functions

 Pcg32 (ulong seed, ulong seq=54UL)
 Creates a new PCG32 generator with the given seed and (optional) stream selector.
 
uint NextUInt ()
 Returns the next 32-bit unsigned integer and advances the generator state.
 
int NextInt (int minInclusive, int maxExclusive)
 Returns an integer in the half-open range [minInclusive, maxExclusive).
 
float NextFloat ()
 Returns a float in the range [0, 1).
 
bool NextBool ()
 Returns a random boolean.
 

Private Attributes

ulong _state
 Internal 64-bit LCG state.
 
ulong _inc
 Per-stream increment; must be odd. Different values select independent random streams.
 

Detailed Description

Minimal PCG32 pseudo-random number generator; fast and reproducible across platforms (including IL2CPP/AOT).

This is the 32-bit output / 64-bit state PCG variant (“pcg32”). See: https://www.pcg-random.org It’s suitable for gameplay determinism because:

  • Identical sequences for the same seed/stream across CPU/OS/IL2CPP.
  • Simple, allocation-free struct; pass by ref to advance deterministically.
  • No reliance on platform RNGs or System.Random implementation details.

State model: The generator keeps a 64-bit state and 64-bit increment (odd). Each call to NextUInt advances the state. Constructing with a seed performs a small warm-up so the first output depends on the seed/stream.

Constructor & Destructor Documentation

◆ Pcg32()

MegaCrush.Spawner.Pcg32.Pcg32 ( ulong seed,
ulong seq = 54UL )

Creates a new PCG32 generator with the given seed and (optional) stream selector.

Parameters
seed64-bit seed value (choose any).
seqStream selector (aka “sequence” or “inc source”). This is internally forced odd as (seq << 1) | 1. Using different seq values creates statistically independent sequences for the same seed.

The constructor performs a standard PCG initialization: set _state = 0, set _inc, step once, add seed to the state, then step again. This avoids low-entropy first outputs.

Member Function Documentation

◆ NextBool()

bool MegaCrush.Spawner.Pcg32.NextBool ( )

Returns a random boolean.

◆ NextFloat()

float MegaCrush.Spawner.Pcg32.NextFloat ( )

Returns a float in the range [0, 1).

Converts the top ~24 random bits to a IEEE-754 single by multiplying by 1/2^24. This mirrors the typical precision of Unity’s Random.value while remaining deterministic across platforms.

◆ NextInt()

int MegaCrush.Spawner.Pcg32.NextInt ( int minInclusive,
int maxExclusive )

Returns an integer in the half-open range [minInclusive, maxExclusive).

Parameters
minInclusiveInclusive lower bound.
maxExclusiveExclusive upper bound (must be > minInclusive ).

Uses modulo reduction of NextUInt; this is extremely fast and the slight modulo bias is negligible for typical gameplay ranges. If you require mathematically uniform bounded integers (e.g., cryptographic or fairness-critical cases), implement rejection sampling instead.

◆ NextUInt()

uint MegaCrush.Spawner.Pcg32.NextUInt ( )

Returns the next 32-bit unsigned integer and advances the generator state.

Core PCG step: LCG update followed by xorshift and rotate to decorrelate bits (this is the “XSH RR” output function in PCG literature).

Member Data Documentation

◆ _inc

ulong MegaCrush.Spawner.Pcg32._inc
private

Per-stream increment; must be odd. Different values select independent random streams.

◆ _state

ulong MegaCrush.Spawner.Pcg32._state
private

Internal 64-bit LCG state.


The documentation for this struct was generated from the following file: