如何在 Python 中生成没有负数的指数变量?

How to generate exponential variate without negative numbers in Python?

如何在 Python 中生成没有负数的指数变量?

我尝试使用此代码,但它生成负数

>>> import random
>>> int(random.expovariate(0.28)
5

我考虑过使用 if 语句,但它会影响我的随机性和最终结果。

来自 random.expovariate 的文档:

Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called “lambda”, but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative.

如果您想要非负结果,请使用非负参数。