<tfoot id='tzdUS'></tfoot>
      <i id='tzdUS'><tr id='tzdUS'><dt id='tzdUS'><q id='tzdUS'><span id='tzdUS'><b id='tzdUS'><form id='tzdUS'><ins id='tzdUS'></ins><ul id='tzdUS'></ul><sub id='tzdUS'></sub></form><legend id='tzdUS'></legend><bdo id='tzdUS'><pre id='tzdUS'><center id='tzdUS'></center></pre></bdo></b><th id='tzdUS'></th></span></q></dt></tr></i><div id='tzdUS'><tfoot id='tzdUS'></tfoot><dl id='tzdUS'><fieldset id='tzdUS'></fieldset></dl></div>

        <bdo id='tzdUS'></bdo><ul id='tzdUS'></ul>

        <small id='tzdUS'></small><noframes id='tzdUS'>

        <legend id='tzdUS'><style id='tzdUS'><dir id='tzdUS'><q id='tzdUS'></q></dir></style></legend>
      1. 在 C/C++ 中获取大随机数

        Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
          <legend id='UtPwT'><style id='UtPwT'><dir id='UtPwT'><q id='UtPwT'></q></dir></style></legend>
        1. <small id='UtPwT'></small><noframes id='UtPwT'>

            <tbody id='UtPwT'></tbody>
                <bdo id='UtPwT'></bdo><ul id='UtPwT'></ul>
              • <tfoot id='UtPwT'></tfoot>
                <i id='UtPwT'><tr id='UtPwT'><dt id='UtPwT'><q id='UtPwT'><span id='UtPwT'><b id='UtPwT'><form id='UtPwT'><ins id='UtPwT'></ins><ul id='UtPwT'></ul><sub id='UtPwT'></sub></form><legend id='UtPwT'></legend><bdo id='UtPwT'><pre id='UtPwT'><center id='UtPwT'></center></pre></bdo></b><th id='UtPwT'></th></span></q></dt></tr></i><div id='UtPwT'><tfoot id='UtPwT'></tfoot><dl id='UtPwT'><fieldset id='UtPwT'></fieldset></dl></div>

                • 本文介绍了在 C/C++ 中获取大随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  标准 rand() 函数给出的数字对我来说不够大:我需要 unsigned long long 的.我们如何获得真正大的随机数?我尝试修改一个简单的散列函数,但它太大,运行时间太长,而且永远不会产生小于 1e5 的数字!!

                  Standard rand() function gives numbers not big enough for me: I need unsigned long long ones. How do we get really big random numbers? I tried modifying a simple hash function but it's too big, takes too long to run and never produces numbers which are less than 1e5!!

                  推荐答案

                  这是一个返回随机 64 位数字的便携式 C99 解决方案:

                  Here's a portable C99 solution that returns a random 64-bit number:

                  unsigned long long llrand() {
                      unsigned long long r = 0;
                  
                      for (int i = 0; i < 5; ++i) {
                          r = (r << 15) | (rand() & 0x7FFF);
                      }
                  
                      return r & 0xFFFFFFFFFFFFFFFFULL;
                  }
                  

                  说明:rand() 返回 0 到 RAND_MAX 范围内的整数,并且 RAND_MAX 只保证至少为 32,767(15 个随机位).long long 保证有 64 位,但可能更大.

                  Explanation: rand() returns integers in the range 0 to RAND_MAX and RAND_MAX is only guaranteed to be at least 32,767 (15 random bits). long long is guaranteed to have 64 bits but may be larger.

                  这篇关于在 C/C++ 中获取大随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                  How do I generate thread-safe uniform random numbers?(如何生成线程安全的统一随机数?)
                  <legend id='ioCW5'><style id='ioCW5'><dir id='ioCW5'><q id='ioCW5'></q></dir></style></legend>

                        • <bdo id='ioCW5'></bdo><ul id='ioCW5'></ul>
                              <tbody id='ioCW5'></tbody>
                            <i id='ioCW5'><tr id='ioCW5'><dt id='ioCW5'><q id='ioCW5'><span id='ioCW5'><b id='ioCW5'><form id='ioCW5'><ins id='ioCW5'></ins><ul id='ioCW5'></ul><sub id='ioCW5'></sub></form><legend id='ioCW5'></legend><bdo id='ioCW5'><pre id='ioCW5'><center id='ioCW5'></center></pre></bdo></b><th id='ioCW5'></th></span></q></dt></tr></i><div id='ioCW5'><tfoot id='ioCW5'></tfoot><dl id='ioCW5'><fieldset id='ioCW5'></fieldset></dl></div>

                            <tfoot id='ioCW5'></tfoot>
                          1. <small id='ioCW5'></small><noframes id='ioCW5'>