Core · function

hashToUnit

A single well-mixed value in [0, 1) from one integer seed.

Explained in The loop.

function hashToUnit(seed: number): number
import { hashToUnit } from '@driftengine/core';

Parameters

ParameterTypeDescription
seednumber

In depth

mulberry32's mixer without the stream: the same avalanche, applied once, for the very common case of "one deterministic value per index" — a per-particle jitter, a per-node displacement — where allocating a generator per lookup is absurd.

It exists because the obvious alternative is wrong in a way that is hard to see. fract(sin(x) * 43758.5453) is the reflex GPU hash and it degrades badly on the CPU for large x: sin of a number in the millions has lost most of its precision to argument reduction, so nearby seeds correlate and whole families of values collapse onto each other. That is what an effect seeded from a tick count hands it. Measured as lightning whose every bolt came out the same shape however much variation the caller thought it was asking for: still visibly deterministic where the caller expected a fresh shape on every strike.

Integer mixing has no such failure: every bit of the input reaches every bit of the output, at any magnitude, and it is bit-identical on every platform, which the replay contract requires.