random.seed(): What does it do?

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

    Here is my understanding. Every time we set a seed value, a "label" or " reference" is generated. The next random.function call is attached to this "label", so next time you call the same seed value and random.function, it will give you the same result.

    np.random.seed( 3 )
    print(np.random.randn()) # output: 1.7886284734303186
    
    np.random.seed( 3 )
    print(np.random.rand()) # different function. output: 0.5507979025745755
    
    np.random.seed( 5 )
    print(np.random.rand()) # different seed value. output: 0.22199317108973948
    

提交回复
热议问题