python随机数种子通俗_随机数种子random.seed()理解
總結(jié):
若采用random.random(),每次都按照一定的序列(默認(rèn)的某一個(gè)參數(shù))生成不同的隨機(jī)數(shù)。
若采用隨機(jī)數(shù)種子random.seed(100),它將在所設(shè)置的種子100范圍內(nèi)調(diào)用random()模塊生成隨機(jī)數(shù),如果再次啟動random.seed(100),它則按照之前的序列從頭開始生成隨機(jī)數(shù),兩次生成的隨機(jī)序列相同。
若采用random.seed(),它則按照默認(rèn)的一個(gè)序列生成隨機(jī)數(shù)。
程序演示:
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 7 17:27:28 2019
@author: Mr.King
"""
import random
random.seed(100)
print("----------random.seed(100)-----------------")
print("The first random number: ", random.random())
print("The second random number: ", random.random())
print("The third random number: ", random.random())
random.seed(100)
print("----------再次調(diào)用random.seed(100)----------")
print("The fourth random number: ", random.random())
print("The fifth random number: ", random.random())
random.seed()
print("----------random.seed()--------------------")
print("The sixth random number: ", random.random())
print("The seventh random number: ", random.random())
運(yùn)行結(jié)果:
----------random.seed(100)----------------------
The first random number: 0.1456692551041303
The second random number: 0.45492700451402135
The third random number: 0.7707838056590222
----------再次調(diào)用random.seed(100)----------
The fourth random number: 0.1456692551041303
The fifth random number: 0.45492700451402135
----------random.seed()---------------------------
The sixth random number: 0.20294571682496443
The seventh random number: 0.05551047377535656
總結(jié)
以上是生活随笔為你收集整理的python随机数种子通俗_随机数种子random.seed()理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消防给水及消火栓系统技术规范_2019一
- 下一篇: python 类属性排序_Python实