random.seed(): What does it do?

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

    >>> random.seed(9001)   
    >>> random.randint(1, 10)  
    1     
    >>> random.seed(9001)     
    >>> random.randint(1, 10)    
    1           
    >>> random.seed(9001)          
    >>> random.randint(1, 10)                 
    1                  
    >>> random.seed(9001)         
    >>> random.randint(1, 10)          
    1     
    >>> random.seed(9002)                
    >>> random.randint(1, 10)             
    3
    

    You try this.

    Let's say 'random.seed' gives a value to random value generator ('random.randint()') which generates these values on the basis of this seed. One of the must properties of random numbers is that they should be reproducible. When you put same seed, you get the same pattern of random numbers. This way you are generating them right from the start. You give a different seed- it starts with a different initial (above 3).

    Given a seed, it will generate random numbers between 1 and 10 one after another. So you assume one set of numbers for one seed value.

提交回复
热议问题