# 1. Deeper Procgen Seeds and Deterministic Randomness
Part of [[Deeper Procedural Generation]]. Next: [[2. Deeper Procgen Wave Function Collapse from Scratch]].
## 1.1 What "deterministic" has to mean
A seed is a promise: **same seed, same content**, on every machine, every build, and every version of your game that hasn't changed the generator. Three things break that promise:
1. **Shared global state.** `UnityEngine.Random` is one generator for the whole game. If any other script (a particle effect, an AI, a package) calls it between your calls, your sequence shifts. `Random.InitState` doesn't help once anything else is running.
2. **An algorithm you don't own.** `System.Random`'s algorithm is an implementation detail of the .NET runtime. Unity's Mono and IL2CPP happen to agree today, but nothing promises they always will, and a server running modern .NET may differ.
3. **Order changes.** Even with your own generator, if the map code and the loot code share one stream, adding one loot roll changes every map after it.
The fix for all three: **your own small generator, owned by the system that uses it, with a separate stream for each purpose.**
## 1.2 SeededRandom
`Assets/Procedural/Core/SeededRandom.cs`:
**`SeededRandom.cs`**
```csharp
using System;
/// <summary>
/// A small deterministic random generator (SplitMix64 seeding a xorshift64* stream). Unlike
/// UnityEngine.Random it isn't global, and unlike System.Random its algorithm is fixed here, so a
/// seed gives the same sequence on every platform, scripting backend and .NET version.
/// </summary>
public sealed class SeededRandom
{
ulong _state;
public SeededRandom(ulong seed)
{
_state = SplitMix(ref seed);
if (_state == 0) _state = 0x9E3779B97F4A7C15UL; // xorshift must never be zero
}
/// <summary>A seed for an independent stream, e.g. Derive(worldSeed, "rooms"). Same inputs, same seed.</summary>
public static ulong Derive(ulong seed, string stream)
{
// FNV-1a over the stream name, mixed with the seed. Stable across runs, unlike string.GetHashCode.
var h = 14695981039346656037UL;
foreach (var c in stream) { h ^= c; h *= 1099511628211UL; }
var s = seed ^ h;
return SplitMix(ref s);
}
public static ulong Derive(ulong seed, int index)
{
var s = seed ^ (0xD1B54A32D192ED03UL * (ulong)(index + 1));
return SplitMix(ref s);
}
public ulong NextULong()
{
_state ^= _state >> 12;
_state ^= _state << 25;
_state ^= _state >> 27;
return _state * 2685821657736338717UL;
}
/// <summary>Uniform in [0, 1).</summary>
public double NextDouble() => (NextULong() >> 11) * (1.0 / (1UL << 53));
/// <summary>Uniform integer in [min, max). Uses rejection, so there's no modulo bias.</summary>
public int Range(int min, int max)
{
if (max <= min) throw new ArgumentException("max must be greater than min");
var span = (ulong)((long)max - min);
var limit = ulong.MaxValue - ulong.MaxValue % span;
ulong r;
do r = NextULong(); while (r >= limit);
return (int)((long)min + (long)(r % span));
}
static ulong SplitMix(ref ulong x)
{
x += 0x9E3779B97F4A7C15UL;
var z = x;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL;
return z ^ (z >> 31);
}
}
```
How it works:
- **SplitMix64** scrambles the seed into the starting state. It turns similar seeds (1, 2, 3) into unrelated states, and it guarantees the state isn't zero, which would make xorshift return zeros forever. `SeedZeroStillWorks` tests that case.
- **xorshift64\*** generates the stream: three shifts and a multiply per number. It's fast, has a period of 2⁶⁴ − 1, and is good enough for games. It isn't for cryptography.
- **`NextDouble`** takes the top 53 bits, exactly what a `double` can hold, so every value in [0, 1) is equally likely.
- **`Range`** uses **rejection sampling**. `r % 6` on a 64-bit number slightly favours small results, because 2⁶⁴ isn't a multiple of 6. Rejecting the few values above the last whole multiple removes that bias. For dice it barely matters; for a loot table rolled a million times it does.
## 1.3 Derived streams
`Derive(seed, "rooms")` makes a new seed from the world seed and a name, by hashing the name with FNV-1a and mixing in the seed with SplitMix64. Give each system its own stream:
```csharp
var world = 20261201UL;
var layoutRng = new SeededRandom(SeededRandom.Derive(world, "layout"));
var lootRng = new SeededRandom(SeededRandom.Derive(world, "loot"));
var enemyRng = new SeededRandom(SeededRandom.Derive(world, "enemies"));
```
Now adding a loot roll changes only the loot. The map players have been sharing stays identical.
Two rules:
- **Never use `string.GetHashCode()` for seeds.** In modern .NET it's randomised per process, so it gives a different number every run. FNV-1a is written out in full here for that reason.
- **Use `Derive(seed, index)` for numbered things:** attempt 3, chunk 17, floor 4. `MapGenerator` (part 3) uses it for retries, so attempt 3 of seed 42 is always the same attempt.
## 1.4 Seed modes
`WfcMapBuilder` (part 4) offers three, and they apply to any generator:
| Mode | Seed | Use it for |
|---|---|---|
| Fixed | the number in the Inspector | development, bug reproduction, hand-picked levels |
| Random each play | the clock | roguelike runs; show the seed on the death screen so players can share it |
| Daily (UTC) | today's date as a number, such as 20261201 | daily challenges: the same map for everyone today |
Show the seed somewhere the player can copy it. A seed nobody can see can't be shared, and it can't be put in a bug report.
## Related
- [[Game Programming Patterns (Review)]]: global `Random` is exactly the shared mutable state its *Singleton* chapter warns about.
- [[Level Up! (Review)]]: shared seeds turn a single-player roguelike into a community challenge.