<bdo id='YdTNM'></bdo><ul id='YdTNM'></ul>
  • <legend id='YdTNM'><style id='YdTNM'><dir id='YdTNM'><q id='YdTNM'></q></dir></style></legend>

  • <tfoot id='YdTNM'></tfoot>

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

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

        在 C++ 中使用 rand() 函数的正确方法是什么?

        时间:2023-10-07
            <tbody id='A5Ynv'></tbody>
          <legend id='A5Ynv'><style id='A5Ynv'><dir id='A5Ynv'><q id='A5Ynv'></q></dir></style></legend>

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

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

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

                  本文介绍了在 C++ 中使用 rand() 函数的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在做一个书本练习,内容是编写一个生成伪随机数的程序.我从简单开始.

                  I'm doing a book exercise that says to write a program that generates psuedorandom numbers. I started off simple with.

                  #include "std_lib_facilities.h"
                  
                  int randint()
                  {
                      int random = 0;
                      random = rand();
                      return random;
                  }
                  
                  int main()
                  {
                      char input = 0;
                      cout << "Press any character and enter to generate a random number." << endl;
                      while (cin >> input)
                      cout << randint() << endl;
                      keep_window_open();
                  }
                  

                  我注意到每次程序运行时,都会有相同的随机"输出.所以我研究了随机数生成器,并决定通过在 randint() 中首先包含它来尝试播种.

                  I noticed that each time the program was run, there would be the same "random" output. So I looked into random number generators and decided to try seeding by including this first in randint().

                      srand(5355);
                  

                  它一遍又一遍地生成相同的数字(我现在觉得实现它很愚蠢.)

                  Which just generated the same number over and over (I feel stupid now for implementing it.)

                  所以我想我会很聪明并像这样实现种子.

                  So I thought I'd be clever and implement the seed like this.

                  srand(rand());
                  

                  这基本上和程序最初所做的一样,但输出了一组不同的数字(这是有道理的,因为 rand() 生成的第一个数字总是 41.)

                  This basically just did the same as the program did in the first place but outputted a different set of numbers (which makes sense since the first number generated by rand() is always 41.)

                  我唯一能想到的让这更随机的是:

                  The only thing I could think of to make this more random is to:

                  1. 让用户输入一个数字并将其设置为种子(这很容易实现,但这是最后的手段)或
                  2. 以某种方式将种子设置为计算机时钟或其他不断变化的数字.

                  我是不是脑子进水了,我现在应该停下来吗?选项 2 难以实施吗?还有其他想法吗?

                  Am I in over my head and should I stop now? Is option 2 difficult to implement? Any other ideas?

                  提前致谢.

                  推荐答案

                  选项 2 并不难,给你:

                  Option 2 isn't difficult, here you go:

                  srand(time(NULL));
                  

                  你需要为 srand() 包含 stdlib.h 和为 time() 包含 time.h代码>.

                  you'll need to include stdlib.h for srand() and time.h for time().

                  这篇关于在 C++ 中使用 rand() 函数的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 [0..n-1] 范围内生成 m 个不同的随机数 下一篇:如果我们在不同的机器上播种 c++11 mt19937 相同,

                  相关文章

                  最新文章

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

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

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

                    <tfoot id='gKbf2'></tfoot>

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