生成随机 64 位整数 [英] generate random 64 bit integer

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

问题描述

我需要你的帮助,请给我一些建议.从编程珍珠我知道要生成随机的 30 位整数,我们应该这样写:

I need your help and please give me some advice. From programming pearls I know that to generate random 30 bit integer we should write it like this:

RAND_MAX*rand()+rand()

但是我该怎么做才能生成不是 30 位而是 64 位的随机整数呢?如果我将两个 30 位整数相乘,然后再乘以 4 位整数,我认为这是一种非常低效的方法,那么我应该使用什么样的方法呢?我现在正在使用 popcount_1 不同的 64 位方法,我想在随机整数上测试它(我也在测量每个人完成任务所需的时间)

But what could I do for generating not 30, but 64 bit random integer instead? I think that is very inefficient method if I multiply two 30 bit integers and then multiply again 4 bit integer, so what kind of method should I use? I am using now popcount_1 different method for 64 bit one and I would like to test it on random integers(I am also measuring the time which each one takes to accomplish the task)

推荐答案

这可能是一个解决方案,无需乘法:

This could be a solution, without multiplication:

r30 = RAND_MAX*rand()+rand()
s30 = RAND_MAX*rand()+rand()
t4  = rand() & 0xf

res = (r30 << 34) + (s30 << 4) + t4

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

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