随机的int64和float64数字 [英] Random int64 and float64 numbers

查看:119
本文介绍了随机的int64和float64数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Numpy在整个类型的有效值范围之内使用Numpy为整数和浮点数生成随机的64位整数值.要生成随机的32位浮点数,我可以使用:

I'm trying to generate random 64-bit integer values for integers and floats using Numpy, within the entire range of valid values for that type. To generate random 32-bit floats, I can use:

In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10)
Out[2]: 
array([  1.47351436e+37,   9.93620693e+37,   2.22893053e+38,
        -3.33828977e+38,   1.08247781e+37,  -8.37481260e+37,
         2.64176554e+38,  -2.72207226e+37,   2.54790459e+38,
        -2.47883866e+38])

但是如果我尝试将其用于64位数字,我会得到

but if I try and use this for 64-bit numbers, I get

In [3]: np.random.uniform(low=np.finfo(np.float64).min,high=np.finfo(np.float64).max,size=10)
Out[3]: array([ Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf])

类似地,对于整数,我可以成功生成随机的32位整数:

Similarly, for integers, I can successfully generate random 32-bit integers:

In [4]: np.random.random_integers(np.iinfo(np.int32).min,high=np.iinfo(np.int32).max,size=10)
Out[4]: 
array([-1506183689,   662982379, -1616890435, -1519456789,  1489753527,
        -604311122,  2034533014,   449680073,  -444302414, -1924170329])

但不能成功使用64位整数:

but am unsuccessful for 64-bit integers:

In [5]: np.random.random_integers(np.iinfo(np.int64).min,high=np.iinfo(np.int64).max,size=10)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)

/Users/tom/tmp/<ipython console> in <module>()

/Library/Python/2.6/site-packages/numpy/random/mtrand.so in mtrand.RandomState.random_integers (numpy/random/mtrand/mtrand.c:6640)()

/Library/Python/2.6/site-packages/numpy/random/mtrand.so in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:5813)()

OverflowError: long int too large to convert to int

这是预期的行为,还是我应该将其报告为Numpy中的错误?

Is this expected behavior, or should I report these as bugs in Numpy?

推荐答案

对于整数,您可以生成2个32位随机数并将其组合:

For integers you could generate 2 32 bit random numbers and combine them:

a + (b << 32)

这篇关于随机的int64和float64数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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