Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating numbers with Gaussian function in a range using python

I want to use the gaussian function in python to generate some numbers between a specific range giving the mean and variance

so lets say I have a range between 0 and 10

and I want my mean to be 3 and variance to be 4

mean = 3, variance = 4

how can I do that ?

like image 786
Lily Avatar asked May 09 '13 21:05

Lily


2 Answers

Use random.gauss. From the docs:

random.gauss(mu, sigma)
    Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly
    faster than the normalvariate() function defined below.

It seems to me that you can clamp the results of this, but that wouldn't make it a Gaussian distribution. I don't think you can satisfy all the constraints simultaneously. If you want to clamp it to the range [0, 10], you could get your numbers:

num = min(10, max(0, random.gauss(3, 4)))

But then the resulting distribution of numbers won't be truly Gaussian. In this case, it seems you can't have your cake and eat it, too.

like image 101
Dan Lecocq Avatar answered Oct 15 '22 11:10

Dan Lecocq


There's probably a better way to do this, but this is the function I ended up creating to solve this problem:

import random

def trunc_gauss(mu, sigma, bottom, top):
    a = random.gauss(mu,sigma))
    while (bottom <= a <= top) == False:
        a = random.gauss(mu,sigma))
    return a

If we break it down line by line:

import random

This allows us to use functions from the random library, which includes a gaussian random number generator (random.gauss).

def trunc_gauss(mu, sigma, bottom, top):

The function arguments allow us to specify the mean (mu) and variance (sigma), as well as the top and bottom of our desired range.

a = random.gauss(mu,sigma))

Inside the function, we generate an initial random number according to a gaussian distribution.

while (bottom <= a <= top) == False:
a = random.gauss(mu,sigma))

Next, the while loop checks if the number is within our specified range, and generates a new random number as long as the current number is outside our range.

return a

As soon as the number is inside our range, the while loop stops running and the function returns the number.

This should give a better approximation of a gaussian distribution, since we don't artificially inflate the top and bottom boundaries of our range by rounding up or down the outliers.

I'm quite new to Python, so there are most probably simpler ways, but this worked for me.

like image 26
JimmyLamothe Avatar answered Oct 15 '22 12:10

JimmyLamothe