DEV Community

Cover image for Are Random numbers really random?

Are Random numbers really random?

Samyak Jain on January 22, 2024

So recently a thought struck me that we use random functions in our software all the time right? Either in games or in machine learning models, cry...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

Truly random number generators do not exist, since true randomness is only a theoretical concept.

Collapse
 
samyak112 profile image
Samyak Jain

Well True Random generators do exist, here is a site which let's you generate True Random numbers - random.org/

They have also explained how they are generating these true randoms

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

The fact that they can explain their method makes them not random. For something to be truly random, it would depend on nothing, and be 'chosen without method'. It's a purely theoretical concept.

Thread Thread
 
samyak112 profile image
Samyak Jain

Doesn't truly random mean that you can't predict it? What does it have to do with "chosen method"? I mean they are relying on environment and I think that's random enough right?

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

With enough information, you could predict that. That's the point.

You said it correctly with your phrasing 'random enough' - it's probably beyond the practical reach of anyone to predict it... but that doesn't make it truly random.

Thread Thread
 
tandrieu profile image
Thibaut Andrieu

random.org seems based on "atmospheric noise" as they said. I didn't check the details, but this seems chaotic enough to make the random sequence be unpredictable in practice.

There is also qrandom.io/, which seems to generate random values based on quantum random generator. These generators are truly unpredictable, even if you know the exact state of the whole universe.

Collapse
 
florianrappl profile image
Florian Rappl

Unfortunately the assumption that all random functions somewhere are PRNGs is wrong. We have true random number generator built into hardware. Since these are usually quite slow they mostly serve as seed input for a PRNGs.

Most likely of your machine can also produce real random numbers using the RDRAND instruction.

Collapse
 
samyak112 profile image
Samyak Jain

Yeah, I agree and that's why I only mentioned the programming language's random functions, although am curious how does hardware generates this random number? Atmospheric noise? I thought for PRNGs we normally use timestamp.

Collapse
 
florianrappl profile image
Florian Rappl

That depends on the hardware RNG - it can be thermal-based / brownian motion, but atmospheric noise or general current fluctuations are also a viable source. There are also more sophisticated methods. In general such hardware RNGs don't give better performance (as mentioned they are quite slow) and are also not used in applications where randomness is crucial (the classic MT19937 is still sufficiently good). They are good, however, for cryptographic applications under certain circumstances (e.g., OpenSSL allows you to use them).

If you refer to timestamps for the seed of a PRNG that can be true (in cases no seed has been given), but then again it fully depends on the framework and usage. Consider /dev/random - they use a mix of different sources incl. RDRAND (and similar like RDSEED) if available. Exceptions (based on the used distro) exist.

Collapse
 
iamspathan profile image
Sohail Pathan

Wow.. I remember using Random.nextInt() for testing against a test case. I was curious but was too lazy to know what goes underneath. Now, I understand the underlying mechanism.

Collapse
 
samyak112 profile image
Samyak Jain

Glad I was able to help you understand something 😁

Collapse
 
sreno77 profile image
Scott Reno

TLDR; truly random numbers aren't feasible but we do have some ways to create numbers that are "random enough" for most purposes.

Collapse
 
samyak112 profile image
Samyak Jain

Haha, that does sum up the blog

Collapse
 
samyak112 profile image
Samyak Jain

Agreed 💯

Collapse
 
muhammadowaiswarsi profile image
Muhammad Owais Warsi

It was great

Collapse
 
tandrieu profile image
Thibaut Andrieu

Have you ever played a game that allowed you to input a 'seed' for generating the game world?

Ho, yeah, the good old Rapid Racer on PS1 and its fractal generator !

Collapse
 
samyak112 profile image
Samyak Jain

Haha never played it but based on your given link it does looks cool, reminds me of boats in GTA vice City

Collapse
 
rechie_kho profile image
Techie Kho

Be like Cloudfare, record bunch of lava lamps and hash the image into numbers.