A deterministic simulation produces bit-identical results on every run from the same initial state and the same input sequence. That property makes replays, reproducible bug reports and lockstep multiplayer, which sends only commands over the network, possible. This article covers sources of nondeterminism, the xoshiro256** generator, stable hash functions, a state hash and the lockstep flow, with C# examples.
Where nondeterminism comes from
The best-known sources are the wall clock (DateTime.Now), unseeded random numbers and Guid.NewGuid. Others are less visible: in .NET, string.GetHashCode is computed with a different seed in each process, the enumeration order of Dictionary and HashSet is not a documented contract, and the completion order of parallel loops can change on every run.
The algorithm behind System.Random is not guaranteed to stay the same across .NET versions; the same seed may produce different numbers after an upgrade. Floating-point arithmetic is consistent for the same binary on the same machine, but functions such as Math.Sin rely on the platform's math library and may differ in the last bit on another operating system or processor. Critical state can be kept in integers or fixed point.
Microsoft Learn — System.Random remarks
xoshiro256** and SplitMix64
xoshiro256** produces fast, statistically strong 64-bit numbers from a 256-bit state. The state is four ulong values and must not be all zero. The authors recommend filling it from the seed with SplitMix64, so a single 64-bit seed expands into four well-mixed values. The algorithm is a few lines long and lives in the codebase, so runtime upgrades do not affect it.
A double between 0 and 1 uses the top 53 bits. Taking a plain modulo to produce a bounded number adds a slight bias toward small values; rejection sampling removes that bias. The generator is not cryptographic: its state can be inferred from a few outputs. Use RandomNumberGenerator for keys or tokens.
using System.Numerics;
public sealed class Xoshiro256StarStar
{
private ulong _s0, _s1, _s2, _s3;
public Xoshiro256StarStar(ulong seed)
{
_s0 = SplitMix64(ref seed); _s1 = SplitMix64(ref seed);
_s2 = SplitMix64(ref seed); _s3 = SplitMix64(ref seed);
}
public static ulong SplitMix64(ref ulong x)
{
ulong z = x += 0x9E3779B97F4A7C15UL;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL;
return z ^ (z >> 31);
}
public ulong NextUInt64()
{
ulong result = BitOperations.RotateLeft(_s1 * 5, 7) * 9;
ulong t = _s1 << 17;
_s2 ^= _s0; _s3 ^= _s1; _s1 ^= _s2; _s0 ^= _s3;
_s2 ^= t;
_s3 = BitOperations.RotateLeft(_s3, 45);
return result;
}
public double NextDouble() => (NextUInt64() >> 11) * (1.0 / (1UL << 53));
public ulong NextBelow(ulong bound) // unbiased: rejects the short tail
{
ulong threshold = unchecked(0UL - bound) % bound;
ulong r;
do r = NextUInt64(); while (r < threshold);
return r % bound;
}
public (ulong, ulong, ulong, ulong) State => (_s0, _s1, _s2, _s3); // persist in saves
}xoshiro / xoroshiro generators
A separate stream per system
If one generator is shared by every system, adding a single random call shifts all later results: one extra decoration in map generation also changes combat outcomes. When each system, such as world generation, AI, combat and loot, uses its own stream derived from the match seed and the system name, changes stay local.
On large maps, a seed per region or coordinate lets one region be regenerated independently of the others. The save file stores the four state values of each stream, so after loading the game continues with the same numbers. A replay record likewise carries only the seed and the list of inputs with their step numbers.
Stable hashing: FNV-1a and mixing
string.GetHashCode is only suitable for dictionaries within one process. Deriving seeds or persistent identifiers needs a hash with a fixed algorithm. FNV-1a starts from a fixed 64-bit offset basis; for each byte it XORs and then multiplies by a fixed prime. It is short to implement and returns the same result on every platform.
When several fields are combined without a separator, ("ab", "c") and ("a", "bc") hash the same. The byte 0xFF never appears in valid UTF-8, so it makes a good separator. FNV-1a is fast, but small input differences do not spread evenly across all bits; applying SplitMix64's mixing step to the result makes it better suited for use as a seed.
using System.Text;
public static class Fnv1aHash
{
private const ulong OffsetBasis = 14695981039346656037UL;
private const ulong Prime = 1099511628211UL;
public static ulong Combine(params string[] parts)
{
ulong h = OffsetBasis;
foreach (string part in parts)
{
foreach (byte b in Encoding.UTF8.GetBytes(part)) { h ^= b; h *= Prime; }
h ^= 0xFF; h *= Prime; // separator: never occurs in UTF-8
}
return Mix(h);
}
private static ulong Mix(ulong z) // SplitMix64 finalizer
{
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL;
return z ^ (z >> 31);
}
}
// var combat = new Xoshiro256StarStar(Fnv1aHash.Combine(matchId, "combat"));
// var loot = new Xoshiro256StarStar(Fnv1aHash.Combine(matchId, "loot"));IETF draft — The FNV non-cryptographic hash
Microsoft Learn — String.GetHashCode
A canonical state hash
To confirm that two machines are in the same state, convert the state into a canonical byte sequence and hash it. Iterate collections by sorted keys (StringComparer.Ordinal) and write a presence marker before optional fields, so a missing value cannot be confused with a default. JSON output depends on dictionary order and serializer settings, so it is not reliable for this.
In IEEE 754, -0.0 equals +0.0 but differs at the bit level. If one computation produces -0, two machines in the same state can report different hashes. The writer normalizes zero; NaN should never appear in the state, and catching one is treated as an error. The bytes are hashed with SHA-256; the goal is not security, but collisions are practically impossible.
using System.Security.Cryptography;
using System.Text;
public sealed class HashWriter(Stream output) : BinaryWriter(output, Encoding.UTF8, leaveOpen: true)
{
public override void Write(double value)
{
if (double.IsNaN(value)) throw new InvalidOperationException("NaN in simulation state");
base.Write(value == 0 ? 0.0 : value); // -0.0 == 0.0, so both become +0.0
}
}
public sealed record Unit(string Id, double X, double Y, string? Target);
public static string HashState(long tick, IReadOnlyDictionary<string, Unit> units)
{
using var buffer = new MemoryStream();
using (var w = new HashWriter(buffer))
{
w.Write(tick);
w.Write(units.Count);
foreach (var unit in units.Values.OrderBy(u => u.Id, StringComparer.Ordinal))
{
w.Write(unit.Id); w.Write(unit.X); w.Write(unit.Y);
w.Write(unit.Target is not null); // presence marker
if (unit.Target is not null) w.Write(unit.Target);
}
}
return Convert.ToHexString(SHA256.HashData(buffer.ToArray()));
}Lockstep: sending only commands
In the lockstep model, clients share each step's commands rather than unit positions. A step is processed only after every player's packet for it has arrived; a player with no command sends an empty packet. Commands are scheduled for a step that lies the input delay ahead, which hides network latency inside that window. Every machine applies the packets in player id order.
Every few steps, each client sends its state hash. A different hash means a desync: the game stops and records diagnostics. The side that distributes commands also validates ownership, so a player cannot give orders to someone else's unit. The number of steps a client may run ahead is capped, and the log of rejected commands is kept in a bounded queue.
Gaffer On Games — Deterministic Lockstep