python (SimPy) generate random numbers that follow the erlang distribution
-
03-07-2021 - |
Вопрос
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:
- Is there some method that I overlooked?
- Do I have to import some other library?
- 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!
Решение
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.
Другие советы
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)