Python中具有特定方差的随机数

问题描述:

在Python程序中,我需要生成具有特定的,由用户控制的方差的正态分布的随机数.我该怎么办?

In a Python program, I need to generate normally-distributed random numbers with a specific, user-controlled variance. How can I do this?

import math
from random import gauss

my_mean = 0
my_variance = 10

random_numbers = [gauss(my_mean, math.sqrt(my_variance)) for i in range(100)]

这将为您提供100个均值0且方差为10的正态分布随机数.

This gets you 100 normally-distributed random numbers with mean 0 and variance 10.