python随机数范围,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.

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值