Python中的random模塊用于生成隨機(jī)數(shù)。下面介紹一下random模塊中最常用的幾個(gè)函數(shù)(前8個(gè)最常用):
import random
(1)random.expovariate(lambd)指數(shù)分布
指數(shù)分布。lambd=1.0/期望的平均值。它應(yīng)該是非零的。(該參數(shù)被稱為“l(fā)ambda”,但這在Python中是一個(gè)保留字。).如果lambd為正,返回的值范圍從0到正無窮;如果lambd為負(fù),返回的值范圍從負(fù)無窮到0。如,顧客到達(dá)時(shí)間服從指數(shù)分布,時(shí)間間隔平均為10分鐘,則lambd=1.0/10。
(2)random.random
用于生成一個(gè)0到1的隨機(jī)符點(diǎn)數(shù): 0 <= n < 1.0
(3)random.uniform
用于生成一個(gè)指定范圍內(nèi)的隨機(jī)符點(diǎn)數(shù),兩個(gè)參數(shù)其中一個(gè)是上限,一個(gè)是下限。如果a > b,則生成的隨機(jī)數(shù)n: a <= n <= b。如果 a?<b, 則 b <= n <= a。
(4)random.randint
用于生成一個(gè)指定范圍內(nèi)的整數(shù)。其中參數(shù)a是下限,參數(shù)b是上限,生成的隨機(jī)數(shù)n: a <= n <= b。size是表示生成總共多少個(gè)數(shù)。若size=2,則表示該數(shù)組里有2個(gè)元素。
(5)random.randrange
從指定范圍內(nèi),按指定基數(shù)遞增的集合中 獲取一個(gè)隨機(jī)數(shù)。如:random.randrange(10, 100, 2),結(jié)果相當(dāng)于從[10, 12, 14, 16, ... 96, 98]序列中獲取一個(gè)隨機(jī)數(shù)。
(6)random.choice
從序列中獲取一個(gè)隨機(jī)元素。參數(shù)sequence表示一個(gè)有序類型。這里要說明 一下:sequence在python不是一種特定的類型,而是泛指一系列的類型。list, tuple, 字符串都屬于sequence。
(7)random.shuffle
用于將一個(gè)列表中的元素打亂。
(8)random.sample(sequence, k),從指定序列中隨機(jī)獲取指定長(zhǎng)度的片斷。sample函數(shù)不會(huì)修改原有序列。這個(gè)模塊很 "變態(tài)",還支持三角、β分布、指數(shù)分布、伽馬分布、高斯分布等等非常專業(yè)的隨機(jī)算法。
(9)random.triangular(low, high, mode)
Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds. The low and highbounds default to zero and one. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.
(10)random.betavariate(alpha, beta)β分布
Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.
(11)random.seed([x])
偽隨機(jī)數(shù)生成模塊。如果不提供 seed,默認(rèn)使用系統(tǒng)時(shí)間。使用相同的 seed,可以獲得完全相同的隨機(jī)數(shù)序列,常用于算法改進(jìn)測(cè)試。
>>>from random import *
>>>a = Random(); a.seed(1)
>>>[a.randint(1, 100) for i in range(20)]
[14, 85, 77, 26, 50, 45, 66, 79, 10, 3, 84, 44, 77, 1, 45, 73, 23, 95, 91, 4]
>>>b = Random(); b.seed(1)
>>>[b.randint(1, 100) for i in range(20)]
[14, 85, 77, 26, 50, 45, 66, 79, 10, 3, 84, 44, 77, 1, 45, 73, 23, 95, 91, 4]
(12)random.gammavariate(alpha, beta)伽馬分布
Gamma distribution. (Not the gamma function!) Conditions on the parameters are alpha > 0 and beta > 0.
(13)random.gauss(mu, sigma)高斯分布
Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function defined below.
(14)random.lognormvariate(mu, sigma)對(duì)數(shù)正態(tài)分布
Log normal distribution. If you take the natural logarithm of this distribution, you’ll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.
(15)random.normalvariate(mu, sigma)正態(tài)分布
normalvariate是正態(tài)分布。mu是均值,sigma是標(biāo)準(zhǔn)差。
random.vonmisesvariate(mu, kappa)
mu是平均角度,表示在0到2*pi之間的弧度,kappa是濃度參數(shù),必須大于等于0。如果kappa等于0,這個(gè)分布會(huì)在0到2*pi之間減小為均勻的隨機(jī)角度。
(16)random.paretovariate(alpha)帕累托分布
Pareto distribution. alpha is the shape parameter.
(17)random.weibullvariate(alpha, beta)
威布爾分布。alpha是尺度參數(shù),beta是形狀參數(shù)。
#轉(zhuǎn)自 numpy 隨機(jī)數(shù)random/隨機(jī)漫步介紹