前言

  • 為啥突然寫這個?因為用到就寫唄,感覺對生成資料很有用,之前都是百度別人的,今天來對著官方文件寫,超級標準!
  • 這邊只講常用的,看了下文件還有什麼數學方法,太高階好像用不上

返回整數

random.randrange

語法格式

兩種寫法

  1. random.randrange(stop)
  2. random.randrange(start, stop[, step])
  • start:起始數字,包含(取得到 start 這個值)
  • stop:末尾數字,不包含(取不到 stop 這個值)
  • step:步長

實際栗子

  1. # 栗子一
  2. for i in range(5):
  3. print(random.randrange(20))
  4.  
  5. ####
  6. 17
  7. 4
  8. 7
  9. 7
  10. 4
  11.  
  12. # 栗子二
  13. for i in range(5):
  14. print(random.randrange(10, 20))
  15.  
  16. ####
  17. 13
  18. 14
  19. 11
  20. 17
  21. 17
  22.  
  23. # 栗子三
  24. for i in range(5):
  25. print(random.randrange(10, 20, 2))
  26.  
  27. ####
  28. 12
  29. 12
  30. 14
  31. 14
  32. 10

random.randint

語法格式

  • 返回隨機整數 N 滿足 a <= N <= b
  • 相當於 randrange(a, b+1)
  1. random.randint(a, b)

實際栗子

  1. for i in range(5):
  2. print(random.randint(0,20))
  3.  
  4. ####
  5. 19
  6. 20
  7. 11
  8. 6
  9. 3

a、b 都可以取得到哦

返回浮點數

random.random()

語法格式

返回 [0.0, 1.0) 範圍內的下一個隨機浮點數

  1. random.random()

實際栗子

  1. # 栗子一
  2. for i in range(5):
  3. print(random.random())
  4.  
  5. ####
  6. 0.9829492243165335
  7. 0.43473506430105724
  8. 0.5198709187243076
  9. 0.6437884305820736
  10. 0.7216771961168909
  11.  
  12. # 栗子二
  13. for i in range(5):
  14. print(math.ceil(random.random() * 1000))
  15.  
  16. ####
  17. 772
  18. 352
  19. 321
  20. 62
  21. 127

random.uniform(a, b)

語法格式

  • 返回一個隨機浮點數 N
  • 當 a <= b 時,a <= N <= b
  • 當 b < a 時, b <= N <= a
  1. random.uniform(a, b)

實際栗子

  1. # 栗子一
  2. for i in range(5):
  3. print(random.uniform(1, 10))
  4.  
  5. ####
  6. 2.6200262089754593
  7. 9.220506911469235
  8. 3.0206896704014783
  9. 9.670905330339174
  10. 1.170694187192196
  11.  
  12. # 栗子二
  13. for i in range(5):
  14. print(random.uniform(8, 2))
  15.  
  16. ####
  17. 2.696842757954265
  18. 6.058794935110275
  19. 7.567631220015144
  20. 2.2057698202258074
  21. 4.454083664106361

傳遞列表作為引數

random.choice

語法格式

  • 從非空序列 seq 返回一個隨機元素
  • 如果 seq 為空,會丟擲 IndexError
  1. random.choice(seq)

實際栗子

  1. # 數字陣列
  2. print(random.choice([1, 2, 3, 4, 5]))
  3. # 字母陣列
  4. print(random.choice(["a", "b", "c"]))
  5. # 字母元組
  6. print(random.choice(("a", "b", "c")))
  7. # 字串
  8. print(random.choice("abcdef"))
  9. # string 模組返回的大小寫字母字串
  10. print(random.choice(string.ascii_letters))
  11. # string 模組返回的數字字串
  12. print(random.choice(string.digits))
  13. # string 模組返回的數字字串+大小寫字母字串
  14. print(random.choice(string.digits + string.ascii_uppercase))
  15.  
  16. ####
  17. 5
  18. c
  19. c
  20. e
  21. l
  22. 2
  23. F

random.choices

語法格式

  • populaiton:序列
  • weights:普通權重
  • cum_weights:累加權重
  • k:選擇次數
  • weights 和 cum_weights 不能同時傳,只能選擇一個來傳
  1. random.choices(population, weights=None, *, cum_weights=None, k=1) 

看的迷迷糊糊啥意思。。?來看栗子。。

不帶引數的栗子

  1. a = [1,2,3,4,5]
  2. print(random.choices(a,k=5))
  3.  
  4. # 結果
  5. [5, 5, 3, 1, 5]

可以重複取元素

帶 weight 的栗子一

  1. a = [1, 2, 3, 4, 5]
  2. print(random.choices(a, weights=[0, 0, 1, 0, 0], k=5))
  3.  
  4. # 結果
  5. [3,3,3,3,3]
  • 序列有多長,weights 對應的序列就得多長,每個位置都是一一對應
  • 像這裡,3 的權重是 1,其他是 0 ,所以每次都取 3,因為它的權重最高,其他元素沒有權重

帶 weight 的栗子二

  1. a = [1, 2, 3, 4, 5]
  2. print(random.choices(a, weights=[0, 2, 1, 0, 0], k=5))
  3.  
  4. # 結果
  5. [2, 2, 2, 2, 3]

2 的權重更大,所以取到它的概率更高

帶 cum_weights 的栗子

  1. a = [1, 2, 3, 4, 5]
  2.  
  3. print(random.choices(a, cum_weights=[1, 1, 1, 1, 1], k=5))
  4.  
  5. print(random.choices(a, cum_weights=[1, 4, 4, 4, 4], k=5))
  6.  
  7. print(random.choices(a, cum_weights=[1, 2, 3, 4, 5], k=5))
  8.  
  9. # 結果
  10. [1, 1, 1, 1, 1]
  11. [2, 2, 1, 2, 1]
  12. [5, 5, 1, 4, 2]

是不是看不懂?我也看不懂,但其實就是普通權重相加而已

cum_weights=[1, 1, 1, 1, 1]

  • 等價於 weights=[1, 0, 0, 0, 0]
  • [1,1+0,1+0+0,1+0+0+0,1+0+0+0+0]
  • 看懂了沒,太反人類了。。

cum_weights=[1, 4, 4, 4, 4]

  • 等價於 weights=[1, 3, 0, 0, 0]
  • [1,1+3,1+3+0,1+3+0+0,1+3+0+0+0]

random.shuffle

語法格式

  • 將序列 x 隨機打亂位置
  • 只能是列表[],元組、字串會報錯哦
  • random 暫時沒找到有什麼用,可以忽略
  1. random.shuffle(x[, random])

實際栗子

  1. # 數字陣列
  2. a = [1, 2, 3, 4, 5]
  3. random.shuffle(a)
  4. print(a)
  5.  
  6. # 字母陣列
  7. b = ["a", "b", "c"]
  8. random.shuffle(b)
  9. print(b)
  10.  
  11. ####
  12. [3, 5, 2, 4, 1]
  13. ['a', 'c', 'b']

random.sample

語法格式

  • 從 population 中取 k 個元素,組成新的列表並返回
  • 每次取元素都是不重複的,所以 population 的長度必須 ≥ k,否則會報錯
  1. random.sample(population, k)

實際栗子

全都是 k=3

  1. # 數字陣列
  2. print(random.sample([1, 2, 3, 4, 5], 3))
  3. # 字母陣列
  4. print(random.sample(["a", "b", "c"], 3))
  5. # 字母元組
  6. print(random.sample(("a", "b", "c"), 3))
  7. # 字串
  8. print(random.sample("abcdef", 3))
  9. # string 模組返回的大小寫字母字串
  10. print(random.sample(string.ascii_letters, 3))
  11. # string 模組返回的數字字串
  12. print(random.sample(string.digits, 3))
  13. # string 模組返回的數字字串+大小寫字母字串
  14. print(random.sample(string.digits + string.ascii_uppercase, 3))
  15.  
  16. ####
  17. [2, 1, 3]
  18. ['b', 'c', 'a']
  19. ['a', 'b', 'c']
  20. ['a', 'f', 'b']
  21. ['M', 'w', 'W']
  22. ['7', '1', '5']
  23. ['R', '8', 'O']