random.seed(): What does it do?

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

    Seed() can be used for later use ---
    
    Example:
    >>> import numpy as np
    >>> np.random.seed(12)
    >>> np.random.rand(4)
    array([0.15416284, 0.7400497 , 0.26331502, 0.53373939])
    >>>
    >>>
    >>> np.random.seed(10)
    >>> np.random.rand(4)
    array([0.77132064, 0.02075195, 0.63364823, 0.74880388])
    >>>
    >>>
    >>> np.random.seed(12) # When you use same seed as before you will get same random output as before
    >>> np.random.rand(4)
    array([0.15416284, 0.7400497 , 0.26331502, 0.53373939])
    >>>
    >>>
    >>> np.random.seed(10)
    >>> np.random.rand(4)
    array([0.77132064, 0.02075195, 0.63364823, 0.74880388])
    >>>
    

提交回复
热议问题