Python - 隨機模組(random)


1.random.choice

import random
data = [1, 6, 16, 25, 29]

# choice:隨機從列表中選取出一個數值
result = random.choice(data)
print(result)

2.random.sample


import random
data = [1, 6, 16, 25, 29]
# sample:隨機從列表中選取出3個數值
result = random.sample(data, 3)
print(result)

3.random.shuffle


import random
data = [1, 6, 16, 25, 29]

# 3.random.shuffle: 對List隨機作順序排列的變更
random.shuffle(data)
print(data)
random.shuffle(data)
print(data)

4.random.random

# 4.random.random 0~1 之間的隨機亂數
for i in range(10):
    result = random.random()
    print(f"random 第{i+1}次:{result}")
print("")

5.random.uniform

# 5.random.uniform 起訖範圍內的隨機亂數
for i in range(10):
    result = random.uniform(60, 100)
    print(f"uniform 第{i+1}次:{result}")
print("")

6.random.normalvariate


# 6.平均數:100,標準差10
# 得到的資料大多數會在90~110之間
print("取得常態分配亂數:")
for i in range(20):
    result = random.normalvariate(100, 10)
    print(f"常態分配亂數 第{i+1}次:{result}")
#Python






你可能感興趣的文章

九月 秋天 心裡暖烘烘的

九月 秋天 心裡暖烘烘的

DAY40:Are they the "same"?

DAY40:Are they the "same"?

淺談二分搜尋法

淺談二分搜尋法






留言討論