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

歡迎訪問 生活随笔!

生活随笔

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

python

python产生随机数random.random_Python内置random模块生成随机数的方法

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

本文我們詳細地介紹下兩個模塊關于生成隨機序列的其他使用方法。

隨機數參與的應用場景大家一定不會陌生,比如密碼加鹽時會在原密碼上關聯一串隨機數,蒙特卡洛算法會通過隨機數采樣等等。Python內置的random模塊提供了生成隨機數的方法,使用這些方法時需要導入random模塊。

import random

下面介紹下Python內置的random模塊的幾種生成隨機數的方法。

1、random.random()隨機生成 0 到 1 之間的浮點數[0.0, 1.0)。注意的是返回的隨機數可能會是 0 但不可能為 1,即左閉右開的區間。

print("random: ", random.random())

#random: 0.5714025946899135

2、random.randint(a , b)隨機生成 a 與 b 之間的整數[a, b],a<=n<=b,隨機整數不包含 b 時[a, b)可以使用 random.randrange() 方法。

print("randint: ", random.randint(6,8))

#randint: 8

3、random.randrange(start,stop,step)按步長step隨機在上下限范圍內取一個隨機數,start<=n

print("randrange: ",random.randrange(20,100,5))

#randrange: 85

4、random.uniform(a, b)隨機生成 a 與 b 之間的浮點數[a, b],a<=n<=b。

print("uniform: ",random.uniform(5,10))

#uniform: 5.119790163375776

5、random.choice()從列表中隨機取出一個元素,比如列表、元祖、字符串等。注意的是,該方法需要參數非空,否則會拋出 IndexError 的錯誤。

print("choice: ",random.choice("www.yuanxiao.net"))

#choice: y

6、random.shuffle(items) 把列表 items 中的元素隨機打亂。注意的是,如果不想修改原來的列表,可以使用 copy 模塊先拷貝一份原來的列表。

num = [1, 2, 3, 4, 5]

random.shuffle(num)

print("shuffle: ",num)

#shuffle: [1, 3, 5, 4, 2]

7、random.sample(items, n)從列表 items 中隨機取出 n 個元素。

num = [1, 2, 3, 4, 5]

print("sample: ",random.sample(num, 3))

#sample: [4, 1, 5]

Python 的random模塊產生的隨機數其實是偽隨機數,依賴于特殊算法和指定不確定因素(種子seed)來實現。如randint方法生成一定范圍內的隨機數,會先指定一個特定的seed,將seed通過特定的隨機數產生算法,得到一定范圍內隨機分布的隨機數。因此對于同一個seed值的輸入產生的隨機數會相同,省略參數則意味著使用當前系統時間秒數作為種子值,達到每次運行產生的隨機數都不一樣。

random.seed(2)

print("random: ", random.random())

#random: 0.9560342718892494

random.seed(3)

print("random: ", random.random())

#random: 0.23796462709189137

random.seed(3)#同一個種子值,產生的隨機數相同

print("random: ", random.random())

#random: 0.23796462709189137

numpy庫也提供了random模塊,用于生成多維度數組形式的隨機數。使用時需要導入numpy庫。

import numpy as np

下面介紹下numpy庫的random模塊的幾種生成隨機數的方法。

1、numpy.random.rand(d0,d1,…,dn)

rand函數根據給定維度生成[0,1]之間的數據,包含0,不包含1

dn表格每個維度

返回值為指定維度的array

print("np.random.rand:\n {}".format(np.random.rand(4,2)))

# shape: 4*3

"""

np.random.rand:

[[0.5488135 0.71518937]

[0.60276338 0.54488318]

[0.4236548 0.64589411]

[0.43758721 0.891773 ]]

"""

print("np.random.rand:\n {}".format(np.random.rand(4,3,2)))

# shape: 4*3*2

"""

np.random.rand:

[[[0.96366276 0.38344152]

[0.79172504 0.52889492]

[0.56804456 0.92559664]]

[[0.07103606 0.0871293 ]

[0.0202184 0.83261985]

[0.77815675 0.87001215]]

[[0.97861834 0.79915856]

[0.46147936 0.78052918]

[0.11827443 0.63992102]]

[[0.14335329 0.94466892]

[0.52184832 0.41466194]

[0.26455561 0.77423369]]]

"""

2、numpy.random.randn(d0,d1,…,dn)

randn函數返回一個或一組樣本,具有標準正態分布。

dn表格每個維度

返回值為指定維度的array

標準正態分布—-standard normal distribution

標準正態分布又稱為u分布,是以0為均值、以1為標準差的正態分布,記為N(0,1)。

print("np.random.randn:\n {}".format(np.random.randn()))

# 當沒有參數時,返回單個數據

"""

np.random.randn:

2.2697546239876076

"""

print("np.random.randn:\n {}".format(np.random.randn(2,4)))

"""

np.random.randn:

[[-1.45436567 0.04575852 -0.18718385 1.53277921]

[ 1.46935877 0.15494743 0.37816252 -0.88778575]]

"""

print("np.random.randn:\n {}".format(np.random.randn(4,3,2)))

"""

np.random.randn:

[[[-1.98079647 -0.34791215]

[ 0.15634897 1.23029068]

[ 1.20237985 -0.38732682]]

[[-0.30230275 -1.04855297]

[-1.42001794 -1.70627019]

[ 1.9507754 -0.50965218]]

[[-0.4380743 -1.25279536]

[ 0.77749036 -1.61389785]

[-0.21274028 -0.89546656]]

[[ 0.3869025 -0.51080514]

[-1.18063218 -0.02818223]

[ 0.42833187 0.06651722]]]

"""

3、numpy.random.randint(low, high=None, size=None, dtype='l')

返回隨機整數,范圍區間為[low,high),包含low,不包含high

參數:low為最小值,high為最大值,size為數組維度大小,dtype為數據類型,默認的數據類型是np.int

high沒有填寫時,默認生成隨機數的范圍是[0,low]

print("np.random.randint:\n {}".format(np.random.randint(1,size=5)))

# 返回[0,1)之間的整數,所以只有0

"""

np.random.randint:

[0 0 0 0 0]

"""

print("np.random.randint:\n {}".format(np.random.randint(1,5)))# 返回1個[1,5)時間的隨機整數

"""

np.random.randint:

2

"""

print("np.random.randint:\n {}".format(np.random.randint(-5,5,size=(2,2))))

"""

np.random.randint:

[[-5 -3]

[ 2 -3]]

"""

4、numpy.random.seed()

np.random.seed()的作用:使得隨機數據可預測。

當我們設置相同的seed,每次生成的隨機數相同。如果不設置seed,則每次會生成不同的隨機數

總結

以上所述是小編給大家介紹的Python內置random模塊生成隨機數的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網站的支持!

如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

本文標題: Python內置random模塊生成隨機數的方法

本文地址: http://www.cppcns.com/jiaoben/python/261243.html

總結

以上是生活随笔為你收集整理的python产生随机数random.random_Python内置random模块生成随机数的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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