random.seed(): What does it do?

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

    Here is a small test that demonstrates that feeding the seed() method with the same argument will cause the same pseudo-random result:

    # testing random.seed()
    
    import random
    
    def equalityCheck(l):
        state=None
        x=l[0]
        for i in l:
            if i!=x:
                state=False
                break
            else:
                state=True
        return state
    
    
    l=[]
    
    for i in range(1000):
        random.seed(10)
        l.append(random.random())
    
    print "All elements in l are equal?",equalityCheck(l)
    

提交回复
热议问题