random.seed(): What does it do?

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

    All the other answers don't seem to explain the use of random.seed(). Here is a simple example (source):

    import random
    random.seed( 3 )
    print "Random number with seed 3 : ", random.random() #will generate a random number 
    #if you want to use the same random number once again in your program
    random.seed( 3 )
    random.random()   # same random number as before
    

提交回复
热议问题