我需要你的帮助,请给我一些建议.从编程珍珠我知道要生成随机的 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