Random numbers and noise
The xorshift generator, turning its bits into a sample, white and pink noise, generators that do not agree, and a slow wander from an oscillator.
7 minutes read
1407 words
Contents11
A computer has no dice. What it has is a rule that scrambles a number so thoroughly that the sequence it produces passes for chance. It also has a way of turning that sequence into a signal with a chosen colour.
Assumed knowledge
Arithmetic, and powers of two. A rule applied to its own output over and over, which is the subject of iteration and orbits . The one-pole low-pass of one-pole filters, shelves and all-passes is used in the section on colouring. The last section uses the running angle of angles and polar form .
Bits are used in one section only. A whole number held in a computer is a row of 32 binary digits, and the shift and exclusive-or below work on that row.
Ground covered
What a pseudo-random generator is, and what its seed and its period are. The xorshift rule, in three lines. How the bits of the state become one audio sample. White noise and its size. What a filter does to noise, and the three-section recipe that gives pink. Why several generators are run rather than one. The slow oscillator that is often wanted beside them.
Rules that look like chance
A pseudo-random generator is a rule applied to a number over and over, in the manner of iteration and orbits . The current number is the state, the rule produces the next, and the sequence of states is the output. Nothing about it is random. The same starting number, called the seed, always gives the same sequence, and that repeatability is what makes a test reproducible. What makes the sequence pass for chance is that no simple pattern relates one output to the next. It also runs for a very long time before it comes back to the seed. That length is the generator’s period.
Xorshift
The state is a 32-bit whole number , never zero. Three operations act on it. Writing shifts the bits of left by places, which multiplies by and discards what overflows the 32 bits. Writing shifts them right by , which divides by and discards the remainder. The symbol is exclusive-or, where each bit of the answer is 1 where the two inputs differ and 0 where they agree. One step of the generator is three of these in a row.
Each line mixes bits from one part of the number into another. Applied together, and with these particular shift counts, the rule visits every non-zero 32-bit value exactly once before returning to the seed. That is a period of . The value 0 maps to 0, which is the one seed that has to be avoided. Three shifts and three exclusive-ors is about as cheap as a generator gets. That is why this one is used where a fresh number is needed on every sample.1
From bits to a sample
An audio sample is a number between and . Taking the top 24 bits of the state and scaling gives one, spread evenly over that range.
The quantity is a whole number from 0 to . Dividing by makes it 0 to just under 2, and subtracting 1 centres it on zero.
White noise
Each sample of equation (2) is unrelated to the last. A sequence of them is called white noise, by analogy with light, because every frequency is present in equal measure on average. That holds from the lowest frequency up to half the sample rate. Its average is 0, and its average square, which is the power, is for values spread evenly over . Its size in the root-mean-square sense is therefore . A run of samples from equation (1) measures 0.578. It sounds like a hiss with no pitch and no colour.
Colouring noise
Pass white noise through a filter and the result carries the filter’s response. At each frequency the power is multiplied by the square of the filter’s magnitude there. A low-pass therefore makes a duller noise, and a high-pass a thinner one.
The colour most often wanted is pink. Pink means power that falls by three decibels for every doubling of frequency, so that every octave holds the same energy. That is how a room’s background sounds and how most natural sounds are balanced.2
No single one-pole filter has that slope, which is six decibels per doubling above its corner and zero below. Three of them with corners a decade or so apart, added together, come close across the audio band. One widely used recipe is the following, with the white sample.
Each line of equation (3) is the one-pole low-pass of the filters page written as . The pole is , and the input gain is chosen so that the three sections add up to the right slope. A pole at is a corner at
so the three corners are , and hertz at a sample rate of . The poles are fixed numbers, so the corners move with the sample rate. The recipe was tuned for , where equation (5) puts them at , and hertz.
Generators that disagree
Two generators started from different seeds produce sequences with no relation to each other. Added together, unrelated noises add their powers rather than their sizes. Six of them at equal level sum to times one rather than six times. A reverb that wants a whisper of background in each of six delay lines runs six generators from six seeds. The lines then do not carry the same whisper, and the background does not collapse to a point between the speakers.
A slow wander
The opposite of noise is a movement so slow and so regular that it is heard as breathing rather than as sound. A low-frequency oscillator keeps an angle and advances it by a fixed step on every sample. Its sine moves a level up and down.
At hertz the cycle lasts seconds. At the level swings between one tenth of and nearly twice it. The angle is the one that goes round the unit circle , once every few seconds instead of thousands of times a second.
Figure sources
One script draws the figure. It runs equation (1) as a 32-bit register and turns the state into samples with equation (2). It filters them with equations (3) and (4), then averages the periodograms of 240 windows of 4096 samples. The slope quoted in the caption is fitted to the result rather than asserted, so a wrong coefficient in equation (3) shows up as a wrong slope. It needs NumPy and Matplotlib.
noise.py
uv run --with numpy --with matplotlib python3 noise.py
Further reading
The xorshift family and the shift triples that give a full period.1
What a generator is asked to do, and the tests it is asked to pass.3
White, pink and the other colours, with their slopes.2
-
Xorshift. Wikipedia. Retrieved 5 September 2026. https://en.wikipedia.org/wiki/Xorshift (opens in a new tab) ↩︎ ↩︎
-
Colors of noise. Wikipedia. Retrieved 5 September 2026. https://en.wikipedia.org/wiki/Colors_of_noise (opens in a new tab) ↩︎ ↩︎
-
Pseudorandom number generator. Wikipedia. Retrieved 5 September 2026. https://en.wikipedia.org/wiki/Pseudorandom_number_generator (opens in a new tab) ↩︎