Random number with specific variance in Python

后端 未结 2 1774
终归单人心
终归单人心 2021-01-07 06:37

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

相关标签:
2条回答
  • 2021-01-07 06:54
    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)]
    

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

    0 讨论(0)
  • 2021-01-07 06:55

    Use random.normalvariate (or random.gauss if you don't need thread-safety), and set the sigma argument to the square root of the variance.

    0 讨论(0)
提交回复
热议问题