Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python numpy.random.normal only positive values

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)
like image 213
ustroetz Avatar asked May 01 '13 02:05

ustroetz


People also ask

What are the three arguments for NP random normal ()?

The np. random. normal function has three primary parameters that control the output: loc , scale , and size .

How do you simulate random normal numbers?

Use the formula "=NORMINV(RAND(),B2,C2)", where the RAND() function creates your probability, B2 provides your mean and C2 references your standard deviation. You can change B2 and C2 to reference different cells or enter the values into the formula itself.


1 Answers

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.

like image 64
wim Avatar answered Sep 19 '22 12:09

wim