python - 根据均值和标准差生成随机整数

import numpy as np, pandas as pd, math

while True:
    mean, std_dev, num_samples = 105, 2.5, 100
    data = np.random.normal(loc=mean, scale=std_dev, size=num_samples)
    df = pd.DataFrame(data, columns=['value'])
    for i in range(len(df)):
        df['value'][i] = math.ceil(df['value'][i])  # 数字取整数,根据实际情况而定
    if 104 <= df['value'].mean().round(2) and df['value'].mean().round(2) <= 106:
        break
print(f"范围: {df['value'].min()}-{df['value'].max()}")
print(f"平均: {df['value'].mean().round(2)}±{df['value'].std().round(2)}")
# 范围: 97.0-111.0
# 平均: 104.96±2.51
posted @ 2024-07-27 17:39  wstong  阅读(1)  评论(0编辑  收藏  举报