前言
① random 庫是使用隨機數的Python標准庫。
②偽隨機數:采用梅森旋轉算法生成的隨機序列中元素。
(隨機數是隨機產生的數據(比如拋硬幣),但是計算機是不可能產生隨機值,真正的隨機數也是在特定條件下產生的確定值,計算機不能產生真正的隨機數,那么偽隨機數也就被稱為隨機數。)
③導入:import random
一、基本隨機數
Python中產生隨機數使用隨機數種子來產生【只要種子相同,產生的隨機序列,無論是每一個數,還是數與數之間的關系都是確定的,所以隨機數種子確定了隨機序列的產生】
random.seed(a=None)
設置隨機種子數,可以是浮點數或整數,如果不設置的話,則random庫默認以系統時間產生當作隨機數種子,設置種子的好處是可以重復再現相同的隨機數序列
二、返回整數
random.randrange
語法格式
兩種寫法
random.randrange(stop)
random.randrange(start, stop[, step])
- start:起始數字,包含(取得到 start 這個值)
- stop:末尾數字,不包含(取不到 stop 這個值)
- step:步長
例如:
# 栗子一 for i in range(5): print(random.randrange(20)) #### 17 4 7 7 4 # 栗子二 for i in range(5): print(random.randrange(10, 20)) #### 13 14 11 17 17 # 栗子三 for i in range(5): print(random.randrange(10, 20, 2)) #### 12 12 14 14 10
random.randint
語法格式
random.randint(a, b)
- 返回隨機整數 N 滿足 a <= N <= b
- 相當於 random.randrange(a, b+1)
- 【a、b 都可以取得到值】
例如:
for i in range(5): print(random.randint(0,20)) #### 19 20 11 6 3
三、返回浮點數
random.random()
語法格式
random.random()
- 返回 [0.0, 1.0) 范圍內的下一個隨機浮點數
例如:
# 栗子一 for i in range(5): print(random.random()) #### 0.9829492243165335 0.43473506430105724 0.5198709187243076 0.6437884305820736 0.7216771961168909 # 栗子二 for i in range(5): print(math.ceil(random.random() * 1000)) #### 772 352 321 62 127
random.uniform(a, b)
語法格式
random.uniform(a, b)
- 返回一個隨機浮點數 N
- 當
a <= b時,a <= N <= b - 當
b <= a時,b <= N <= a
例如:
# 栗子一 for i in range(5): print(random.uniform(1, 10)) #### 2.6200262089754593 9.220506911469235 3.0206896704014783 9.670905330339174 1.170694187192196 # 栗子二 for i in range(5): print(random.uniform(8, 2)) #### 2.696842757954265 6.058794935110275 7.567631220015144 2.2057698202258074 4.454083664106361
四、傳遞列表作為參數
random.choice
random.choice(seq)
- 從非空序列 seq 返回一個隨機元素
- 如果 seq 為空,會拋出 IndexError
例如:
# 數字數組 print(random.choice([1, 2, 3, 4, 5])) # 字母數組 print(random.choice(["a", "b", "c"])) # 字母元組 print(random.choice(("a", "b", "c"))) # 字符串 print(random.choice("abcdef")) # string 模塊返回的大小寫字母字符串 print(random.choice(string.ascii_letters)) # string 模塊返回的數字字符串 print(random.choice(string.digits)) # string 模塊返回的數字字符串+大小寫字母字符串 print(random.choice(string.digits + string.ascii_uppercase)) #### 5 c c e l 2 F
random.choices
語法格式
random.choices(population, weights=None, *, cum_weights=None, k=1)
- populaiton:序列
- weights:普通權重
- cum_weights:累加權重
- k:選擇次數
- weights 和 cum_weights 不能同時傳,只能選擇一個來傳
例如:
①不帶參數的栗子:可以重復取元素
a = [1,2,3,4,5] print(random.choices(a,k=5)) # 結果 [5, 5, 3, 1, 5]
②帶 weight 的栗子一:
a = [1, 2, 3, 4, 5] print(random.choices(a, weights=[0, 0, 1, 0, 0], k=5)) # 結果 [3,3,3,3,3]
- 序列有多長,weights 對應的序列就得多長,每個位置都是一一對應
- 像這里,3 的權重是 1,其他是 0 ,所以每次都取 3,因為它的權重最高,其他元素沒有權重
③帶 weight 的栗子二:2 的權重更大,所以取到它的概率更高
a = [1, 2, 3, 4, 5] print(random.choices(a, weights=[0, 2, 1, 0, 0], k=5)) # 結果 [2, 2, 2, 2, 3]
④帶 cum_weights 的栗子:
a = [1, 2, 3, 4, 5] print(random.choices(a, cum_weights=[1, 1, 1, 1, 1], k=5)) print(random.choices(a, cum_weights=[1, 4, 4, 4, 4], k=5)) print(random.choices(a, cum_weights=[1, 2, 3, 4, 5], k=5)) # 結果 [1, 1, 1, 1, 1] [2, 2, 1, 2, 1] [5, 5, 1, 4, 2]
random.shuffle
語法格式
random.shuffle(x[, random])
- 將序列 x 隨機打亂位置
- 只能是列表;元組和字符串會報錯
- random 暫時沒找到有什么用,可以忽略
例如:
# 數字數組 a = [1, 2, 3, 4, 5] random.shuffle(a) print(a) # 字母數組 b = ["a", "b", "c"] random.shuffle(b) print(b) #### [3, 5, 2, 4, 1] ['a', 'c', 'b']
random.sample
語法格式
random.sample(population, k)
- 從 population 中取 k 個元素,組成新的列表並返回
- 每次取元素都是不重復的,所以 population 的長度必須 ≥ k,否則會報錯
例如:
# 數字數組 print(random.sample([1, 2, 3, 4, 5], 3)) # 字母數組 print(random.sample(["a", "b", "c"], 3)) # 字母元組 print(random.sample(("a", "b", "c"), 3)) # 字符串 print(random.sample("abcdef", 3)) # string 模塊返回的大小寫字母字符串 print(random.sample(string.ascii_letters, 3)) # string 模塊返回的數字字符串 print(random.sample(string.digits, 3)) # string 模塊返回的數字字符串+大小寫字母字符串 print(random.sample(string.digits + string.ascii_uppercase, 3)) #### [2, 1, 3] ['b', 'c', 'a'] ['a', 'b', 'c'] ['a', 'f', 'b'] ['M', 'w', 'W'] ['7', '1', '5'] ['R', '8', 'O']
