【轉】Python 玩轉隨機數


Python中的random模塊用於生成隨機數。下面介紹一下random模塊中最常用的幾個函數。

random.random

random.random()用於生成一個0到1的隨機符點數: 0 <= n < 1.0

random.uniform

random.uniform的函數原型為:random.uniform(a, b),用於生成一個指定范圍內的隨機符點數,兩個參數其中一個是上限,一個是下限。

如果a > b,則生成的隨機數n: a <= n <= b。如果 a <b, 則 b <= n <= a。

 

 1 print random.uniform(10, 20)  2 print random.uniform(20, 10)  3 
 4 #---- 結果(不同機器上的結果不一樣) 
 5 
 6 #18.7356606526 
 7 #12.5798298022 
 8 
 9 print random.uniform(10, 20) print random.uniform(20, 10) 10 #---- 結果(不同機器上的結果不一樣) #18.7356606526 #12.5798298022

 

 random.randint

random.randint()的函數原型為:random.randint(a, b),用於生成一個指定范圍內的整數。

其中參數a是下限,參數b是上限,生成的隨機數n: a <= n <= b

 

1 print random.randint(12, 20)  #生成的隨機數n: 12 <= n <= 20 
2 
3 print random.randint(20, 20)  #結果永遠是20 
4 
5 #print random.randint(20, 10) #該語句是錯誤的。下限必須小於上限。 
6 
7 print random.randint(12, 20) #生成的隨機數n: 12 <= n <= 20 print random.randint(20, 20) #結果永遠是20 #print random.randint(20, 10) #該語句是錯誤的。下限必須小於上限。

 

random.randrange

random.randrange的函數原型為:random.randrange([start], stop[, step]),從指定范圍內,按指定基數遞增的集合中 獲取一個隨機數。

如:random.randrange(10, 100, 2),結果相當於從[10, 12, 14, 16, ... 96, 98]序列中獲取一個隨機數。

random.randrange(10, 100, 2)在結果上與 random.choice(range(10, 100, 2) 等效。

random.choice

random.choice從序列中獲取一個隨機元素。其函數原型為:random.choice(sequence)。

參數sequence表示一個有序類型。這里要說明 一下:sequence在python不是一種特定的類型,而是泛指一系列的類型。list, tuple, 字符串都屬於sequence。

有關sequence可以查看python手冊數據模型這一章。下面是使用choice的一些例子:

1 print random.choice("學習Python") 2 
3 print random.choice(["JGood", "is", "a", "handsome", "boy"]) 4 
5 print random.choice(("Tuple", "List", "Dict")) 6 
7 print random.choice("學習Python") print random.choice(["JGood", "is", "a", "handsome", "boy"]) print random.choice(("Tuple", "List", "Dict"))

 

random.shuffle

random.shuffle的函數原型為:random.shuffle(x[, random]),用於將一個列表中的元素打亂。如:

 1 p = ["Python", "is", "powerful", "simple", "and so on..."]  2 
 3 random.shuffle(p)  4 
 5 print p  6 
 7 #---- 結果(不同機器上的結果可能不一樣。) 
 8 
 9 #['powerful', 'simple', 'is', 'Python', 'and so on...'] 
10 
11 p = ["Python", "is", "powerful", "simple", "and so on..."] random.shuffle(p) print p #---- 結果(不同機器上的結果可能不一樣。) #['powerful', 'simple', 'is', 'Python', 'and so on...']

 

random.sample

random.sample的函數原型為:random.sample(sequence, k),從指定序列中隨機獲取指定長度的片斷。sample函數不會修改原有序列。

 

1 list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 2 slice = random.sample(list, 5)  #從list中隨機獲取5個元素,作為一個片斷返回 
3 print slice 4 print list #原有序列並沒有改變。 

 

 1 隨機整數:  2 >>> import random  3 >>> random.randint(0,99)  4 21
 5 
 6 隨機選取0到100間的偶數:  7 >>> import random  8 >>> random.randrange(0, 101, 2)  9 42
10 
11 隨機浮點數: 12 >>> import random 13 >>> random.random() 14 0.85415370477785668
15 >>> random.uniform(1, 10) 16 5.4221167969800881
17 
18 隨機字符: 19 >>> import random 20 >>> random.choice('abcdefg&#%^*f') 21 'd'
22 
23 多個字符中選取特定數量的字符: 24 >>> import random 25 random.sample('abcdefghij',3) 26 ['a', 'd', 'b'] 27 
28 多個字符中選取特定數量的字符組成新字符串: 29 >>> import random 30 >>> import string 31 >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r 32 eplace(" ","") 33 'fih'
34 
35 隨機選取字符串: 36 >>> import random 37 >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] ) 38 'lemon'
39 
40 洗牌: 41 >>> import random 42 >>> items = [1, 2, 3, 4, 5, 6] 43 >>> random.shuffle(items) 44 >>> items 45 [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])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM