r/computerscience • • Jan 27 '24

How tf do computers generate random numbers?

Hi guys, I’ve been using random number generators lately and I can’t seem to figure out how a computer can generate a random number. Don’t they just do what they’re told? Please explain like im stupid Edit: holy moly this is blowing up

504 Upvotes

178 comments sorted by

View all comments

460

u/CipheredBytes Jan 27 '24

Computers use clever math tricks to make numbers that look random. They begin with a starting point called a seed and then follow a set of rules to create a sequence of numbers. The catch is that if you use the same starting point (seed), you'll get the exact same sequence. To make things less predictable, they often use things like the current time or user actions to set the initial seed. This makes the numbers seem random enough for things like games or security.

287

u/IBJON Jan 27 '24

Cloudflare generates their seeds using a wall of lavalamps and a camera. 

112

u/theusualguy512 Jan 27 '24

Apparently according to their website, they weren't the first ones who did it either (Silicon Graphics had seemingly patented it in 1996 but the patent expired).

They mix the lava lamp data with the ones generated from the Linux OS to ensure maximum entropy.

It's generally an interesting rabbit hole to fall into although I honestly do not understand the mathematics behind many of the algorithms for pseudorandom numbers and why they are proven to be mirroring random distributions.

Two popular algorithms for pseudorandom numbers according to Wikipedia seem to be the Mersenne Twister and PCG, Python uses PCG64 by default.

16

u/[deleted] Jan 28 '24

Well if Silicon Graphics did it that we can be sure NVidia ripped it off and did it.

→ More replies (1)

18

u/freddfx Jan 27 '24

Cloudflare's write-up seems to also answer OP's question. https://www.cloudflare.com/learning/ssl/lava-lamp-encryption/

26

u/deelowe Jan 27 '24

When I worked at google we installed special devices in the DC that used radioactive decay to generate true randomness.

6

u/[deleted] Jan 28 '24

tell me more ?!?!

→ More replies (3)

2

u/wormhole_bloom Jan 28 '24

This is awesome

1

u/sobriety-no Jul 17 '24

I imagine there are much more hardcore ways now, such as random numbers generated by some action within the realm of unpredictable quantum physics. I dont know if this is true, I just thought it as I wrote the comment, but I can imagine it is true

→ More replies (6)

8

u/No_District_3330 Jan 28 '24

interesting, so it can be predicted, right?

10

u/bazeon Jan 28 '24

If you know the seed and the random generator you can recreate it. That’s why stuff that matters for security use all tricks mentioned in the tread to get a unguessable seed.

2

u/aftersox Jan 28 '24

For most pseudorandom numbers, you can't make predictions in a closed formula. Meaning you just plug in the index and seed and get the number.The algorithm has to be iterated. But yes ultimately you can predict it.

→ More replies (2)

7

u/trickman01 Jan 28 '24

Older games sometimes used tables to simulate RNG. The original Final Fantasy for instance. It would start on one index of the table and then advance every time RNG was needed. To the casual gamer it looks completely random. Speed runners however know exactly how to manipulate it for the optimal result in each run.

4

u/The_Chief_of_Whip Jan 28 '24

Doom did the same thing, that’s how it was able to save replays by recording only player actions

11

u/dmazzoni Jan 28 '24

This answer is missing the fact that all major processors these days have a hardware random number generator built-in.

https://en.wikipedia.org/wiki/RDRAND

2

u/[deleted] Jan 28 '24 edited Jan 28 '24

an instruction for returning random numbers from an Intel on-chip hardware random number generator which has been seeded by an on-chip entropy source.[3] Intel introduced the feature around 2012, and AMD added support for the instruction in June 2015.

RDSEED is similar to RDRAND and provides lower-level access to the entropy-generating hardware. The RDSEED generator and processor instruction rdseed are available with Intel Broadwell CPUs[8] and AMD Zen CPUs.[9]

The generator takes pairs of 256-bit raw entropy samples generated by the hardware entropy source and applies them to an Advanced Encryption Standard (AES) (in CBC-MAC mode) conditioner which reduces them to a single 256-bit conditioned entropy sample. A deterministic random-bit generator called CTR DRBG defined in NIST SP 800-90A is seeded by the output from the conditioner, providing cryptographically secure random numbers to applications requesting them via the RDRAND instruction.

The entropy source for the RDSEED instruction runs asynchronously on a self-timed circuit and uses thermal noise within the silicon to output a random stream of bits at the rate of 3 GHz,[16] slower than the effective 6.4 Gbit/s obtainable from RDRAND (both rates are shared between all cores and threads)

- X86 hardware driven Random Number Generated Instruction

→ More replies (3)

3

u/anor_wondo Jan 28 '24

no one can ever trust such solutions

→ More replies (2)

2

u/Flair_Loop Jan 30 '24

This actually happened to us in I believe either my Algebra or precalc classes in high school. Our teacher was demonstrating the random number function on a TI-84 graphing calculator, explaining that the process wasn’t truly random. One kid in the back who was borrowing his calculator from the classroom was on the same seed as the teacher and their calculators were generating the same numbers.

1

u/[deleted] Jan 15 '25

They, however, generate pseudo-random numbers! 😎

1

u/Comprehensive-Flow-7 May 10 '25

I assume the set of rules are the same each time?

1

u/Remote_Try_6062 Mar 11 '26

does this meam that if you know tbe seed gen algorithm you can crack the seed, assuking you know the valuesnused in calculating the seed?

→ More replies (5)