Python numpy.random.normal only positive values

ustroetz picture ustroetz · May 1, 2013 · Viewed 22.1k times · Source

I want to create a normal distributed array with numpy.random.normal that only consists of positive values. For example the following illustrates that it sometimes gives back negative values and sometimes positive. How can I modify it so it will only gives back positive values?

>>> import numpy
>>> numpy.random.normal(10,8,3)
array([ -4.98781629,  20.12995344,   4.7284051 ])
>>> numpy.random.normal(10,8,3)
array([ 17.71918829,  15.97617052,   1.2328115 ])
>>> 

I guess I could solve it somehow like this:

myList = numpy.random.normal(10,8,3)

while item in myList <0:
       # run again until all items are positive values
       myList = numpy.random.normal(10,8,3)

Answer

wim picture wim · May 1, 2013

The normal distribution, by definition, extends from -inf to +inf so what you are asking for doesn't make sense mathematically.

You can take a normal distribution and take the absolute value to "clip" to positive values, or just discard negative values, but you should understand that it will no longer be a normal distribution.