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

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

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

      1. <legend id='sIPrx'><style id='sIPrx'><dir id='sIPrx'><q id='sIPrx'></q></dir></style></legend>
      2. <tfoot id='sIPrx'></tfoot>

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

        时间:2023-06-05
        • <bdo id='7UGJj'></bdo><ul id='7UGJj'></ul>

          <legend id='7UGJj'><style id='7UGJj'><dir id='7UGJj'><q id='7UGJj'></q></dir></style></legend>
            <tbody id='7UGJj'></tbody>
        • <small id='7UGJj'></small><noframes id='7UGJj'>

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

                  <tfoot id='7UGJj'></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() 函数的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文章

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

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

                      <tfoot id='JEiYl'></tfoot>
                      • <bdo id='JEiYl'></bdo><ul id='JEiYl'></ul>