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!

2 Answers 2

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.

2
  • 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, 2012 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, 2012 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)
3
  • thank you, but please check my comment on Harel's post on how to match an Erlang distribution using Alpha and Beta
    – george
    Sep 20, 2012 at 17:46
  • 1
    @george α = κ and β = 1/θ
    – chucksmash
    Sep 20, 2012 at 17:47
  • Thank you very much! I'd like to accept both answers but I can only one and since Harel replied first..
    – george
    Sep 20, 2012 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.