Python中的全范围随机数 [英] Full-range random number in Python
问题描述
我正在使用此行生成一系列随机浮点数:
I'm generating a series of random floats using this line:
random.random()*(maxval-minval) + minval
我正在使用它来将可变噪声添加到给定变量中,并且添加的噪声量取决于一系列因素.在某些情况下,噪声应该很高,以至于在实践中会丢失原始值,而我则拥有一个完全随机的值.
I'm using it to add variable noise to a given variable, and the amount of noise added depends on a series of factors. In some cases, the noise should be so high that in practice the original value is lost, and I have a completely random value.
在这种情况下,代码适用于有限值,但是如果我使用"inf",它将返回NaN.有没有变通办法允许连续的随机范围可能包括无穷大?我不想篡改os.random()
,因为它是特定于计算机的.
In this context, the code works works with finite values, but if I use "inf" it returns NaN. Is there a workaround to allow a continuos random range that might include the infinity? I don't want to tamper with os.random()
as it is machine-specific.
推荐答案
在评论中讨论之后,我提出以下建议:
After the discussion in comments i suggest the following :
>>> m=sys.maxint
>>> np.random.uniform(-m,m,5)
array([ -5.32362215e+18, -2.90131323e+18, 5.14492175e+18,
-5.64238742e+18, -3.49640768e+18])
据说,您可以使用sys.maxint
来获取最大整数,然后可以使用np.random.randint
来获取maxint
和-maxint
之间的随机数.
As is said the you can get the max integer with sys.maxint
then you can use np.random.randint
to get a random number between the maxint
and -maxint
.
这篇关于Python中的全范围随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!