10

I'm learning random module of python. And I know it generates pseudo random number. Which its core idea is to use a high-frequency clock as a seed and then use a function to produce a 'looking-like random number.

As I know it's even impossible to produce authentic random number in the real world.

But I know Unix random producer has introduced some other factors such as the parameter of mouse-movement track, the IO response time, to introduce uncertainty to its random number producer function. Through which we can get a better random number than normal pseudo random number. It's much more harder to predict.

So, is there a way that in python we can produce such a random number, or maybe import a good third party library?

Zen
  • 3,775
  • 4
  • 25
  • 52
  • 1
    Use random.SystemRandom – U2EF1 Apr 06 '14 at 08:36
  • @U2EF1, how to use that function to generate a number? I didn't see too much description about that function on the document. – Zen Apr 06 '14 at 08:48
  • 2
    All of the functions that can be called on `random` can be called on an instance of `random.SystemRandom`. For example, `rng = random.SystemRandom(); x = rng.randint(1, 10)`. – U2EF1 Apr 06 '14 at 08:55
  • One more question, if random.SystemRandom() can generate better random number, why didn't python just use it as the default generator? Because it's slower? – Zen Apr 06 '14 at 09:08
  • 1
    It's much slower, yes. – U2EF1 Apr 06 '14 at 09:15

4 Answers4

11

The documentation for the random module has this to say:

Warning: The pseudo-random generators of this module should not be used for security purposes. Use os.urandom() or SystemRandom if you require a cryptographically secure pseudo-random number generator.

Andrew Gorcester
  • 19,021
  • 6
  • 54
  • 71
  • 2
    So os.urandom() use the random generator of the operating system? What if some guys use windows? – Zen Apr 06 '14 at 08:43
  • 1
    @Zen You should use `random.SystemRandom` for an easy interface that is platform agnostic. That having been said, according to the documentation, `os.urandom()` will use `CryptGenRandom()` on Windows. I don't know what that is, but assuming it's analogous to /dev/random than that's as good as you are likely to get. – Andrew Gorcester Apr 06 '14 at 08:59
  • 1
    The MS documentation says: "The **CryptGenRandom** function fills a buffer with cryptographically random bytes." See http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx – rossum Apr 06 '14 at 14:30
11

Truly random numbers can be generated from

https://pypi.python.org/pypi/quantumrandom/

pip install quantumrandom

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.

CtrlAltF2
  • 1,375
  • 5
  • 16
  • 44
Alexander McFarlane
  • 9,832
  • 8
  • 52
  • 96
3

https://www.random.org/integers/
https://api.random.org/json-rpc/1/

This website generates random numbers through atmospheric white noise, which is better than pseudo random numbers for use with development, You can also use there API for automated random numbers (Though it won't be free for long as it's currently in beta.)

Another method of obtaining true random numbers is through the quantum random number generator, http://photonics.anu.edu.au/qoptics/Research/qrng.php.

To reiterate what someone said earlier, you should avoid using computationally made pseudo random numbers for security purposes.

dgw
  • 485
  • 1
  • 3
  • 18
li x
  • 3,808
  • 2
  • 31
  • 48
2

Python has nothing that allows you to generate "truly random" or "authentic 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. Rather, the requirements for randomness depend on the application, and you didn't really specify what kind of application you have in mind. For example, 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