r/explainlikeimfive • • 5d ago

Technology ELI5: How can you have non-deterministic programs if computers are deterministic machines?

294 Upvotes

373 comments sorted by

View all comments

1

u/EduardoBarreto 4d ago

If a computer wants to use randomness it uses a pseudo-random number generator as the input of the randomness function known as a seed.

A randomness function is one that has crazy variance in its output for any series of seed inputs, like entering 2 outputs 100 then 3 outputs 5 then 4 outputs 31 etc. It's basically unpredictable if you can't predict the seed that was entered.

I just told you that to make randomness you need randomness which is a "Thinking quickly Dave made a megaphone using rope, a squirrel, and a megaphone" moment but it's because sometimes programs want to reuse their randomness seed, particularly often in games. We also make this distinction as a way to abstract the source of randomness away from its usage, if you assume the seed is always truly random then you can safely assume that any function that uses it is also truly random.

And how is the seed generated? A number of factors, sometimes simply using the time of day is enough if you don't need that much security. User input is also a usable source for similarly low security applications, it was often generated in the start screen where you need to press something to start the game and it was often exploited by pressing it in specific moments for speedruns. Then if you really need randomness for security purposes things like electrical noise, hardware interrupts, slight thermal variations are used either by querying all of the sensors and activity visible to the operating system or by leveraging dedicated randomness components in a CPU. And if you need even more randomness you can use sensors and external phenomena such as Cloudflare's camera pointing at the famous lava lamp wall or weather patterns, radioactive decay, and more.