1. <tfoot id='vjttR'></tfoot>

      1. <legend id='vjttR'><style id='vjttR'><dir id='vjttR'><q id='vjttR'></q></dir></style></legend>

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

      3. rand() 在单个函数中调用时返回相同的值

        时间:2023-10-07
        <tfoot id='kmzFI'></tfoot>

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

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

                • 本文介绍了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() 在单个函数中调用时返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么 rand() 的使用被认为是不好的? 下一篇:为什么 rand() 每次运行都会产生相同的数字序列

                  相关文章

                  最新文章

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

                  • <bdo id='R9fAu'></bdo><ul id='R9fAu'></ul>

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

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