Python內置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<stop。

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]<br>  [0.52184832 0.41466194]<br>  [0.26455561 0.77423369]]]<br>"""

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學習基地,看前輩們是如何學習的!從基礎的python腳本、爬蟲、django、數據挖掘等編程技術,還有整理零基礎到項目實戰的資料,送給每一位愛學習python的小夥伴!每天都有老前輩定時講解Python技術,分享一些學習的方法和需要留意的小細節,點擊加入我們的 python學習者聚集地

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章