16

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
  • 4,381
  • 5
  • 29
  • 56
  • 2
    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
  • 3
    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
  • what i want is that every time I run python the seed I generate is different. How do I do this? – Charlie Parker Feb 01 '23 at 21:02
  • related: https://stackoverflow.com/questions/63746362/generate-true-random-numbers-in-python – Charlie Parker Feb 01 '23 at 21:02

5 Answers5

14

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.

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
12

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,595
  • 7
  • 57
  • 73
  • 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
  • what i want is that every time I run python the seed I generate is different. How do I do this? – Charlie Parker Feb 01 '23 at 21:02
  • @CharlieParker The default is that the seed is different every time you run python. You would only have to follow special instructions if you wanted the opposite of that. – Andrew Gorcester Feb 02 '23 at 01:23
2

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
  • 4
  • 18
li x
  • 3,953
  • 2
  • 31
  • 51
1

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.
  • 32,158
  • 14
  • 82
  • 96
1

I think this works:

def get_truly_random_seed_through_os() -> int:
    """
    Usually the best random sample you could get in any programming language is generated through the operating system. 
    In Python, you can use the os module.

    source: https://stackoverflow.com/questions/57416925/best-practices-for-generating-a-random-seeds-to-seed-pytorch/57416967#57416967
    """
    RAND_SIZE = 4
    random_data = os.urandom(
        RAND_SIZE
    )  # Return a string of size random bytes suitable for cryptographic use.
    random_seed: int = int.from_bytes(random_data, byteorder="big")
    return int(random_seed)

if you only want different seeds you can do:

def get_different_pseudo_random_seed_every_time_using_time() -> int:
    import random
    import time

    # random.seed(int(time.time()))
    seed: int = int(time.time())
    return seed
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323