生成随机 64 位整数

编程入门 行业动态 更新时间:2024-10-25 16:22:11
本文介绍了生成随机 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 位整数

本文发布于:2023-11-30 05:39:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!