random.seed(): What does it do?

后端 未结 12 854
余生分开走
余生分开走 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:43

    Imho, it is used to generate same random course result when you use random.seed(samedigit) again.

    In [47]: random.randint(7,10)
    
    Out[47]: 9
    
    
    In [48]: random.randint(7,10)
    
    Out[48]: 9
    
    
    In [49]: random.randint(7,10)
    
    Out[49]: 7
    
    
    In [50]: random.randint(7,10)
    
    Out[50]: 10
    
    
    In [51]: random.seed(5)
    
    
    In [52]: random.randint(7,10)
    
    Out[52]: 9
    
    
    In [53]: random.seed(5)
    
    
    In [54]: random.randint(7,10)
    
    Out[54]: 9
    

提交回复
热议问题