random.seed(): What does it do?

后端 未结 12 1016
余生分开走
余生分开走 2020-11-22 12:44

I am a bit confused on what random.seed() does in Python. For example, why does the below trials do what they do (consistently)?

>>> i         


        
12条回答
  •  时光说笑
    2020-11-22 13:25

    Set the seed(x) before generating a set of random numbers and use the same seed to generate the same set of random numbers. Useful in case of reproducing the issues.

    >>> from random import *
    >>> seed(20)
    >>> randint(1,100)
    93
    >>> randint(1,100)
    88
    >>> randint(1,100)
    99
    >>> seed(20)
    >>> randint(1,100)
    93
    >>> randint(1,100)
    88
    >>> randint(1,100)
    99
    >>> 
    

提交回复
热议问题