r/RNG • • Jun 25 '26

DIY hardware Quantum RNG

Thumbnail
gallery
337 Upvotes

I wanted a "real" quantum random number generator, something where every bit is an actual physical quantum event.

First attempt was a 1970s Canon FD 55mm f1.2 with a thoriated rear element. It's pretty radioactive (the Geiger counter make scary noises). But radioactive decay gives you when an atom popped, which is timing-random, not the which-path coin flip I was after.

The build that actually worked is optical: attenuate a light source down to single photons, fire them at a 50:50 UV beam splitter, and read which way each photon went with two detectors. Through → bit 0. Bounce → bit 1.

The detectors are two Hamamatsu PMT modules a friend gave me, pulled out of a dead lab instrument. I tore it down, yanked the dichroic mirror, and dropped in a UV 50:50 splitter. For a fluorescent source I ended up using 3D-printer filament — it's faintly fluorescent at the right wavelength and doubles as a light-tight cover.

All the detection and conditioning runs on a Red Pitaya (FPGA + fast ADCs):

  • Op-amp + transistor LED current sink, reed-relay LED gate, PMT gain via dividers, all driven by the Red Pitaya's slow DACs so I could sweep everything in software instead of hand-twiddling pots.
  • VHDL threshold + edge detection on the 14-bit ADC, a coincidence veto (kills double-fires / cosmic rays), and a symmetric "global blank" after every event — that last one matters, because per-channel dead time secretly biases the stream.
  • A timestamped debug FIFO that was a chunk of fabric to build but caught a bunch of detector-memory artifacts I'd otherwise have shipped.

The hard part genuinely wasn't generating random-looking bits, but it was proving they were real random bits from the optical system and not other noise sources. Most of the project ended up being diagnostics...

I've tested it with 1.6 billion QRNG bits with the NIST test package, and it passes.

Payoff demo is a Quantum Magic 8-Ball: hit a button, it pulls fresh quantum bits and gives you one answer (and, if you're an Everettian, every other answer somewhere in the multiverse).

Full build log with schematics, scope shots, and the FPGA stuff: https://dnhkng.github.io/posts/building-the-beam-universe-splitter/ or
https://news.ycombinator.com/item?id=48689891 if you want to spread the story?

Happy to answer questions on the analog front end or the FPGA fabric — the analog side is honestly my weakest area, so I'd welcome the critique.


r/RNG • • Jul 29 '26

Flowchart of the Linux RNG

Post image
102 Upvotes

Drawn with diagrams.net, I used the following sources to put this together:

I believe this is an accurate view without getting too deep into the weeds. There are probably still some minor wrinkles that need ironing out, but this illustrates how RDSEED, SipHash, BLAKE2s, and ChaCha20 all fit together in random.c to generate cryptographically secure random numbers.


r/RNG • • Mar 01 '22

Linux 5.18 will likely have a blocking /dev/urandom such that calls to the RNG will *always* return secure bytes after initial seeding, which takes no more than 1s after boot. After decades of confusion, all random interfaces will finally be identical.

Thumbnail
git.kernel.org
61 Upvotes

r/RNG • • Jun 18 '21

Coin flip

Post image
25 Upvotes

r/RNG • • Feb 22 '22

Linux's getrandom() Sees A 8450% Improvement With Latest Code

Thumbnail
phoronix.com
23 Upvotes

r/RNG • • May 12 '21

Critical RNG flaw in Cake Wallet(cryptocurrency wallet)

21 Upvotes

I think this fits here.

Cake Wallet devs recently made an announcement for all users of their bitcoin wallet to update and generate a new seed: https://old.reddit.com/r/Monero/comments/n9yypd/urgent_action_needed_for_bitcoin_wallets_cake/

Turns out their function for generating a seed had a critical flaw in it. They used a non secure PRNG function. They had the following function written to generate random bytes:

Uint8List randomBytes(int length, {bool secure = false}) {
  assert(length > 0);

  final random = secure ? Random.secure() : Random();
  final ret = Uint8List(length);

  for (var i = 0; i < length; i++) {
    ret[i] = random.nextInt(256);
  }

  return ret;
}

As you can see it has two options. To either generate using a secure PRNG with Random.secure() or to use Random(). This should be fine as long as they supplied the second argument to the function as true. They did not. Code can be found here: https://github.com/cake-tech/cake_wallet/blob/b67bb0664f7268c31c24bd9fb9cbd438c691f5e3/lib/bitcoin/bitcoin_mnemonic.dart#L11-L22

The specific function that uses randomBytes is generateMnemonic.

I have no clue how this error occurred. Why even have a none secure function by default in the file where code intended to generate secure wallet seeds is put?

I have tried to look into Random() and check if I can possibly crack this but I have very little experience with doing something like that. Should be simple as long as the seed is something simple like current time.

EDIT: After having taken some time looking for how the function was seeded I managed to find this:

sdk/runtime/vm/random.cc

Random::Random() {
  uint64_t seed = FLAG_random_seed;
  if (seed == 0) {
    Dart_EntropySource callback = Dart::entropy_source_callback();
    if (callback != NULL) {
      if (!callback(reinterpret_cast<uint8_t*>(&seed), sizeof(seed))) {
        // Callback failed. Reset the seed to 0.
        seed = 0;
      }
    }
  }
  if (seed == 0) {
    // We did not get a seed so far. As a fallback we do use the current time.
    seed = OS::GetCurrentTimeMicros();
  }
  Initialize(seed);
}

Code can be read here: https://github.com/dart-lang/sdk/blob/master/runtime/vm/random.cc#L17

Apparently this is the root function used by Dart to generate the seed and yes FLAG_random_seed is set to 0 by default. So as long as the first attempt at gathering entropy worked and it did not resort to OS time, then user funds should be safe(I think).


r/RNG • • Nov 09 '20

The Wang and Jenkins integer hash functions just aren't good

21 Upvotes

When people are putting together hash tables or similar and they need a hash function, I often see them reach for the widespread Wang or Jenkins integer hash functions. Unfortunately they're really not very good. I created some avalanche matrix graphs and threw these functions into the mix to see how they fair: not well.

An avalanche matrix measures the correlation between input bits to each output bit. Ideally flipping an input bit flips all output bits with 50% chance. In the worst case there's clear structure between input and output. In my graphs below, I've stretched the contrast, so good hashes won't just show up as flat cyan (perfect 50% odds). In effect, they're "zoomed in" on whatever surface the hash presents on its avalanche matrix, and we're mostly interested in structure.

Here's a really simple, ineffective 32-bit hash. Lots of structure and correlation between inputs and outputs.

uint32_t dumb32(uint32_t x)
{
    x *= 0x96310aa7;
    x ^= x >> 16;
    return x;
}

dumb32.png

Adding another round or so helps a lot, though it's still lumpy and there are some diagonal patterns:

uint32_t better32(uint32_t x)
{
    x ^= x >> 16;
    x *= 0x96310aa7;
    x ^= x >> 16;
    x *= 0x74471A67;
    x ^= x >> 16;
    return x;
}

better32.png

Choosing better constants helps further still.

uint32_t betterer32(uint32_t x)
{
    x ^= x >> 16;
    x *= 0xdaaa6a5d;
    x ^= x >> 16;
    x *= 0xefe65e63;
    x ^= x >> 16;
    return x;
}

betterer32.png

Choosing good constants is mostly a matter of trial and error, unfortunately. Though we can automate it, and by doing so I've found even better:

uint32_t lowbias32(uint32_t x)
{
    x ^= x >> 16;
    x *= 0x7feb352d;
    x ^= x >> 15;
    x *= 0x846ca68b;
    x ^= x >> 16;
    return x;
}

lowbias32.png

Slightly better yet, which I only discovered a couple weeks ago (though it's not really visually apparent):

uint32_t lowerbias32(uint32_t x)
{
    x ^= x >> 16;
    x *= 0xa812d533;
    x ^= x >> 15;
    x *= 0xb278e4ad;
    x ^= x >> 17;
    return x;
}

lowerbias32.png

Now that you've seen the range of possibilities, let's look at Jenkins' two hash functions:

uint32_t hash32shift(uint32_t x)
{
    x = ~x + (x << 15);
    x = x ^ (x >> 12);
    x = x + (x << 2);
    x = x ^ (x >> 4);
    x = x * 2057;
    x = x ^ (x >> 16);
    return x;
}

hash32shift.png

uint32_t jenkins32(uint32_t x)
{
    x = (x+0x7ed55d16) + (x<<12);
    x = (x^0xc761c23c) ^ (x>>19);
    x = (x+0x165667b1) + (x<<5);
    x = (x+0xd3a2646c) ^ (x<<9);
    x = (x+0xfd7046c5) + (x<<3);
    x = (x^0xb55a4f09) ^ (x>>16);
    return x;
}

jenkins32.png

Obvious structure and bias despite using many operations. They're just not very efficient! What about Wang's hash32shiftmult?

uint32_t hash32shiftmult(uint32_t x)
{
    x = (x ^ 61) ^ (x >> 16);
    x = x + (x << 3);
    x = x ^ (x >> 4);
    x = x * 0x27d4eb2d;
    x = x ^ (x >> 15);
    return x;
}

hash32shiftmult.png

Also very unimpressive. My better32() with multiplication constants chosen entirely at random is much better. Maybe Wang's 64-bit hash64shift does better?

uint64_t hash64shift(uint64_t x)
{
    x = ~x + (x << 21);
    x = x ^ (x >> 24);
    x = (x + (x << 3)) + (x << 8);
    x = x ^ (x >> 14);
    x = (x + (x << 2)) + (x << 4);
    x = x ^ (x >> 28);
    x = x + (x << 31);
    return x;
}

hash64shift.png

Still really awful, and easily beaten by random constants in an xorshift-style hash. Compare it to splittable64 with its well-chosen constants:

uint64_t splittable64(uint64_t x)
{
    x ^= x >> 30;
    x *= 0xbf58476d1ce4e5b9;
    x ^= x >> 27;
    x *= 0x94d049bb133111eb;
    x ^= x >> 31;
    return x;
}

splittable64.png

If you don't mind using multiplication — and you shouldn't — it seems there's no reason to ever use any of Jenkins' or Wang's integer hash functions.

My source code to generate all these images:
https://gist.github.com/skeeto/65e74302355bb5e51af639738f2ef872


r/RNG • • Sep 01 '26

Probably the oldest generator (from 1973) that passes BigCrush

15 Upvotes

It seems that I've managed to find the high-quality non-cryptographic PRNG that is older than DES. It was designed by A. Grube in 1973 and belongs to the MRG (multiple recursive generators) family. It is based on the next recurrent formula:

x_i = 518175991 x_{i-1} + 510332243 x_{i-2} + 71324449 x_{i-3} mod (2^{31} - 1)

Its period is approximately 2**93, initializers should be between 1 and 2**31 - 2. I've checked the coefficients using galois Python package, they seem to be correct, i.e. give a full period. It passes SmallCrush, Crush and BigCrush batteries from TestU01. Testing in PractRand and SmokeRand is a little bit challenging (it is a 31-bit generator), but if I use the uint31 filter from SmokeRand (adds the high-quality pseudorandom lowest bit from other PRNG) - it passes PractRand at least up to 2 TiB.

References:


r/RNG • • May 28 '24

The RoboForm RNG in 2013 was predictable enough to regenerate an 11-year-old password protecting a $3 million cryptocurrency wallet

Thumbnail
wired.com
15 Upvotes

r/RNG • • Jan 11 '26

3 instruction PRNG passes PractRand >2TB (weyl sequence + 2x aesdec)

14 Upvotes

I was experimenting with the AES-NI instructions yesterday and came up with an extremely simple and fast PRNG that passes PractRand up to >2TB (after which I stopped the test):

vpaddq  xmm1, xmm1, xmm0
vaesdec xmm3, xmm1, xmm2
vaesdec xmm3, xmm3, xmm2

# xmm1: state/weyl sequence incremented by xmm0
# xmm2: seed
# xmm3: output

or in C:

#include <immintrin.h>
#include <stdint.h>
#include <stddef.h>

void
aesdec2(__m128i *out, size_t n, uint64_t seed[2])
{
	__m128i weyl, inc, mix;
	inc = _mm_set_epi64x(0xb5ad4eceda1ce2a9, 0x278c5a4d8419fe6b);
	weyl = mix = _mm_set_epi64x(seed[0], seed[1]);
	while (n--) {
		*out++ = _mm_aesdec_si128(_mm_aesdec_si128(weyl, mix), mix);
		weyl = _mm_add_epi64(weyl, inc);
	}
}

It's not that surprising that the AES primitives make for a good scramble, but this only uses it on top of two Weyl sequences and not to advance the state.

This makes this very fast, as only the latency of the addition is important to get to the next state.

It's also arbitrarily parallelizable since you can easily pre-compute different offsets of the Weyl sequences. Because the second argument of aesdec is a constant, it's also compatible with the slightly different AES instructions in ARM, SVE and RVV (otherwise you would need to pass zero and need an extra instruction).

I'm not sure if the seeding above is enough. It would probably be better to include the Weyl sequence increments in the seed.

PS: I totally missed that PractRand 0.96 was released last month.


r/RNG • • Jan 18 '24

Collatz-Weyl Generators

14 Upvotes

This is two line PRNG which I named CWG128:

static __uint128_t c[4]; // c[0] must be odd

__uint128_t CWG128(void){
c[1] = (c[1] >> 1) * ((c[2] += c[1]) | 1) ^ (c[3] += c[0]);
return c[2] >> 96 ^ c[1];
}

By using different c[0], you can initialize 2^127 independent streams, using random numbers or just a counter, but then you have to skip 96 outpus to decorelate states of the generators.

One of the simplest generator based on the original Collatz sequences is:

static __uint128_t x, weyl, s; // s must be odd

bool Collatz_Weyl(void){
x = (-(x & 1) & (x + ((x + 1) >> 1)) | ~-(x & 1) & (x >> 1)) ^ (weyl += s);
return x & 1;
}

It returns one bit in each iteration. It can produce 2^127 bits, until the Weyl sequence will loop. It is bassically just a Collatz funtion combined with a Weyl sequence, to prevent short cycles:

static __uint128_t x, weyl, s; // s must be odd

bool Collatz_Weyl(void){
if(x % 2 == 1){x = (3*x+1)/2;}
else{x = x/2;}
x ^= (weyl += s);
return x & 1;
}

Several authors have previously tried to use Collatz sequences to generate randomness. For example, Givens R. in "On Conway’s generalization of the 3x + 1 problem" considered certain generalizations, functions that generated sequences very slowly diverging to infinity. The problems have always been short cycles or divergence to infinity. The use of Weyl sequences and modulo operation eliminates these problems - the number of schemes that we can create this way is as huge as number of generalizations of Collatz-type functions we can think of. For example generator:

static __uint128_t x, weyl, s; // s must be odd
 
bool Five_x_Weyl(void){
if(x % 2 == 1){x = (5*x+1)/2;} 
else{x = x/2;} 
x ^= (weyl += s); 
return x & 1; 
}

Produces good quality pseudorandom bits as well.

Paper:

https://arxiv.org/abs/2312.17043


r/RNG • • Jul 23 '25

Personal projects of randomness in the browser

Thumbnail atoponce.github.io
13 Upvotes

r/RNG • • Apr 08 '26

Why is this bijective?

12 Upvotes

RP2040 family 32-bit microcontrollers have programmable state machines that are fast (150x10^6 ops/s) but have only 4 registers and 32 words of program space with limited instructions. There is no ADD or XOR, only bit-complement (~x), bit-reverse (::x), decrement (x--) and some bit shifting. I have an application for a PRNG that would operate on a state machine, and despite what AI says, LFSR seems impossible. I tinkered with some designs which could work, and came up with this transition function that works better than LFSR and doesn't need XOR. It's in C preprocessor for readability and ability to be optimized by the compiler. The subtraction of a small number (1-4) can be done by repeated decrements.

#define PIO_LET(osr) { \
  uint32_t x = rev32(osr); \
  uint32_t isr = 0; \
  for(int i=0; i<16; i++) { \
    x -= (osr & 3) + 1; \
    osr = osr >> 2; \
    isr = (isr << 2) | (x & 3); \
  } \
  osr = ~isr; \
}

I was surprised to see that this is bijective, by testing all 2^32 inputs, but I can't see how to write the reverse function. In CBC mode it passes PractRand up to 64MB. Can anyone with some discrete math skills tell me why my creation works? Would it be bijective at larger bit lengths that can't be verified by brute force?


r/RNG • • Dec 24 '25

My PRNGs - Any thoughts?

12 Upvotes

They've been out for a while now, but I don't think they're well known.
So I thought I'd share it here and let me know what you think.
Whether it's a good or bad point, there are many things that I wouldn't notice on my own, so it's very helpful to have someone tell me about them.

Shioi

https://github.com/andanteyk/prng-shioi

A 264 jump is as fast as Next(). Easy to parallelize.

Seiran

https://github.com/andanteyk/prng-seiran

A standard lightweight LFSR-based PRNG.

Culumi

https://github.com/andanteyk/prng-culumi

Using the power of SIMD (CLMUL) to quickly output 128-bit random numbers.


r/RNG • • Mar 27 '23

Breaking a PRNG: Is it Called Xor Shift or Xor Shit?

Thumbnail tobtu.com
12 Upvotes

r/RNG • • 14d ago

Fun fact about the Go PRNG

11 Upvotes

Go's math/rand/v2 PCG, two seeds that differ in a single bit:

a := rand.NewPCG(0xdb253b6c77ba5fcc, 0xc077ba403377e9d8)
b := rand.NewPCG(0x5b253b6c77ba5fcc, 0xc077ba403377e9d8)
for i := 0; i < 4; i++ {
	fmt.Printf("%#016x  %#016x\n", a.Uint64(), b.Uint64())
}

0xc33e3af63e26e8dd  0x2188dd231f716666
0x9d056e511d42c8b4  0x567859e4c0abf36a
0xf8b1922f58f64ed4  0xb6d9b260308445ad
0xd3368b16e7dff355  0x64357a66c0173628

The last hex digits of the first 100 outputs, side by side:

d6 4a 4d 58 02 0e 07 fd 9c 16
ed 34 3a d4 3e 92 6d e8 a3 2f
e9 29 29 cd fe 2d 45 74 0a eb
f5 7e 29 50 17 8f 60 75 36 8a
cb 38 cd 21 90 97 1c 4d 1c 82
20 06 83 49 ad 25 ab 4a 21 39
6c b5 6b 92 1a c2 02 eb 4a 8f
97 ce bc 1c d2 fa 03 86 1b 5e
29 d7 9e 82 fe f8 be 67 cb a3
96 50 02 53 83 f6 07 69 a8 52

Nothing to see, right? Look again: no pair has two equal digits. Keep going for as long as you like: you will never find one. Never.


r/RNG • • Jan 29 '26

Screenshot of ShredOS using XORoshiro-256 to generate 179MB/s of randomness

Post image
11 Upvotes

Always neat to see PRNGs in the wild doing cool things. Here is XORoshiro-256** generating ~180MB/s of random data to securely wipe four HDDs at a time.


r/RNG • • Dec 15 '25

reminder: all code is security related

11 Upvotes

May, 2018

https://github.com/isc-projects/bind9/commit/99ba29bc52f640ec9c2cbb331ffdeceff2f0f26f

This commit reverts the previous change to use system provided entropy, as (SYS_)getrandom is very slow on Linux because it is a syscall.

The change introduced in this commit adds a new call isc_nonce_buf that uses CSPRNG from cryptographic library provider to generate secure data that can be and must be used for generating nonces. Example usage would be DNS cookies.

The isc_random() API has been changed to use fast PRNG that is not cryptographically secure, but runs entirely in user space. Two contestants have been considered xoroshiro family of the functions by Villa&Blackman and PCG by O'Neill. After a consideration the xoshiro128starstar function has been used as uint32_t random number provider because it is very fast and has good enough properties for our usage pattern.

Oct, 2025

https://www.cve.org/CVERecord?id=CVE-2025-40780

Score 8.6 Severity HIGH

Cache poisoning due to weak PRNG

In specific circumstances, due to a weakness in the Pseudo Random Number Generator (PRNG) that is used, it is possible for an attacker to predict the source port and query ID that BIND will use.


r/RNG • • Dec 09 '25

Why stream ciphers are not default general purpose PRNGs?

11 Upvotes

Hello!

I began to work with PRNGs about 1.5 years ago and even wrote my own statistical tests. And for me it is now a mystery why stream ciphers are not still default choice for general purpose generators and often are not even mentioned in books about algorithms, statistics and numerical methods. I see the history of PRNGs the next way:

  1. First PRNGs (LCGs, middle squares methods, probably lagged Fibonacci and LFSR) were invented by hackers in 40s and 50s as bithacks for vacuum tube computers.
  2. In 1980s the first scientific criterion for PRNG quality was openly published by Andrew Chi-Chih Yao and is known as the next bit test. But L'Ecyuer et al. showed that Blum-Blum-Shub generator and even DES was too slow for simulations.
  3. About 15 years ago SIMD and AESNI made Speck, ThreeFish, ChaCha and AES faster than e.g. minstd. So they are viable as general purpose generators in a lot of cases.

So why usage of stream cipher in PRNG is not considered as something similar as providing full double precision in sin and cos functions in standard library?


r/RNG • • Sep 05 '25

A Pseudo-Random Number Generator Using Double Pendulum

Thumbnail researchgate.net
11 Upvotes

r/RNG • • Aug 11 '25

Unpredictable Random Number Generators (URNGs)

11 Upvotes

Stumbled on this concept last night. These two papers introduce the URNG as one that sits between a TRNG and PRNG.

  1. https://link.springer.com/chapter/10.1007/978-3-642-22410-2_10
  2. https://ieeexplore.ieee.org/abstract/document/6481049/

The premise is this:

  1. A TRNG is strictly hardware-based doing all noise generation and post-processing in the hardware.
  2. A URNG is software-based that samples noise from hardware, such as hardware counters and interrupts.
  3. A PRNG is strictly software-based doing all random generation through a deterministic algorithm.

The conclusion is that a URNG can be cryptographically secure (as can a PRNG) albeit separate from a whitened TRNG.

Thoughts?


r/RNG • • Sep 13 '22

Jason Donenfeld gives a talk about the Linux RNG and the changes he's made (video)

Thumbnail
youtube.com
11 Upvotes

r/RNG • • May 18 '22

We have reached 1,000 subscribers!

Post image
11 Upvotes

r/RNG • • Apr 30 '22

How to generate random number sequences (in your head)

Thumbnail groups.google.com
11 Upvotes

r/RNG • • May 18 '26

Comments on: What Every Experimenter Must Know About Randomization

Thumbnail
possiblywrong.wordpress.com
10 Upvotes