Python's random: What happens if I don't use seed(someValue)?

前端 未结 3 1721
迷失自我
迷失自我 2020-12-06 16:30

a)In this case does the random number generator uses the system\'s clock (making the seed change) on each run?

b)Is the seed used to generate the pseudo-random valu

相关标签:
3条回答
  • 2020-12-06 16:49

    "Use the Source, Luke!"...;-). Studying https://svn.python.org/projects/python/trunk/Lib/random.py will rapidly reassure you;-).

    What happens when seed isn't set (that's the "i is None" case):

    if a is None:
        try:
            a = long(_hexlify(_urandom(16)), 16)
        except NotImplementedError:
            import time
            a = long(time.time() * 256) # use fractional seconds
    

    and the expovariate:

    random = self.random
    u = random()
    while u <= 1e-7:
        u = random()
    return -_log(u)/lambd
    

    obviously uses the same underlying random generator as every other method, and so is identically affected by the seeding or lack thereof (really, how else would it have been done?-)

    0 讨论(0)
  • 2020-12-06 17:08

    a) It typically uses the system clock, the clock on some systems may only have ms precision and so seed twice very quickly may result in the same value.

    seed(self, a=None) Initialize internal state from hashable object.

    None or no argument seeds from current time or from an operating
    system specific randomness source if available.
    

    http://pydoc.org/2.5.1/random.html#Random-seed

    b) I would imagine expovariate does, but I can't find any proof. It would be silly if it didn't.

    0 讨论(0)
  • 2020-12-06 17:11

    current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

    Random Docs

    0 讨论(0)
提交回复
热议问题