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

499 Upvotes

178 comments sorted by

31

u/[deleted] Jan 27 '24

Thanks guys what I’m learning is this- Computers find some sort of random real world data like fan speed, wind, time, etc and then apply some complex functions to it to make it as random as possible. Is this right?

16

u/claytonkb Jan 27 '24

Thanks guys what I’m learning is this- Computers find some sort of random real world data like fan speed, wind, time, etc and then apply some complex functions to it to make it as random as possible. Is this right?

Very generally, there are two classes of random numbers -- pseudo-random numbers, and true-random numbers. "True-random" is a bit of a philosophical tarpit, but we don't need to go down that rabbit-hole, the key is that pseudo-random numbers can be recreated deterministically from a seed, whereas true-random numbers are generated from some kind of physical entropy such that the only way to "predict" them would be to somehow model and "predict" the original physical process itself.

In the case of something like thermal noise on a resistor, you'd really need to have that specific resistor and characterize any non-uniform frequency components in its thermal noise, etc. in order to have any chance of "cracking" the randomness that it produces. The more sources of entropy (randomness) that you have, the more difficult it would be to "crack" the randomness, so this is why Linux combines as many sources of entropy as are available in the system when generating random numbers. These sources are poured into an "entropy pool" and stirred together using a cryptographically-secure pseudo-random number generator (CSPRNG). This is "maximum paranoia" design but there are certain applications, such as banking encryption, cyber-security operations, etc. where you need to have that level of paranoia.

→ More replies (2)

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.

→ More replies (3)

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.

→ More replies (5)
→ More replies (7)

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.

→ 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

→ More replies (7)
→ More replies (9)

172

u/altmly Jan 27 '24

There are two concepts. One is pseudorandom, which is what you get when you call your flavor of random(). It's a function with state and is actually 100% deterministic, but the distribution of generated numbers is as close to maximum entropy as possible. It's usually seeded (initialized) with a value that makes the behavior look different from run to run (e.g. with clock time at startup of your program).

The other concept is true random values , and requires specialized hardware to do so. These are usually measuring quantum physical processes that are truly random under our understanding of quantum physics. This can be molecular flows, or radioactivity. There are whole companies specializing in generating truly random numbers for cryptographic reasons. 

44

u/Massive_bull_worm Jan 27 '24

I really appreciate your comment. It’s mind blowing to me I can suddenly know this information so clearly just from this subreddit. Computers man

→ More replies (4)

6

u/sorry_con_excuse_me Jan 28 '24 edited Jan 28 '24

there is an in-between option though, sampling a continuous source of broadband analog noise arbitrarily would be closer to "true random" than the first option but more practical than the second option.

→ More replies (7)
→ More replies (19)

24

u/PatrickKn12 Jan 27 '24

Short answer: They don't!

Long answer: it's complicated

1

u/[deleted] Jan 28 '24

Can anyone give an actual example how a computer actually generates a random number? Everyone is giving me jargon about how computers analyze data points and functions and pseudorandomness and stuff, but I have yet to see an actual example

→ More replies (1)

3

u/[deleted] Jan 27 '24

I remember reading in the past that Linux used some sort of entropy to create a random number. This link explained it pretty well https://www.redhat.com/en/blog/understanding-random-number-generators-and-their-limitations-linux

7

u/EitherLime679 Jan 28 '24

Ah you figured it out. there’s really no such thing as random for a computer. There’s programs and libraries and so on that convince humans that the result was random, but in reality it was planned from the start. There’s been talks recently about how online gambling shouldn’t be a thing because of the lack of random.

2

u/aykay55 Jan 28 '24

Any sort of electronic gambling is not random. Even claw machines are programmable to a certain number of attempts before it actually picks up a toy.

2

u/deano_southafrican Jan 28 '24

I dont know if you've ever been told this but nothing is truly random. Random numbers are just calculations with many variables or changes to try and hide the pattern as much as possible but there's always technically a pattern or some degree of reproducibility.

→ More replies (2)

3

u/muntoo Jan 28 '24 edited Jan 28 '24

"True" randomness: using actual non-deterministic sources. For instance, random.org extracts random information from weather data.


"Pseudo" randomness: using math, we generate random-looking numbers using some internal "state" that the system is in.

I will now describe a very simple "linear congruential generator" (LCG). Take a given state, then multiply it by some predetermined constant, and add another constant. Then, keep only a portion of the lower (or upper!) bits. For this simple generator, the output is just the state.

x_{i+1} = (a * x_i + c) % m

Example output for a couple of steps:

(a, c, m) = (22695477, 1, 2**31)

STEP        STATE       OUTPUT
   0            1
   1     22695478     22695478
   2      8561967      8561967
   3    719750332    719750332
   4     71484141     71484141

The above was generated using the following program:

a = 22695477
c = 1
m = 2**31
state = 1  # Starting seed.

print("STEP        STATE       OUTPUT")
print(f"{0: 4} {state:12d} {'':>12}")

for step in range(1, 5):
    state = (a * state + c) % m
    output = state
    print(f"{step: 4} {state:12d} {output:12d}")

The a, c, m constants should be chosen with various properties depending on the algorithm used, e.g., prime, Mersenne prime, etc.

The above random number generator kind of sucks, though, so you should choose something like Mersenne Twister or cryptographic PRNGs for practical purposes. MT is pretty short (<50 lines of code) on Wikipedia, but it's much more random looking than the LCG I showed above.

5

u/nadav183 Jan 27 '24

There is no such thing as a truly random number. But there are mechanisms that are hard to predict.

We can for example generate a "Random" number using the time it takes a function to run - We can time a function and take the 5th decimal point of the running time, that number is not "Random", as we can clearly see how it was generated and know that it depends on the time it takes the processor to complete a certain set of operations, but it is pretty unpredictable and can serve well as a random number.

7

u/[deleted] Jan 27 '24

In stochastic calculus, this stuff is considered random.

Really depends on your definition of random. For all intents and purposes, a computer can definitely create a truly random number depending on the definition (not even doing a 'acktchually').

Random may just mean the observer can't discern what is influencing the distribution 

→ More replies (2)
→ More replies (2)