Reproducible random numbers
PyTorch gives different random numbers for the same seed on a CPU and on a GPU, and its GPU numbers depend on the launch grid. This paper measures a counter-based generator in a Riptorch 0.4.1 pre-release that gives the same bits everywhere, how good its numbers are, and what it costs in speed.
Abstract
A Monte Carlo price, a dropout mask or a shuffled dataset should not change because the job moved from a laptop to a GPU server, or from 16 threads to 224. With stock PyTorch it does: the same seed gives different numbers on the CPU and on CUDA. Riptorch's 0.4.1 pre-release (0.4.1.dev3) draws from a counter-based generator, Philox4x32-10, whose state is a seed and an offset, so a draw is defined by its position in the stream and not by the device or thread that computes it.
On 30 November 2026 the same 128 draws (8 kinds, 4 seeds, offsets up to , sizes from 5 to 16,777,216 values) were hashed on an RTX 4090 (two pods), an H100 SXM and an Apple M4, on each machine's GPU, its CPU and a single-threaded reference: 2,000 SHA-256 hashes, 0 differences. A thread test gave one hash on 1, 3 and all threads on six machines. The full random-number suite passed 123 of 123 on the RTX 4090, H100, Apple M4, an Intel Xeon 8488C (Sapphire Rapids) and an AMD EPYC 9R14 (Genoa). PractRand found no failure in 256 GB of the integer stream and 64 GB of the float32 uniforms. Unscrambled Sobol points are bit-equal to stock PyTorch's in every dimension tested up to 21,201.
On CPUs the draws ran 1.35–116× faster than stock PyTorch, except small float32 normal draws (65,536 values), where stock was faster on three of four CPUs (0.73–0.88×), and small float32 uniforms on one (0.96×). On GPUs Riptorch is behind (main RTX 4090 run and H100): 0.66–0.89× of stock at 64M values for uniforms, normals and integers on the RTX 4090 and H100 (0.47× for integers on the H100), as low as 0.16× at 1M values, and 0.02–0.59× for bernoulli. The measured cause of the small-draw GPU gap is that the engine recycles GPU memory late; an engine-wide fix is in progress.
The problem
Stock PyTorch's CPU generator is a Mersenne Twister; its CUDA generator is Philox, but CUDA kernels hand out the random values according to the launch grid, so the values an element receives depend on the GPU's number of multiprocessors. Neither stream is part of PyTorch's documented API, and the two never agree. A result computed on a CPU cannot be replayed on a GPU, a simulation cannot be split across mixed machines and recombined into exactly the single-machine run, and a GPU upgrade can move every random draw.
Riptorch 0.4.0 already had one global counter-based stream (manual_seed, rand and randn were reproducible), but generator= arguments shared that global stream, float64 draws were float32 values upcast (24 random bits), randint ran element by element in Python at about 1M values per second, every GPU draw was made on the CPU and copied, and there was no SobolEngine. The 0.4.1 work replaces all of that.
What Riptorch guarantees
- A counter-based generator. Philox4x32-10 (Salmon, Moraes, Dror and Shaw, SC11), matching the Random123 known-answer vectors. The state of a stream is two integers: a seed (64 bits) and an offset (the next 128-bit block a draw starts at).
manual_seedsets the offset to 0, and each draw moves it forward. - Thread-count and device independence. Each value depends only on (seed, offset) and its element index, never on how the work is split across threads, blocks or devices. The same seed and the same sequence of draws give the same tensors, bit for bit, on x86-64 and arm64 CPUs, NVIDIA GPUs and Apple GPUs, with no settings.
- Real per-Generator streams.
torch.Generatorcarries its own (seed, offset), withget_state/set_stateandget_offset/set_offset.set_offsetlets a large simulation be split across workers so that the union of the pieces is exactly the single-machine run. - Native dtypes. float32 uniforms carry 24 random bits, float64 uniforms 53; float16 and bfloat16 draws are float32 values rounded once.
randintmaps 64 random bits onto the range, so its bias is below range / (stock's modulo can reach range / on 32-bit ranges). - The PyTorch API.
manual_seed,initial_seed,seed,get_rng_state/set_rng_state,fork_rng,torch.Generator,rand,randn,randint,normal,bernoulli, the_likeforms,uniform_,normal_,random_,bernoulli_, andtorch.quasirandom.SobolEngine, with stock's shapes, dtypes,generator=,out=and error cases. Dropout masks,nn.initfills andrandpermdraw from the same stream. - What it is not. The numbers are not stock PyTorch's numbers for the same seed (stock's are device-dependent, so there is no single stock stream to match). Unscrambled Sobol points are the exception: they are deterministic by definition and equal stock's. Scrambled Sobol points are reproducible but differ from stock's scrambled points.
Method
Machines
| machine | processor | used for |
|---|---|---|
| RTX 4090 pod | GeForce RTX 4090 (sm_89, 24 GB), host EPYC 7352, 48 logical CPUs | tests, device hashes, GPU and CPU speed |
| RTX 4090 pod (2nd) | GeForce RTX 4090, host EPYC 7K62, 96 logical CPUs | tests, device hashes, GPU speed (dev2 build) |
| H100 pod | H100 SXM 80 GB HBM3 (sm_90), host Xeon Platinum 8480+, 224 logical CPUs | tests, device hashes, GPU and CPU speed |
| EC2 mac-m4 | Apple M4 (Metal) | tests, device hashes (macOS arm64 build) |
| EC2 c7i.4xlarge | Xeon Platinum 8488C (Sapphire Rapids), 16 vCPU | tests, CPU speed, PractRand 64 GB |
| EC2 c7a.4xlarge | EPYC 9R14 (Genoa), 16 vCPU | tests, CPU speed |
| EC2 c7i.8xlarge | Xeon Platinum 8488C | PractRand 256 GB and float 64 GB |
Checks
- Device hashes. 128 draws: rand, randn,
uniform_(−3, 5),normal_(1, 2)and bernoulli(0.3) in float32, rand and randn in float64, and int64 randint; seeds 0, 12345, −1 and at offsets 0, 7, and ; sizes 5, 4,099, 1,000,003 and 16,777,216. Each draw was SHA-256 hashed in separate processes on four paths: the machine's GPU at its default size threshold, the GPU with every draw forced onto it, the multithreaded CPU code, and a single-threaded reference (which skips the 16,777,216-value draws of the three 64-bit kinds). Any difference fails the run. - The suite (123 checks). 7 bit checks (Random123 known answers, 132 draws against an independent NumPy model of the stream, stream position after a sequence of draws), 14 state checks (seeding, state round trips, offsets, independent Generators,
fork_rng, dropout andrandpermrepeatability), 1 thread check (one hash on 1, 3 and all threads), 56 API checks against stock PyTorch installed alongside (2.13 on the GPU pods, 2.15 on the EC2 CPUs; shapes, dtypes, error cases, Sobol), and 45 statistical checks. - Statistics. 4,000,000 values per draw from a Generator seeded with 2026, α = per test: uniform and normal moments, Kolmogorov–Smirnov (on the first 1,000,000 values), chi-square on 1,024 bins and on 32 × 32 serial pairs, normal tails beyond 2, 3 and 4, lag-1 correlation, Box–Muller pair independence, four randint ranges up to , bernoulli for p from 0.001 to 0.9, and Sobol balance.
- PractRand 0.95, core test set, standard folding, fed through standard input: the integer stream as 32-bit words to 256 GB, and the float32 uniforms' 24 bits to 64 GB.
- Speed. Riptorch against
torch.<op>on the same machine in the same process, the median of 15 timed calls (5 at 64M values) after 3 warm-up calls, with a synchronize after every GPU call. Sizes 65,536, 1,048,576, 16,777,216 and 67,108,864 values. Stock PyTorch used its default thread count and its own Generator.
Results
Same bits on every device and thread count


On the RTX 4090 and H100, 64 of the 128 draws ran on the GPU at the default size threshold and all 128 when forced; on the Apple M4, 40 and 80. Every GPU hash equals the CPU and reference hashes. So one seed gives the same numbers on x86-64 (two instruction-set builds), arm64, CUDA sm_89 and sm_90, and Apple M4 Metal.


Statistical quality
The statistical checks drew the same samples on every machine, so they report the same values, apart from the last digits of some float64 sums computed by NumPy on each host. Every goodness-of-fit p-value is far from the threshold, and every moment, tail and frequency is within three standard errors of its expected value.


| statistic | float32 | float64 | expected |
|---|---|---|---|
| uniform mean | 0.50006 | 0.50014 | 0.5 |
| uniform variance | 0.083341 | 0.083352 | 0.083333 |
| uniform KS p (1M values) | 0.72 | 0.223 | > 10⁻⁴ |
| uniform chi-square p, 1,024 bins | 0.408 | 0.874 | > 10⁻⁴ |
| normal variance | 0.99953 | 0.99972 | 1 |
| normal kurtosis | 2.9993 | 2.9976 | 3 |
| normal KS p (1M values) | 0.651 | 0.233 | > 10⁻⁴ |
| normals beyond ±3 (count) | 10,651 | 10,615 | 10,799 |
| normals beyond ±4 (count) | 256 | 207 | 253 |
PractRand evaluated 284 test results at 256 GB of the integer stream and 181 at 64 GB of the float32 uniforms, and reported no failure and nothing "suspicious" at any length. Two single "unusual" results appeared along the way (expected now and then among hundreds of tests), and each was gone at the next length. The second 64 GB word run read the same stream on another machine and reproduced the first run's results at every length, the same "unusual" included.
| stream | length | final result | flags along the way |
|---|---|---|---|
randint(0, 2³²) as 32-bit words | 256 GB ( bytes) | no anomalies in 284 results | one "unusual" at 1 GB (p = 9.2×10⁻⁴) |
rand() float32 as its 24 bits | 64 GB | no anomalies in 181 results | one "unusual" at 32 GB (p = 6.3×10⁻⁴) |
randint(0, 2³²) as 32-bit words, second machine | 64 GB | no anomalies in 263 results | the same "unusual" at 1 GB; identical results at every length (same stream) |

Sobol points
Riptorch's torch.quasirandom.SobolEngine uses Joe and Kuo's new-joe-kuo-6.21201 direction numbers, the set stock PyTorch documents, with 30 bits per coordinate and up to 21,201 dimensions. Unscrambled points equal stock's bit for bit for dimensions 1, 5, 40, 1,111 and 21,201 and through fast_forward, reset and draw_base2; the direction numbers of all 21,201 dimensions equal stock's state. The suite also checks that 2¹² Sobol points fill every 1/16 interval exactly 256 times. With scramble=True the points are reproducible from the seed but are not stock's scrambled points.
The two figures below are illustrations computed for this paper with NumPy from the published definitions of the two sequences, not Riptorch measurements. They show why quasi-random points are worth having in a reproducible library: for a smooth Monte Carlo integrand the Sobol error falls roughly as 1/N instead of 1/√N.


Speed on CPUs

| op | 65,536 | 1M | 16M | 64M |
|---|---|---|---|---|
| rand float32 | 0.59 (1.87×) | 1.87 (5.86×) | 6.41 (27.6×) | 8.30 (36.7×) |
| randn float32 | 0.24 (0.74×) | 0.88 (2.72×) | 0.99 (4.31×) | 1.00 (4.69×) |
| rand float64 | 0.30 (1.35×) | 1.09 (4.94×) | 0.54 (3.79×) | 0.54 (3.92×) |
| randn float64 | 0.17 (3.03×) | 0.42 (7.50×) | 0.31 (6.40×) | 0.32 (6.49×) |
| randint int64 | 0.59 (1.96×) | 1.30 (4.40×) | 0.52 (3.07×) | 0.53 (3.19×) |
| bernoulli float32 | 0.99 (14.5×) | 1.18 (15.1×) | 4.82 (67.7×) | 1.22 (17.1×) |
On the EC2 c7a.4xlarge (EPYC 9R14, Genoa) every row was faster than stock, from 1.39× (randn float32 at 65,536 values) to 116× (bernoulli at 16M). On the GPU pods' host CPUs (Xeon 8480+ with 224 logical CPUs, EPYC 7352 with 48, timed up to 16M values) Riptorch was 1.57–89× stock except the small float32 draws listed below. Across the four CPUs, every case where Riptorch is faster lies between 1.35× and 116×.

Speed on GPUs
| op | 4090 1M | 4090 16M | 4090 64M | H100 1M | H100 16M | H100 64M |
|---|---|---|---|---|---|---|
| rand float32 | 8.2 (0.17×) | 92 (0.47×) | 172 (0.79×) | 16.7 (0.25×) | 205 (0.50×) | 445 (0.79×) |
| randn float32 | 8.3 (0.16×) | 94 (0.49×) | 170 (0.81×) | 16.6 (0.26×) | 160 (0.50×) | 272 (0.66×) |
| rand float64 | 8.4 (0.20×) | 60 (0.65×) | 86 (0.88×) | 16.5 (0.28×) | 131 (0.48×) | 261 (0.78×) |
| randn float64 | 5.1 (0.45×) | 10.8 (0.71×) | 11.4 (0.66×) | 10.2 (0.22×) | 87 (0.60×) | 140 (0.85×) |
| randint int64 | 8.5 (0.18×) | 67 (0.64×) | 100 (0.89×) | 10.4 (0.17×) | 96 (0.34×) | 161 (0.47×) |
| bernoulli float32 | 0.86 (0.03×) | 9.8 (0.06×) | 32 (0.28×) | 0.79 (0.02×) | 12.3 (0.04×) | 48 (0.14×) |


At 65,536 values Riptorch ran at 0.50–0.86× of stock on the two GPUs. The best runs, on the second RTX 4090 pod, reached 0.96× for rand and randn float32 and 0.99× for randint at 64M values. Making the values on the CPU and copying them in would run at under 2 G/s for 1M values, so the GPU path stays the default for large draws.
Limitations and next steps
The limits of these results, each with its cause and, where one is planned, the next step.
- GPU draws of every size. In the main RTX 4090 run and on the H100 every measured GPU draw is slower than stock PyTorch: 0.16–0.45× at 1M values (uniforms, normals and integers), 0.34–0.71× at 16M, 0.47–0.89× at 64M. Cause: the engine recycles GPU memory late. A tensor's GPU buffer returns to the pool only after the engine's garbage collector has run and the stream has synchronised, so a fresh draw often pays a new GPU allocation and prefetch (80–800 µs). The second RTX 4090 pod (an earlier build of the same streams) measured 0.15–0.61× at 1M, up to 0.84× at 16M and up to 0.99× at 64M. With the memory pool warm,
randof values took 0.38 ms on the RTX 4090 against stock's 0.31 ms (0.83×). The fix is engine-wide (a tensor releases its GPU buffer when its last handle goes) and is in progress; every GPU operation gains from it, not only random numbers. Until then, one large draw per batch of paths (for examplerandn(paths, steps)) instead of many small ones keeps Riptorch at about 0.66–0.88× of stock with identical values on every device.

- bernoulli on GPUs runs at 0.02–0.59× of stock, 0.14–0.32× even at 64M values (all three GPU runs). Cause: under investigation.
- Small float32 normal draws on CPUs. randn float32 at 65,536 values ran at 0.74× of stock on the Xeon 8488C (0.60× with an earlier build, 0.97× in a separate run of the same code), 0.73× on the H100 pod's Xeon 8480+ and 0.88× on the RTX 4090 pod's EPYC 7352; it was 1.39× on the EPYC 9R14. Reason, stated for the Intel machines: at this size stock fills the values on one thread with its AVX-512 Box–Muller, and spreading so small a draw across threads costs more than it saves. On the EPYC 7352: under investigation. Stock's generator cannot be used instead without breaking the same-bits guarantee. At 1M values Riptorch is 1.6–6.5× stock on the same machines, and up to 9.6× at 64M.
- Small float32 uniforms on the H100 pod's CPU: rand float32 at 65,536 values ran at 0.96× of stock. Cause: under investigation.
- 64-bit outputs above 1M values on CPUs. rand and randn float64 and int64 randint run slower at 16M and 64M values than at 1M on both EC2 CPUs (still 2.3–7.5× stock). Cause: page faults on fresh result memory above 32 MB in the engine's allocator, which every operation shares.
- Coverage. Metal makes only the float32 kinds on Apple GPUs (float64 and integer draws run on the CPU, with the same bits). AMD GPUs use the CPU path; they were not measured in this campaign. No speed was measured on the Apple M4.
Reproduction notes
- Software. The random-number work described here is in Riptorch's 0.4.1 pre-release builds (0.4.1.dev3, manylinux x86-64 and macOS arm64), not in 0.4.0, whose random numbers are the basic global stream described under The problem. 0.4.1 is not yet published; until it is, the Riptorch page and the playgrounds are the public entry points, and evaluation builds are available through Request an evaluation.
- Checks. The device hash test, the 123-check suite, the PractRand feeder and the speed script were run on the machines listed in Table 1 (see its last column), each in a fresh environment with stock PyTorch installed alongside. Seeds, sizes and offsets are listed under Method.
- Records. Run
rng-2026-11-30: per-machine test, hash and speed JSON, logs, machine records and the PractRand outputs. Evaluation partners can have the raw files on request. Cost of the whole campaign: about $2.40 of rented machine time. - Figures 8 and 9 were computed with NumPy from the public definitions of Philox4x32-10 and the two-dimensional Sobol sequence (Gray-code order, 30 bits); they are illustrations, not measurements.
Date and version
Version 1.1, 30 November 2026. Measurements from 30 November 2026. Updated 1 December 2026: limitations section retitled.
Working with Riptide
Riptide works with a small number of teams. Each engagement starts with 4 weeks inside your systems, learning how your models, data and infrastructure work. Only then does Riptide apply its software to them. Contact samario@riptidecompute.com or riptidecompute.com/evaluate.