Back Original

On WebGPU, default to using Threefry for random numbers

This piece has a runnable WebGPU benchmark and 7 interactive demos!
Ising model
Ising model at the critical temperature, where domains form at every scale. Interactive version below.

I’ve previously written about generating random numbers on the GPU, where I recommended using a counter-based RNG. Here, based on performance benchmarks and statistical tests I ran, I’m recommending a specific RNG for WebGPU code: use Threefry, specifically Threefry4x32-12. It’s fast and the random numbers are high quality.

If you only care about performance, use pcg4d. It’s 18% faster than Threefry on average, and never slower by much. But beware! It fails a lot of statistical tests of randomness real quick. Is the performance win worth it? That’s up to you to judge. It’s probably fine for rendering. You could reasonably say, “If you’re doing something serious then why are you on WebGPU?” For me, the tradeoff isn’t worth it; I’d rather not have to think, “Huh, I wonder if my results are wonky because the sequence of random numbers I’m using has weird regularities.”

I did this work because many of my projects use WebGPU to do heavy number crunching in the browser. WebGPU is cool for me because consumer hardware is now fast enough that I can make interactive demos of things like molecular dynamics simulation and Bayesian sampling. Nearly all these projects need to generate random numbers at some point, so I spent some time comparing WGSL implementations of several RNGs.

Why does it matter whether you’re using WebGPU or CUDA or something else? The hardware’s the same, right? The main reason is that the different shader languages provide different instructions and data types. This really matters. For example, Philox needs the high bits of 32x32->64 bit multiplication, and WebGPU doesn’t expose that even when running on hardware that supports it. Also, each browser’s WGSL compiler is different and probably not as mature as nvcc. This benchmark taught me that, at least in 2026, what’s right for CUDA isn’t right for WebGPU.

The rest of this post describes the benchmarks, the statistical tests, and some caveats. I’ll be mercifully brief. If you’re curious about something I didn’t write about, let me know on GitHub. Since it’s all WebGPU, this whole post is interactive! Every one of the seven benchmarks produces a picture, and some of them are kinda pretty! You can run all the benchmarks right now while you read, there’s a button for that in the summary chart below. There’s a json of the benchmark results that you can copy and send me to include here, if you like. Open a PR in the repo.

You might get different performance results because your hardware is different, but the statistical tests aren’t hardware dependent. I’d recommend using the RNG that’s fastest on the hardware you care about, that also doesn’t fail the tests quite badly. The biggest hole in my results is that I’ve only benchmarked on NVIDIA and Apple GPUs. That leaves a lot of devices not represented here: the GPUs in Android phones, integrated Intel GPUs, AMD chips, and more.

Results

Without further ado, here’s a chart of all the results:

The solid lines are counter-based RNGs; the dashed lines are stateful RNGs. As expected, counter-based RNGs won on most benchmarks. On one benchmark, path tracer, the stateful RNGs did quite well, probably because it’s compute bound and does relatively little memory traffic.

Here are the results of the statistical tests:

RNG PractRand, GPU layout PractRand, one thread BigCrush
Philox4x32-10 pass (1 TB) — pass
Threefry4x32-20 pass (1 TB) — pass
Threefry4x32-12 pass (1 TB) — pass
pcg4d fail (128 MB) fail (128 MB) fail (9 of 160)
xxhash32 fail (1 GB) fail (1 GB) fail (10 of 160)
TEA, 6 cycles pass (1 TB) — pass
XORWOW pass (1 TB) — pass
xoshiro128** pass (1 TB) — pass
PCG32, seeded with Philox pass (1 TB) pass (64 GB) pass
PCG32, pcg32_srandom_r(seed, thread) fail (128 MB) pass (64 GB) not run

Threefry4x32-12 is the fastest clean RNG on average, on these workloads and on my hardware. The Random123 paper reported that Threefry-12 is the minimum number of rounds that passes BigCrush. The Random123 library uses Threefry-20 by default, which has more rounds and therefore probably produces higher quality randoms. If you really care about quality and are willing to sacrifice 10-13% performance on average (sometimes more), use Threefry-20. It’s faster than every other clean generator on average.

pcg4d fails PractRand at 128 MB by a huge margin (p=2e-1332). That is, within generating the first 128 MB of data, the test finds regularities that are vanishingly unlikely to have happened by chance. This is very sad because pcg4d is really, really fast. Bummer. A single thread’s sequence fails at 128 MB as well, so it’s a problem whether your code is serial or parallel.

I tested several variants of Threefry not in the table above. No clean variant of Threefry beats pcg4d’s performance on WGSL. This is also a bummer. Threefry with 5 rounds matches the performance of pcg4d, but it fails PractRand real quick, after just 512 MB, about 3 seconds in.

“GPU layout” in the table here means the stream that’s actually produced when running a grid of threads on a GPU. “one thread” is just running the RNG on a single thread. The sizes for PractRand are how far PractRand got before the RNG either failed the test or I declared it a pass.

PCG32, when seeding naively (pcg32_srandom_r(seed, thread)), fails PractRand at 128 MB because neighboring threads are correlated. This is interesting! One thread on its own is clean to 64 GB. If you use PCG32, seed each thread with Philox; then it tests clean.

On both my desktop and my laptop, the benchmarks ran faster and gave more consistent results on Chrome than on Firefox. As of November 2026, Chrome’s WGPU implementation seems more mature than Firefox’s, at least on the workloads I measured.

Method

I tested nine RNGs, six stateless and three stateful. I included all the usual suspects that people use on GPUs in general and WGSL specifically: a couple PCG variants, a couple Threefry variants, Philox, XORWOW (cuRAND’s default), the fast and high-quality xoshiro128**, and a few others. I also tested several other variants of Threefry to see if any of them beats pcg4d while passing the tests (negative). With LLM assistance I produced a WGSL implementation of each. Whenever reference implementations or test vectors or papers were available, I tested that my implementation matched the reference. I’m pretty convinced the implementations are correct, in that they implement the algorithm they’re supposed to implement. For Threefry I tried several GPU code optimizations since the initial implementation was surprisingly slow. There’s a good chance I missed an optimization in Threefry or any of the other RNGs! It’s certainly possible these could be faster, and that may change my recommendation.

I ran the benchmark on three machines I own:

Machine OS Browsers
RTX 4060 desktop Linux Chrome 157, Firefox 160
2021 M1 Pro MacBook macOS Chrome 156, Firefox 159
iPhone 15 iOS Safari 27.0.1

Each RNG was tested in seven workloads. They are:

  1. pi: Monte Carlo estimate of pi by throwing darts in a unit square, and counting the fraction that land inside a quarter circle.
  2. ink: diffusion of ink, i.e., independent random walk of millions of particles, each moving by a small Gaussian at each timestep.
  3. path tracer: a port of a simple cuda path tracer rendering a Cornell box. It shoots many rays through each pixel, each with a small random jitter, and lets the rays bounce around the scene.
  4. Ising: simulation of a 2D Ising model near its critical temperature. This is a famous model in statistical physics. It’s one of the simplest systems that has a phase transition.
  5. dropout: a simple implementation of the ML trick of setting random parts of a layer’s output to zero, to avoid overfitting. Here the input is a 2048x2048 image of floats, and each element is kept with probability one half. It’s a memory-heavy workload so the counter-based RNGs do well.
  6. Gaussians: each thread performs the Box-Muller transform to turn uniform randoms into samples from the Gaussian distribution. Gaussians are one of the most common ways random numbers are used. The transform needs to compute some transcendentals, which are expensive, so performance differences between RNGs are diluted in this workload.
  7. Raw words: each thread just writes 16 bytes of random bits. It’s a baseline that measures just calling the generator on its own (plus a store).

I ran two major batteries of statistical tests, BigCrush and PractRand. Both of these work by drawing lots of random samples from a generator and seeing if they exhibit certain regularities. Neither test can prove that an RNG is good, but they can show that an RNG is bad in that the numbers it produces follow a pattern that real random numbers would not. For example, one of the things BigCrush tests is birthday spacings and collisions: throw numbers into buckets and count collisions. If there are too many or too few, that’s structure. A cool geometric test is to scatter points in a high-dimensional cube (by interpreting the random bits as coordinates) and check how close the nearest ones get. This catches lattice structure, a well-known weakness of the LCGs. PractRand has a smaller number of tests that are more sensitive per byte of input data. Papers about random numbers often mention these two collections of tests. For these tests, I had the LLM port the WGSL implementations to C, to make them easier to run: getting all the random numbers off the browser into the test programs would be complicated and brittle. I tested the C ports against the WGSL shaders.

Importantly, I ran the tests on the sequences produced by these generators when used in grids of threads on a GPU. I did this to catch regularities that might not be apparent when just using the RNGs in a single serial computation.

Demos

Ising model

Path tracer

Gaussians

Ink

Pi

Dropout

Raw words