python中uniform randint_python生成随机数:uniform(), randint(), gauss(), expovariate()
1 模塊:random內(nèi)建模塊,偽隨機數(shù)生成器
使用Mersenne Twister的偽隨機數(shù)生成器PRNG進行生成,它以一個確定的數(shù)字作為屬于,并為其生成一個隨機數(shù);為了安全起見,不要用PRNG生成隨機數(shù),要用secrets模塊的真隨機數(shù)TRNG生成;
2 播種隨機數(shù),即用隨機數(shù)種子seed控制隨機數(shù)
>>> import random
## 1、當不指定種子seed時,PRNG每次生成的數(shù)不一樣
>>> print('Random Number 1=>',random.random())
Random Number 1=> 0.21008902332926982
>>> print('Random Number 2=>',random.random())
Random Number 2=> 0.434434837731393
## 2、當指定種子seed時,PRNG每次生成的數(shù)是一樣的,所以稱為偽隨機數(shù)
>>> random.seed(42)
>>> print('Random Number 1=>',random.random())
Random Number 1=> 0.6394267984578837
>>> random.seed(42)
>>> print('Random Number 2=>',random.random())
Random Number 2=> 0.6394267984578837
3 在已知的范圍內(nèi)生成隨機數(shù),例如[2, 5],那就可以random.random()*3 + 2, uniform(2,5), randint(2,5)
##Python學習交流群:778463939
## 1、random.random()*3 + 2
>>> print('Random Number in range(2,8)=>', random.random()*6+2)
Random Number in range(2,8)=> 2.1500645313360014
## 2、uniform():獲取開始值和結(jié)束值作為參數(shù),返回一個浮點型的隨機數(shù)
>>> print('Random Number in range(2,8)=>', random.uniform(2,8))
Random Number in range(2,8)=> 3.6501759102147155
## 3、randint():和uniform相似,不同的是返回值為一個整數(shù)
>>> print('Random Number in range(2,8)=>', random.randint(2,8))
Random Number in range(2,8)=> 3
4 從列表中隨機選擇一個值:choice(), choices()
## 1、choice會從這個列表中隨機選擇一個值
>>> a=[5,9,20,10,2,8]
>>> print('Randomly picked number=>',random.choice(a))
Randomly picked number=> 9
>>> print('Randomly picked number=>',random.choice(a))
Randomly picked number=> 8
>>> print('Randomly picked number=>',random.choice(a))
Randomly picked number=> 5
## 2、choices會從這個列表中隨機選擇多個值(隨機數(shù)的數(shù)量可以超過列表程度)
>>> print('Randomly picked number=>',random.choices(a,k=3))
Randomly picked number=> [5, 20, 5]
>>> print('Randomly picked number=>',random.choices(a,k=3))
Randomly picked number=> [9, 10, 5]
>>> print('Randomly picked number=>',random.choices(a,k=3))
Randomly picked number=> [9, 10, 10]
## 3、choices利用weights將數(shù)組作為權(quán)重傳遞,增加每個值被選取的可能性
>>> print('Randomly picked number=>',random.choices(a,weights=[1,1,1,3,1,1],k=3))
Randomly picked number=> [5, 5, 2]
>>> print('Randomly picked number=>',random.choices(a,weights=[1,1,1,3,1,1],k=3))
Randomly picked number=> [10, 2, 10]
>>> print('Randomly picked number=>',random.choices(a,weights=[1,1,1,3,1,1],k=3))
Randomly picked number=> [10, 8, 10]
5 shuffling改組列表,對列表隨機重排
>>> print('Original list=>',a)
Original list=> [5, 9, 20, 10, 2, 8]
>>> random.shuffle(a)
>>> print('Shuffled list=>',a)
Shuffled list=> [10, 5, 8, 9, 2, 20]
6 根據(jù)概率分布生成隨機數(shù):gauss(), expovariate()
(1)高斯分布gauss()
##Python學習交流群:778463939
>>> import random
>>> import matplotlib.pyplot as plt
>>> temp = []
>>> for i in range(1000):
... temp.append(random.gauss(0,1))
...
>>> plt.hist(temp, bins=30)
>>> plt.show()
(2)變數(shù)分布expovariate():以lambda的值作為參數(shù),lambda為正,則返回從0到正無窮的值;如果lambda為負,則返回從負無窮到0的值
>>> print('Random number from exponential distribution=>',random.expovariate(10))
Random number from exponential distribution=> 0.012164560954097013
>>> print('Random number from exponential distribution=>',random.expovariate(-1))
Random number from exponential distribution=> -0.6461397037921695
(3)伯努利分布
(4)均勻分布
(5)二項分布
(6)正太分布
(7)泊松分布
總結(jié)
以上是生活随笔為你收集整理的python中uniform randint_python生成随机数:uniform(), randint(), gauss(), expovariate()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机械加工工艺师手册_机械加工工艺师——机
- 下一篇: 如何使用python效率办公_日常off