如何在python中使用random.expovariate()生成整数到达时间 [英] how to generate integer inter arrival times using random.expovariate() in python

查看:54
本文介绍了如何在python中使用random.expovariate()生成整数到达时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python random 模块中,expovariate() 函数生成浮点数,可用于对泊松过程的到达间隔时间进行建模.我如何利用它来生成到达之间的整数时间而不是浮点数?

解决方案

jonrsharpe 已经有点提到了,你可以让函数生成浮点数,并使用 int()

这个

<预><代码>>>>随机导入>>>[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]

然后应该输入为

<预><代码>>>>随机导入>>>[int(random.expovariate(0.2)) for i in range(10)][0, 10, 5, 15, 4, 0, 0, 4, 5, 4]

另一个例子

<预><代码>>>>随机导入>>>[int(random.expovariate(0.001)) for i in range(10)][435, 64, 575, 2147, 1233, 1630, 1128, 899, 180, 1190]

以上示例使用列表理解在一行中生成多个结果.当然可以简化为

<预><代码>>>>随机导入>>>int(random.expovariate(0.1)5

请注意,如果您将更大的数字传递给 .expovariate(),您更有可能获得更小的浮点数,并且在使用 int() 进行转换时浮点数转换为整数,0 到 1 之间的数字将转换为 0.

In python random module, the expovariate() 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()

This

>>> 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)
5

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.

这篇关于如何在python中使用random.expovariate()生成整数到达时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆