2

I am using Python (SimPy package mostly, but it is irrelevant to the question I think), modeling some systems and running simulations. For this purpose I need to produce random numbers that follow distributions. I have done alright so far with some distributions like exponential and normal by importing the random (eg from random import *) and using the expovariate or normalvariate methods. However I cannot find any method in random that produce numbers that follow the Erlang distribution. So:

  1. Is there some method that I overlooked?
  2. Do I have to import some other library?
  3. Can I make some workaround? (In think that I can use the Exponential distribution to produce random “Erlang” numbers but I am not sure how. A piece of code might help me.

Thank you in advance!

aronisstav
  • 7,755
  • 5
  • 23
  • 48
george
  • 1,386
  • 5
  • 22
  • 38

2 Answers2

5

Erlang distribution is a special case of the gamma distribution, which exists as numpy.random.gamma (reference). Just use an integer value for the k ("shape") argument. See also about scipy.stats.gamma for functions with the PDF, CDF etc.

Harel
  • 327
  • 1
  • 5
  • Thank you. In random I only find "gammavariate(self, alpha, beta)" is that the one? If yes, how should I set alpha and beta to match an Erlang distribution that need a μ and σ as arguments? – george Sep 20 '12 at 17:44
  • 1
    Yes, you can use that if you don't have numpy. But notice that while alpha=k, beta is 1/theta (that's just another common method of describing the Gamma distribution parameters. – Harel Sep 20 '12 at 17:47
3

As the previous answer stated, the erlang distribution is a special case of the gamma distribution. As far as I know, you do not, however, need the numpy package. Random numbers from a gamma distribution can be generated in python using random.gammavariate(alpha, beta).

Usage:

import random
print random.gammavariate(3,1)
chucksmash
  • 5,777
  • 1
  • 32
  • 41