I wonder how the random.normalvariate function works. When I do the method below, I end up with negative numbers. I don't want that. How do I prevent that?

And also, i want 10 numbers, but it always turn out 9 and a "none". What is none? How can i remove it?

CODE:
i

``````mport random
resultat = [random.normalvariate(self.my, self.sigma) for i in range(0,9)]
print resultat``````

http://www.python.org/doc/2.5.2/lib/module-random.html
That is the documentation for the random module. I suggest you take a look at it. Granted, it's not very discriptive of the function you're using, but I don't know what it does either. I think that it generates a float between the two numbers you put in or something. :/ Anyways, what are you using it for? I could give you a better solution using a different function

The random.normalvariate( mu, sigma) function has to be called with the arguments:
mu = the requested mean
sigma = the requested standard deviation
You have to supply those arguments.

Also, if you want to loop 10 times use range(10).

Indentations are important with Python. Line up your code properly.

Also, the normal distribution can return negative numbers, there is nothing wrong with that.

yeah i know i have loops, and the right indents in my original code. but even if i have a loop the last one turns "none".

what i wonder is, how to make all the numbers positive. there is something wrong with that because i need only positive numbers in my code.

yeah i know i have loops, and the right indents in my original code. but even if i have a loop the last one turns "none".

what i wonder is, how to make all the numbers positive. there is something wrong with that because i need only positive numbers in my code.

Use function abs() for absolute value.

Something like this example ...

``````import random

# mean mu
mu = 12.5
# standard diviation sigma = mu ** 0.5
sigma = 3.53
resultat = [abs(random.normalvariate(mu, sigma)) for i in range(10)]

print resultat

"""my test resultat -->
[7.091583185794657, 3.4583670333483294, 11.877525423436163,
4.8899955831160469, 12.763448492282313, 13.44488190820579,
14.050995227925315, 14.896283137343586, 13.475151825413349,
15.062250014411603]
"""``````

The probability that normalvariate returns a negative number can be computed with the scipy module like this

``````from scipy.special import erf as erf
from math import sqrt
sqrt2 = sqrt(2.0)

def proba_negative(mu, sigma):
return 0.5 * ( 1.0 + erf(- mu/(sigma * sqrt2)))``````

Ah thank you!

I also found that i could use "fabs". What is the difference between abs and "fabs"?

Ah thank you!

I also found that i could use "fabs". What is the difference between abs and "fabs"?

abs() is a built-in function and returns an integer or float, dep. on arg. type.
fabs() is part of the math module and returns a float.

For example ...

``````import math

print abs(-2)        # 2
print math.fabs(-2)  # 2.0

print abs(-123.456)  # 123.456``````