Python随机数生成方法
1. random.seed(int)
- 給隨機(jī)數(shù)對(duì)象一個(gè)種子值,用于產(chǎn)生隨機(jī)序列。
- 對(duì)于同一個(gè)種子值的輸入,之后產(chǎn)生的隨機(jī)數(shù)序列也一樣。
- 通常是把時(shí)間秒數(shù)等變化值作為種子值,達(dá)到每次運(yùn)行產(chǎn)生的隨機(jī)系列都不一樣
- seed() 省略參數(shù),意味著使用當(dāng)前系統(tǒng)時(shí)間生成隨機(jī)數(shù)
| 12345678910 | random.seed(10)print?random.random()???#0.57140259469random.seed(10)print?random.random()???#0.57140259469? 同一個(gè)種子值,產(chǎn)生的隨機(jī)數(shù)相同print?random.random()???#0.428889054675random.seed()???????????#省略參數(shù),意味著取當(dāng)前系統(tǒng)時(shí)間print?random.random()random.seed()print?random.random() |
2. random.randint(a,b)
- 返回指定范圍的一個(gè)隨機(jī)整數(shù),包含上下限
| 1 | print?random.randint(1,10) |
3. random.uniform(u,sigma)
- 隨機(jī)正態(tài)浮點(diǎn)數(shù)
| 1 | print?random.uniform(1,5) |
4. random.randrange(start,stop,step)
- 按步長(zhǎng)隨機(jī)在上下限范圍內(nèi)取一個(gè)隨機(jī)數(shù)
| 1 | print?random.randrange(20,100,5) |
5. random.random()
- 隨機(jī)浮點(diǎn)數(shù)
| 1 | print?random.random() |
6. 隨機(jī)選擇字符
- 隨機(jī)的選取n個(gè)字符
| 1 | print?random.sample('abcdefghijk',3) |
- 隨機(jī)的選取一個(gè)字符
| 1 | print?random.choice('abcde./;[fgja13ds2d') |
- 隨機(jī)選取幾個(gè)字符,再拼接成新的字符串
| 1 | print?string.join(random.sample('abcdefhjk',4)).replace(" ","") |
7.random.shuffle
- 對(duì)list列表隨機(jī)打亂順序,也就是洗牌
- shuffle只作用于list,對(duì)Str會(huì)報(bào)錯(cuò)比如‘a(chǎn)bcdfed’,而['1','2','3','5','6','7']可以
123456789 | item=[1,2,3,4,5,6,7]print?itemrandom.shuffle(item)print?itemitem2=['1','2','3','5','6','7']print?item2random.shuffle(item2)print?item2 |
隨機(jī)整數(shù):
>>> import random
>>> random.randint(0,99)
21
隨機(jī)選取0到100間的偶數(shù):
>>> import random
>>> random.randrange(0, 101, 2)
42
隨機(jī)浮點(diǎn)數(shù):
>>> import random
>>> random.random()?
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881
隨機(jī)字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'
多個(gè)字符中選取特定數(shù)量的字符:
>>> import random
random.sample('abcdefghij',3)?
['a', 'd', 'b']
多個(gè)字符中選取特定數(shù)量的字符組成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'
隨機(jī)選取字符串:
>>> import random
>>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
'lemon'
洗牌:
>>> import random
>>> items = [1, 2, 3, 4, 5, 6]
>>> random.shuffle(items)
>>> items
[3, 2, 5, 6, 4, 1]
二、使用numpy.random模塊來(lái)生成隨機(jī)數(shù)組
1、np.random.rand?用于生成[0.0, 1.0)之間的隨機(jī)浮點(diǎn)數(shù), 當(dāng)沒(méi)有參數(shù)時(shí),返回一個(gè)隨機(jī)浮點(diǎn)數(shù),當(dāng)有一個(gè)參數(shù)時(shí),返回該參數(shù)長(zhǎng)度大小的一維隨機(jī)浮點(diǎn)數(shù)數(shù)組,參數(shù)建議是整數(shù)型,因?yàn)槲磥?lái)版本的numpy可能不支持非整形參數(shù)。
1 import numpy as np 2 >>> np.random.rand(10) 3 array([ 0.56911206, 0.99777291, 0.18943144, 0.19387287, 0.75090637, 4 0.18692814, 0.69804514, 0.48808425, 0.79440667, 0.66959075])?
當(dāng)然該函數(shù)還可以用于生成多維數(shù)組,這里不做詳述。
2、np.random.randn該函數(shù)返回一個(gè)樣本,具有標(biāo)準(zhǔn)正態(tài)分布。
1 >>> np.random.randn(10) 2 array([-1.6765704 , 0.66361856, 0.04029481, 1.19965741, -0.57514593, 3 -0.79603968, 1.52261545, -2.17401814, 0.86671727, -1.17945975])?
3、np.random.randint(low[, high, size])?返回隨機(jī)的整數(shù),位于半開(kāi)區(qū)間 [low, high)。
>>> np.random.randint(10,size=10) array([4, 1, 4, 3, 8, 2, 8, 5, 8, 9])?
4、random_integers(low[, high, size])?返回隨機(jī)的整數(shù),位于閉區(qū)間 [low, high]。
>>> np.random.random_integers(5) 4?
5、np.random.shuffle(x)?類(lèi)似洗牌,打亂順序;np.random.permutation(x)返回一個(gè)隨機(jī)排列
>>> arr = np.arange(10) >>> np.random.shuffle(arr) >>> arr [1 7 5 2 9 4 3 6 0 8]>>>> np.random.permutation(10) array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的Python随机数生成方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 机器学习简介及学习思维导图
- 下一篇: Python获取磁盘使用信息,pytho