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

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

          <bdo id='k6qbn'></bdo><ul id='k6qbn'></ul>
      1. <tfoot id='k6qbn'></tfoot>

        <legend id='k6qbn'><style id='k6qbn'><dir id='k6qbn'><q id='k6qbn'></q></dir></style></legend>

      2. 如何从 rand() 缩小数字?

        How do I scale down numbers from rand()?(如何从 rand() 缩小数字?)
        • <bdo id='Lir25'></bdo><ul id='Lir25'></ul>
          <i id='Lir25'><tr id='Lir25'><dt id='Lir25'><q id='Lir25'><span id='Lir25'><b id='Lir25'><form id='Lir25'><ins id='Lir25'></ins><ul id='Lir25'></ul><sub id='Lir25'></sub></form><legend id='Lir25'></legend><bdo id='Lir25'><pre id='Lir25'><center id='Lir25'></center></pre></bdo></b><th id='Lir25'></th></span></q></dt></tr></i><div id='Lir25'><tfoot id='Lir25'></tfoot><dl id='Lir25'><fieldset id='Lir25'></fieldset></dl></div>
            • <small id='Lir25'></small><noframes id='Lir25'>

                  <tfoot id='Lir25'></tfoot>
                    <tbody id='Lir25'></tbody>

                  <legend id='Lir25'><style id='Lir25'><dir id='Lir25'><q id='Lir25'></q></dir></style></legend>

                  本文介绍了如何从 rand() 缩小数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下代码每秒输出一个随机数:

                  The following code outputs a random number each second:

                  int main ()
                  {
                      srand(time(NULL)); // Seeds number generator with execution time.
                  
                      while (true)
                      {
                          int rawRand = rand();
                  
                          std::cout << rawRand << std::endl;
                  
                          sleep(1);
                      }
                  }
                  

                  如何缩小这些数字的大小,使其始终在 0-100 的范围内?

                  How might I size these numbers down so they're always in the range of 0-100?

                  推荐答案

                  如果您使用的是 C++ 并且关心良好的分布,您可以使用 TR1 C++11 .

                  If you are using C++ and are concerned about good distribution you can use TR1 C++11 <random>.

                  #include <random>
                  
                  std::random_device rseed;
                  std::mt19937 rgen(rseed()); // mersenne_twister
                  std::uniform_int_distribution<int> idist(0,100); // [0,100]
                  
                  std::cout << idist(rgen) << std::endl;
                  

                  这篇关于如何从 rand() 缩小数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                    <tbody id='1HQUF'></tbody>
                1. <i id='1HQUF'><tr id='1HQUF'><dt id='1HQUF'><q id='1HQUF'><span id='1HQUF'><b id='1HQUF'><form id='1HQUF'><ins id='1HQUF'></ins><ul id='1HQUF'></ul><sub id='1HQUF'></sub></form><legend id='1HQUF'></legend><bdo id='1HQUF'><pre id='1HQUF'><center id='1HQUF'></center></pre></bdo></b><th id='1HQUF'></th></span></q></dt></tr></i><div id='1HQUF'><tfoot id='1HQUF'></tfoot><dl id='1HQUF'><fieldset id='1HQUF'></fieldset></dl></div>

                    <bdo id='1HQUF'></bdo><ul id='1HQUF'></ul>
                          <tfoot id='1HQUF'></tfoot>
                        • <legend id='1HQUF'><style id='1HQUF'><dir id='1HQUF'><q id='1HQUF'></q></dir></style></legend>

                          <small id='1HQUF'></small><noframes id='1HQUF'>