日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python随机数生成方法

發布時間:2024/7/5 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python随机数生成方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. random.seed(int)

  • 給隨機數對象一個種子值,用于產生隨機序列。
  • 對于同一個種子值的輸入,之后產生的隨機數序列也一樣。
  • 通常是把時間秒數等變化值作為種子值,達到每次運行產生的隨機系列都不一樣
  • seed() 省略參數,意味著使用當前系統時間生成隨機數
12345678910random.seed(10)print?random.random()???#0.57140259469random.seed(10)print?random.random()???#0.57140259469? 同一個種子值,產生的隨機數相同print?random.random()???#0.428889054675random.seed()???????????#省略參數,意味著取當前系統時間print?random.random()random.seed()print?random.random()

2. random.randint(a,b)

  • 返回指定范圍的一個隨機整數,包含上下限
1print?random.randint(1,10)

3. random.uniform(u,sigma)

  • 隨機正態浮點數
1print?random.uniform(1,5)

4. random.randrange(start,stop,step)

  • 按步長隨機在上下限范圍內取一個隨機數
1print?random.randrange(20,100,5)

5. random.random()

  • 隨機浮點數
1print?random.random()

6. 隨機選擇字符

  • 隨機的選取n個字符
1print?random.sample('abcdefghijk',3)
  • 隨機的選取一個字符
1print?random.choice('abcde./;[fgja13ds2d')
  • 隨機選取幾個字符,再拼接成新的字符串
1print?string.join(random.sample('abcdefhjk',4)).replace(" ","")

7.random.shuffle

  • 對list列表隨機打亂順序,也就是洗牌
  • shuffle只作用于list,對Str會報錯比如‘abcdfed’,而['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

隨機整數:
>>> import random
>>> random.randint(0,99)
21

隨機選取0到100間的偶數:
>>> import random
>>> random.randrange(0, 101, 2)
42

隨機浮點數:
>>> import random
>>> random.random()?
0.85415370477785668
>>> random.uniform(1, 10)
5.4221167969800881

隨機字符:
>>> import random
>>> random.choice('abcdefg&#%^*f')
'd'

多個字符中選取特定數量的字符:
>>> import random
random.sample('abcdefghij',3)?
['a', 'd', 'b']

多個字符中選取特定數量的字符組成新字符串:
>>> import random
>>> import string
>>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
eplace(" ","")
'fih'

隨機選取字符串:
>>> 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模塊來生成隨機數組

1、np.random.rand?用于生成[0.0, 1.0)之間的隨機浮點數, 當沒有參數時,返回一個隨機浮點數,當有一個參數時,返回該參數長度大小的一維隨機浮點數數組,參數建議是整數型,因為未來版本的numpy可能不支持非整形參數。
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])

?

當然該函數還可以用于生成多維數組,這里不做詳述。

2、np.random.randn該函數返回一個樣本,具有標準正態分布。
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])?返回隨機的整數,位于半開區間 [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])?返回隨機的整數,位于閉區間 [low, high]。
>>> np.random.random_integers(5) 4

?

5、np.random.shuffle(x)?類似洗牌,打亂順序;np.random.permutation(x)返回一個隨機排列
>>> 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])
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Python随机数生成方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。