概述
該模塊為多種分布實現(xiàn)偽隨機(jī)數(shù)生成器。
對于整數(shù),在某范圍內(nèi)均勻選擇。 對于序列,對隨機(jī)元素均勻選擇,通過產(chǎn)生隨機(jī)排列就地生成列表以及用于隨機(jī)抽取而不進(jìn)行替換的功能。
Bookkeeping 函數(shù)
random.seed(a=None, version=2)
初始化隨機(jī)數(shù)字產(chǎn)生器
如果省略a或None,則使用當(dāng)前系統(tǒng)時間。 如果操作系統(tǒng)提供隨機(jī)源,則使用它們而不是系統(tǒng)時間(有關(guān)可用性的詳細(xì)信息,請參閱os.urandom()函數(shù))。
如果a是int, 則直接使用
使用版本2(默認(rèn)值),str,bytes或bytearray對象將被轉(zhuǎn)換為int,并使用其所有位。
使用版本1(提供用于從舊版本的Python重現(xiàn)隨機(jī)序列),str和字節(jié)的算法產(chǎn)生較窄的種子范圍。
random.getstate()
返回捕獲當(dāng)前生成器內(nèi)部狀態(tài)的對象。這個對象可以通過setstate()恢復(fù)狀態(tài)
random.setstate(state)
狀態(tài)應(yīng)該是從之前的函數(shù)調(diào)用getstate()獲得的,而setstate()將生成器的內(nèi)部狀態(tài)恢復(fù)到當(dāng)時調(diào)用getstate()的狀態(tài)。
Functions for integers
random.randrange(stop) / random.randrange(start, stop, [, step])
返回一個從range(start, stop, step)隨機(jī)抽取的元素. 這個相當(dāng)于choic(range(start, stop, step)), 但實際上并不構(gòu)建range對象
位置參數(shù)與range()函數(shù)相匹配. 不應(yīng)該使用關(guān)鍵詞參數(shù),因為函數(shù)可能會以不尋常的方式使用他們
random.randint(a, b)
返回一個隨機(jī)的數(shù)字N, a <= N <= b. 函數(shù)相當(dāng)于randrange(a, b + 1)
Functions for sequences
random.choice(seq)
從非空序列seq返回隨機(jī)元素。 如果seq為空,則引發(fā)IndexError。
random.shuffle(x[, random])
隨機(jī)打亂序列x, 會改變x本身.
random.sample(population, k)
返回從序列或集合中選擇的K長度的list, list中元素唯一。 用于隨機(jī)抽樣,但不會替換本身
返回一個新列表,其中包含來自population的元素,同時保持原始population不變。 所得到的列表處于選擇順序,使得所有子片段也將是有效的隨機(jī)樣本。 這允許抽獎優(yōu)勝者(樣本)被劃分為大獎和第二名獲獎?wù)撸ㄏ录墸?br> population不需要是可散列的或獨特的。 如果population包含重復(fù)元素,則樣本中每一個元素都可能會出現(xiàn)。
要從整數(shù)范圍中選擇一個樣本,請使用range()對象作為參數(shù)。 這對于從很大的population進(jìn)行抽樣來說,特別快速,空間有效:sample(range(1000000), 60)
如果樣本大小大于總體大小,則會引發(fā)ValueError。
分布函數(shù)
函數(shù)參數(shù)以分布方程中的相應(yīng)變量命名,如通用數(shù)學(xué)實踐中所使用的; 大多數(shù)這些方程可以在任何統(tǒng)計文本中找到。
random.random()
返回范圍[0.0,1.0)中的下一個隨機(jī)浮點數(shù)。
random.uniform(a, b)
返回一個隨機(jī)浮點數(shù)N,若a <= b, 則返回的N會a <= N <= b, 若b < a, b <= N <= a
根據(jù)等式a +(b-a)* random()中的浮點舍入,終點值b可以包括也可以不包括在范圍內(nèi)。
還有一些分布函數(shù)沒有整理出來,如果需要可以查閱文檔.
應(yīng)用
基本應(yīng)用
>>> random.random() # Random float x, 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10) # Random float x, 1.0 <= x < 10.0
1.1800146073117523
>>> random.randrange(10) # Integer from 0 to 9
7
>>> random.randrange(0, 101, 2) # Even integer from 0 to 100
26
>>> random.choice('abcdefghij') # Single random element
'c'
>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]
>>> random.sample([1, 2, 3, 4, 5], 3) # Three samples without replacement
[4, 1, 5]
進(jìn)階
一個常見的任務(wù)是產(chǎn)生一個帶加權(quán)比率的random.choice()
如果權(quán)重是小的整數(shù)比率,一個簡單的技術(shù)是建立重復(fù)的樣本population:
>>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
>>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
>>> population
['Red', 'Red', 'Red', 'Blue', 'Blue', 'Yellow', 'Green', 'Green', 'Green', 'Green']
>>> random.choice(population)
'Red'
更一般的方法是使用itertools.accumulate()權(quán)重排列在累積分布中,然后使用bisect.bisect()定位隨機(jī)值:
>>> choices, weights = zip(*weighted_choices)
>>> choices
('Red', 'Blue', 'Yellow', 'Green')
>>> weights
(3, 2, 1, 4)
>>> import itertools
>>> cumdist = list(itertools.accumulate(weights))
>>> cumdist
[3, 5, 6, 10]
>>> x = random.random() * cumdist[-1]
>>> x
7.904737262461592
>>> import bisect
>>> choices[bisect.bisect(cumdist, x)]
'Green'
后記
目前對于Python隨機(jī)用的不多,不過既然用到了,我還是整理了一下官方的文檔,加油.