-1

I recently found that the generation of truly random numbers is an unsolved mystery in Computer Science. Is there a python function that solves this anomaly?

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
Ali Hassan
  • 184
  • 1
  • 10
  • 2
    No. Because true random number should never rely on a code or algorithm. – Sandrin Joy Sep 04 '20 at 18:37
  • The 'secrets" module would be the first step towards "truly random enough". You will always need an entropy-source. – sascha Sep 04 '20 at 18:41
  • https://stackoverflow.com/questions/22891583/can-i-generate-authentic-random-number-with-python – Prune Sep 04 '20 at 18:45
  • 1
    Does this answer your question? [Can I generate authentic random number with python?](https://stackoverflow.com/questions/22891583/can-i-generate-authentic-random-number-with-python) – Gino Mempin Sep 05 '20 at 01:47
  • "Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin." - von Neumann – rossum Sep 05 '20 at 11:33

3 Answers3

3

There are many ways of generating random numbers, with exactly 1 thing in common - they all require external input. Let's say you use a simple RNG like games use. The RNG takes an input (usually the system time, in seconds or milliseconds), and performs various wonky mathematical operations to produce a random-looking output.

Let's say, however, that your computer has hardware that can measure atmospheric noise - you could fairly easily do that with your built-in microphone on any laptop, or an external mic on a desktop... Or you can measure the randomness of the user's input - humans are known to be good sources of entropy... Or you could measure the decay of a subatomic particle - quantum mechanics is as random as it gets.

If you could do any of those things - and you can actually do all of them (#3 requires special hardware) you could pass those through a cryptographic hash (ex. SHA-256) to create a truly random stream of bytes with equal probability for every possible state. If you use SHA-256 it would be a good idea to hash at least 512 bits (64 bytes) of data if you want the most randomness possible.

Also, most modern systems have TRNGs (true random number generators) built into their CPUs; hardware manufacturers started doing this to address the need for better RNGs in cryptography. As such, many systems will default to a TRNG if one is available (using the python secrets module).

You can easily check if you have a TRNG on Linux by running cat /dev/random. If it stops and waits after a few seconds you don't and need to use another technique. If if keeps going the same as /dev/urandom, you have a TRNG already and can make truly-random numbers easily!

Update: The Python secrets module documentation can be found here. A quick example program:

import secrets
low = 10
high = 100
out = secrets.randbelow(high - low) + low # out = random number from range [low, high)
print(out) # Print your number

You can also use secrets to create a hex string directly, or produce a stream of random bytes. You can see its docs to learn more.

DaImTo
  • 88,623
  • 26
  • 153
  • 389
Serpent27
  • 262
  • 1
  • 6
  • pip install quantumrandom I found this on a thread posted above. Currently, you are limited to blocks of 1024 but with a bit of simple programming and a little bit of time, you will be able to extend this limit to a large enough sample for most applications. – Ali Hassan Sep 05 '20 at 12:39
  • This is interesting! – Ali Hassan Sep 16 '20 at 13:05
3

Python has nothing that allows you to generate "truly random" numbers, in the sense that they are uniformly distributed and independent of everything else (especially the latter).

In any case, the distinction between "pseudorandom" and "truly random" numbers is not what applications care about (and you didn't really specify what kind of application you have in mind). Instead, in general:

  • Security applications care whether the numbers are hard to guess; in this case, only a cryptographic RNG can achieve this requirement (even one that relies on a pseudorandom number generator). A Python example is the secrets module or random.SystemRandom.
  • Scientific simulations care whether the numbers behave like independent uniform random numbers, and often care whether the numbers are reproducible at a later time. A Python example is numpy.random.Generator or random.Random.

See also these questions:

Peter O.
  • 30,765
  • 14
  • 76
  • 91
0

There is a thing called true random numbers.
Check: www.random.org for more information.
Code example:

import requests
source = "https://www.random.org/integers/?num=1&min=1&max=100&col=5&base=10&format=plain&rnd=new"
number = requests.get(source)
number = int(number.text)