<tfoot id='tiTEH'></tfoot>

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

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

        <bdo id='tiTEH'></bdo><ul id='tiTEH'></ul>
      1. rand() 在单个函数中调用时返回相同的值

        rand() returns same values when called within a single function(rand() 在单个函数中调用时返回相同的值)
        <i id='ZGfWw'><tr id='ZGfWw'><dt id='ZGfWw'><q id='ZGfWw'><span id='ZGfWw'><b id='ZGfWw'><form id='ZGfWw'><ins id='ZGfWw'></ins><ul id='ZGfWw'></ul><sub id='ZGfWw'></sub></form><legend id='ZGfWw'></legend><bdo id='ZGfWw'><pre id='ZGfWw'><center id='ZGfWw'></center></pre></bdo></b><th id='ZGfWw'></th></span></q></dt></tr></i><div id='ZGfWw'><tfoot id='ZGfWw'></tfoot><dl id='ZGfWw'><fieldset id='ZGfWw'></fieldset></dl></div>
        <legend id='ZGfWw'><style id='ZGfWw'><dir id='ZGfWw'><q id='ZGfWw'></q></dir></style></legend>

          <tbody id='ZGfWw'></tbody>

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

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

                  本文介绍了rand() 在单个函数中调用时返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 C++ 新手,对此我很困惑.我需要在主函数中调用这个函数三次,但每次都给我相同的结果,即 pull_1、pull_2、pull_3 是相同的.我需要做什么才能使它们真正随机?

                  I'm a C++ newbie and I'm stumped on this. I need to call this function in my main function three times but each time it gives me the same result, i.e. pull_1, pull_2, pull_3 are the same. What do I need to do to make them actually random?

                  string PullOne()
                  {
                      string pick;
                      string choices[3] = {"BAR", "7", "cherries"};
                  
                      std::srand(time(0));
                      pick = choices[(std::rand() % 3)];
                      return pick;
                  }
                  

                  来自我的主要功能:

                  string pull_1, pull_2, pull_3;
                  pull_1 = PullOne();
                  pull_2 = PullOne();
                  pull_3 = PullOne();
                  

                  推荐答案

                  你不应该在每次调用 rand() 之前调用 srand().调用一次在程序开始的某个位置.

                  You shouldn't call srand() before each call to rand(). Call it once – somewhere at the start of your program.

                  问题是您重新启动随机生成器,以便它从同一点开始生成完全相同的伪随机序列.

                  The problem is you restart the random generator so it starts to produce the very same pseudorandom sequence from the very same point.

                  这篇关于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++ 中生成随机数的最佳方法是什么?)

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

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