参数:
————————————————————————————————————
seed:若seed为None,返回RandomState singleton(np.random)。
若seed 为int,返回一个新的randomState实例,由这个新的seed产生。
若seed为randomState实例则直接返回它本身。
流程
————————————————————————————————————
def check_random_state(seed):
"""Turn seed into a np.random.RandomState instance
If seed is None, return the RandomState singleton used by np.random.
If seed is an int, return a new RandomState instance seeded with seed.
If seed is already a RandomState instance, return it.
Otherwise raise ValueError.
"""
#seed为None或者为np.random则返回np.random.mtrand._rand 。
if seed is None or seed is np.random:
return np.random.mtrand._rand
#seed为numbers.Intergral或np.integer实例。
if isinstance(seed, (numbers.Integral, np.integer)):
return np.random.RandomState(seed)
#若为np.random.RandomState则返回自身。
if isinstance(seed, np.random.RandomState):
return seed
#若不为上述情况则报错。
raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
' instance' % seed)