okkhoy okkhoy - 1 month ago 5x
Python Question

how to generate integer inter arrival times using random.expovariate() in python

In python

module, the
function generates floating point numbers which can be used to model inter-arrival times of a Poisson process. How do I make use of this to generate integer times between arrival instead of floating point numbers?


jonrsharpe already kind of mentioned it, you can just let the function generate floating point numbers, and convert the output to integers yourself using int()


>>> import random
>>> [random.expovariate(0.2) for i in range(10)]
[7.3965169407177465, 6.950770519458953, 9.690677483221426, 2.1903490679843927, 15.769487400856976, 3.508366058170216, 2.1922982271553155, 2.591955678743926, 7.791150855029359, 22.180358323964935]

Should then be typed as

>>> import random
>>> [int(random.expovariate(0.2)) for i in range(10)]
[0, 10, 5, 15, 4, 0, 0, 4, 5, 4]

Another example

>>> import random
>>> [int(random.expovariate(0.001)) for i in range(10)]
[435, 64, 575, 2147, 1233, 1630, 1128, 899, 180, 1190]

The examples above use list comprehension to generate multiple results in one line. Can of course be reduced to

>>> import random
>>> int(random.expovariate(0.1)

Note that if you pass a higher number to .expovariate() you are more likely to get smaller floating points as result, and when using int() to convert the floats to integers, numbers that are between 0 and 1 will get converted to 0.