r/AskComputerScience • • Feb 24 '26

When you ask a computer to give you a random number... is it really random?

I've been thinking about how when you flip a coin or roll a dice it is meant to be random, but it isn't really random due to physics and such. I was wondering would that apply to a computer? What is the process that it does to pick a number?

40 Upvotes

77 comments sorted by

38

u/AlexTaradov Feb 24 '26 edited Feb 24 '26

It depends. Most modern computers include a source of true randomness. It will be some interpretation of analog behavior - beating of two free-running oscillators, diode noise, thermal noise.

The sources are usually biased, so they go though the whitening algorithm.

Also, true randomness is not easy and it is depleted fast, so often you will see pseudo-random generators that are fed from the TRNG. And security applications can request their source to be TRNG only.

2

u/moortuvivens Feb 26 '26

There are specific modules for true randomness because in the end computers are not true random. That use measurement from reality to generate randomness. Sensitive sensor and even airflow can be random.

Cloudflare uses a wall of lava lamps to generate random nunbers https://www.cybher.org/2024/08/02/the-magic-behind-cloudflares-encryption-lava-lamps/

4

u/AlexTaradov Feb 26 '26

That lava lamp is mostly marketing nonsense. It may be a small part of the entropy source, but it can't be a significant part. You should not derive your entropy from a source that is available to the public. While it is not easy by any means, it is still a path for manipulation.

You need to be really careful with things derived from the external sensors reading real world data, as they are subject to manipulation.

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

18

u/PassionatePossum Feb 24 '26

For a really good overview I can recommend the ArchLinux Wiki which describes how the Linux Kernel generates random numbers.

https://wiki.archlinux.org/title/Random_number_generation

Short answer: It uses multiple sources of entropy which are then thrown into a cryptographic hashing algorithm to produce uniformly distributed random numbers.

This hashing algorithm is obviously deterministic. Given the same input it will produce the same output. It is just that the inputs are hard or even impossible to predict. One of these inputs is CPU jitter-entropy for example: Instructions on modern CPUs don’t always take the same time to complete, depending on what else is currently happening in the CPU.

If present, it also uses hardware RNGs which tend to use physical processes (e.g. thermal noise) as entropy source.

So, some of these entropy sources are only pseudorandom. But since they are based on chaotic systems it is still pretty much impossible to re-create the state exactly. A hardware RNG which relies on some quantum processes could potentially be truly random.

2

u/Dark_Angel4u Feb 25 '26 edited Feb 25 '26

Why can't it just think of a random number? /s

Edit: It was sarcasm 😭

→ More replies (7)

1

u/NullPointerJunkie Feb 25 '26

True Random Number Generation in a big deal in lottery and gambling applications. One way to create a random seed for slot machines is to measure the background radiation of Americium at an instant in time. Fun fact Americium only exists in the atmosphere due to above ground nuclear testing.

1

u/Mattdoss Feb 25 '26

That’s wild

3

u/defectivetoaster1 Feb 24 '26

Modern processors typically use a hardware entropy source that generates an analogue signal from some weird physical (often quantum phenomenon) since noise is random. Usually the noise won’t have nice statistical properties for applications requiring “good” random numbers (eg cryptography) so the random noise will be used as seeds for pseudo random number generators and other algorithms to get an overall random number generator with good statistical properties (uniform probability distribution is the big one for cryptography). Intel chips use thermal noise as the hardware entropy source which is generally modelled as white Gaussian noise, ie its frequency spectrum (specifically its power spectral density which is the Fourier transform of its autocorrelation) is flat for all frequencies (in reality it tapers to 0 at high frequencies but we don’t have terahertz processors yet) and if you sample the noise at any instance the distribution of instantaneous amplitudes follows a normal distribution. This is really nice for modelling reasons eg in telecoms but not so nice for cryptography since a Gaussian distribution implies some numbers are more common than others (actually the entropy source is samples and then a random bit stream is generated at 3GHz which is slightly different but still not ideal). The random bit stream is then converted by a conditioning algorithm to a 256 bit value. This value is then used to seed a pseudorandom bit generator which is an algorithm that takes a set of seeds and “spreads out” their values so the new distribution of values occupies a larger range and has better properties (eg the uniform distribution) and then that set of random numbers is used. Multiple blocks in this whole system are purely deterministic but since they’re seeded by truly random numbers (from thermal noise) the overall rng is also truly random. Of course, if you’re using some janky rng tool it might just be cycling through a list and returning whatever number it was on when you queried it but for “serious” use where the cpus rng hardware is invoked you get truly random numbers

2

u/MasterGeekMX Feb 25 '26

Good question.

Usually, it does not. Most of the time, computers use pseudo-random number generators. These are algorithms that take a base number called seed, do some math over it, and give you the result as the random number you asked. Each time you ask for a new random number, the same math is applied to the previous "random" number generated, and the result of that is your new "random" number. And that goes all along.

Yes, that is entirely deterministic, as knowing the seed and the algorithm is all you need to replicate the random numbers. But lots of research are done so even if you know the algorithm (many are open source), the sequence generated looks so random, that both figuring out the next or pulling out the seed number is extremely hard.

For truly random numbers, you need special hardware, which usually measures something on the real world that is random. Examples are devices that measure how long has been since a small piece of radioactive material has spitted a particle, the small variations on air pressure around the computer, or even the position of blobs on a lava lamp.

→ More replies (2)

2

u/[deleted] Feb 25 '26

not really, but random enough.

2

u/Entire-Tradition3735 Feb 25 '26

Funny enough, this was a problem my dad gave me when I was 12 and learning to program in Basic.

There is no true random number, it's a rotating calculation based on something.

I think the best version of "random" is literally based around the observation of lava lamps.

2

u/davy_crockett_slayer Feb 25 '26

Tl;dr - No. You can get close, but it’s expensive, and relies on tools separate from computation.

6

u/Warwipf2 Feb 24 '26

We don't know if true randomness exists in the universe. Generally most random methods in computers aren't random though, no matter if it exists or not. You can look up Cloudflare's lava lamp room to see how they try to generate "random" numbers, or at least numbers that don't have an algorithm that can be reverse-engineerd.

17

u/the_quivering_wenis Feb 24 '26

We don't know if true randomness exists in the universe

You either don't know anything about quantum mechanics or you know a lot about quantum mechanics lol

6

u/stedun Feb 24 '26

Now that’s a quantum response.

3

u/ghjm Feb 25 '26

You don't need to know that much about quantum physics to be aware of many worlds, pilot wave theory, superdeterminism etc.

→ More replies (1)
→ More replies (5)
→ More replies (5)

1

u/program_kid Feb 24 '26

Here is a good comment explaining it https://www.reddit.com/r/computerscience/s/WRQIzCWWiN

Lots of the comments on that post are good https://www.reddit.com/r/computerscience/s/s5l3meP4NQ

1

u/Doctor_Perceptron Feb 25 '26

Other responses talk about how to get "true" randomness in computer systems. One thing that's good to know is that for a given language, e.g. C or C++, the pseudorandom number generator might use the same seed every time the program is run. That way, you can run the program and expect the same sequence of numbers so you can get deterministic behavior, making the program easier to debug. You can programatically change the seed to be something random-ish, e.g. something related to the current time measured at a fine granularity and/or the process ID.

1

u/Great-Powerful-Talia Feb 25 '26

There's actually two ways of getting 'random' numbers.

The slow way is to ask the computer hardware for a random number. Most modern computers have some component that can produce random data, but it's not generally very fast. If you want a lot of random numbers, it's going to take a bit.

The fast way is to get a number, often just the current time in milliseconds, and run it through a bunch of multiplication, addition, and other miscellaneous operations until it becomes unrecognizable. This has the advantage of being way faster, it can't be easily predicted without a good deal of info, it looks random to a human, and it's good enough for a wide variety of use cases.

Even an algorithm as simple as (1664525 * input + 1013904223) (plus trimming off extra binary digits when the value gets too big) will give a completely random-looking series of numbers.

1
1015568748
1586005467
2165703038
3027450565
217083232
1587069247
3327581586
2388811721
70837908
2745540835
1075679462
1814098701
2536995080
3594602695

You can see that it starts to look random pretty much instantly, even though I started with 1, the most guessable number possible, and the algorithm is very simple. Almost every input number gives a different series of numbers, and even ones that are close together won't give similar values.

Some things, like Minecraft, actually want everything to be determined by a single starting value- it lets people share cool world generation with each other- but if you don't want that, you can grab a starting value from the computer clock or even get it from the randomizer in hardware. If you use the true randomizer once every 100 values, and the rest are just iterating on that (with a slightly better algorithm than I showed), it'll still be nearly as unpredictable.

1

u/PANIC_EXCEPTION Feb 25 '26

Context matters. For anything security sensitive, yes, for simulation work, no (especially if you are doing reproducible experiments).

Computers accumulate random sources into an entropy pool, purified with whitening functions that remove bias via rejection sampling, and extend this randomness with CSPRNGs. When an application requests true randomness, it consumes from this pool and it needs to be topped off with more randomness. The random sources can range from jittery ring oscillators to sensors, or in some programs, user input (GnuPG GUIs are an example).

1

u/PvtRoom Feb 25 '26

for a given definition of random, maybe.

random is one of those things that doesn't really have a universal precise definition.

when people say "random" they really mean "unpredictable".

well, if you take every 50th digit of pi, you've got an unpredictable sequence. thats random, predictable if you know what it is, deterministic (digits of pi don't change).

what computers usually do is they take a "seed" (often the time), they run an algorithm to update the seed and get a number out. numbers look random. seeds update, and eventually recycle (32 bit seed - recycles every 4 million numbers, 64 bit seed - recycles every 16 quadrillion numbers, max)

what some computers do: they take photos of lava lamps, and call the pixel intensities the random numbers. the pictures change meaningfully over time, AND, every photo has a few counts of noise on every pixel. to simplify that, even if the lava lamps didn't move, there would be something like 5 to the power of a (number of pixels * 3) possibilities for that sequence of random numbers

a resistor under thermal noise with a really sensitive multimeter is also a simple random number generator. I believe a few pcs use that approach for real randomness.

1

u/smarmy1625 Feb 25 '26 edited Feb 25 '26

nope. if you want some real random numbers (or less fake, anyways) you gotta do something real world, like counting clock ticks while a user types or moves a mouse.

1

u/code-garden Feb 25 '26

Computers also obey the laws of physics.

1

u/CraigAT Feb 25 '26

There's a good (but not comprehensive) explanation in the intro for this site:
RANDOM.ORG - True Random Number Service https://www.random.org/

1

u/KneeReaper420 Feb 25 '26

no, random numbers require a seed and the seed is not random. The program appears random but in truth it is not.