3

I've been using Numpy's numpy.random.exponential function for a while. I now see that Python's random module has many functions that I didn't know about. Does it have something that replaces numpy.random.exponential? It would be nice to drop the numpy requirement from my project.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

2 Answers2

4

If anything about random.expovariate() does not suit your needs, it's also easy to roll your own version:

def exponential(beta):
    return -beta * math.log(1.0 - random.random())

It seems a bit of an overkill to have a dependency on NumPy just for this functionality.

Note that this function accepts the mean beta as a parameter, as does the NumPy version, whereas the parameter lambd of random.expovariate() is the inverse of beta.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
3

http://docs.python.org/library/random.html#random.expovariate

random.expovariate(lambd)

Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called “lambda”, but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.

gruszczy
  • 40,948
  • 31
  • 128
  • 181