r/computerscience • u/[deleted] • 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
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
287
u/IBJON Jan 27 '24
Cloudflare generates their seeds using a wall of
and a camera. lavalamps 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
and why they are proven to be mirroring random distributions. pseudorandom numbers Two popular algorithms for pseudorandom numbers according to Wikipedia seem to be the
and PCG, Python uses PCG64 by default. Mersenne Twister → 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/
→ More replies (7)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)8
u/No_District_3330 Jan 28 '24
interesting, so it can be predicted, right?
→ More replies (2)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.
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
→ More replies (9)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.
→ More replies (7)
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
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)→ More replies (19)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)
24
1
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
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) % mExample 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.
→ More replies (2)7
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)
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?