python产生随机整数数组_在numpy中生成随机int64数组的最简单方法?

如果您有一个旧版本的numpy,并且由于某些原因不能升级,那么您建议的生成随机字节、将其视为int64并根据您所需的范围截断它们的解决方案应该同样有效-

*正如@moarningsun在下面指出的rk_random_uint64实际上使用了拒绝采样,但我仍然认为没有理由不用模来包装这些值。在import numpy as np

from scipy import stats

def randint64(low, high, size, seed=None):

# generate a string of random bytes

n = np.prod(size)

bytes = np.random.RandomState(seed).bytes(n * 8)

# view as an int64 array

ints = np.fromstring(bytes, np.int64, n).reshape(size)

ints %= np.int64(high - low) # truncate

ints += np.int64(low) # offset

return ints

imax = np.iinfo(np.int64).max

print(imax)

# 9223372036854775807

ints = randint64(0, imax, int(1E6), seed=0)

print(ints.max())

# 9223355891497906972

# test uniformity

print(stats.kstest(ints, stats.uniform(loc=0, scale=imax).cdf))

# KstestResult(statistic=0.00085961807556278469, pvalue=0.45082598256836681

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值