共計(jì) 435 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
Python 生成隨機(jī)浮點(diǎn)數(shù)的方法有以下幾種:
- 使用 random 模塊的 random() 函數(shù):
import random
x = random.random() # 生成一個(gè) 0 到 1 之間的隨機(jī)浮點(diǎn)數(shù)
- 使用 random 模塊的 uniform() 函數(shù):
import random
x = random.uniform(a, b) # 生成一個(gè)位于 a 和 b 之間的隨機(jī)浮點(diǎn)數(shù)
- 使用 numpy 庫的 random 模塊:
import numpy as np
x = np.random.random() # 生成一個(gè) 0 到 1 之間的隨機(jī)浮點(diǎn)數(shù)
- 使用 numpy 庫的 random 模塊的 uniform() 函數(shù):
import numpy as np
x = np.random.uniform(a, b) # 生成一個(gè)位于 a 和 b 之間的隨機(jī)浮點(diǎn)數(shù)
- 使用 random 模塊的 gauss() 函數(shù)生成正態(tài)分布的隨機(jī)浮點(diǎn)數(shù):
import random
x = random.gauss(mu, sigma) # 生成一個(gè)均值為 mu,標(biāo)準(zhǔn)差為 sigma 的正態(tài)分布隨機(jī)浮點(diǎn)數(shù)
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完