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

    2. <small id='TsmRk'></small><noframes id='TsmRk'>

    3. <tfoot id='TsmRk'></tfoot>

        srand(time(NULL)) 产生类似的结果

        时间:2023-05-23

          <tbody id='bjrHL'></tbody>

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

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

                <tfoot id='bjrHL'></tfoot>
                • 本文介绍了srand(time(NULL)) 产生类似的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我不明白为什么 srand() 在运行之间生成如此相似的随机数

                  I don't understand why srand() generates so similar random numbers between runs!

                  我正在尝试运行以下代码

                  I am trying to run the following code

                  srand ( time(NULL) );
                  int x = rand();
                  cout << x << endl;
                  

                  然而,我总是得到几乎相同的数字,而不是一个正确的随机数,随着时间的推移,这个数字增长缓慢.所以我得到的数字是:11669、11685、11701、11714、11731.

                  However instead of a proper random number I always end up with almost the same number, which is growing slowly as the time goes. So I get numbers like: 11669, 11685, 11701, 11714, 11731.

                  我做错了什么?

                  我使用的是 Visual Studio 2010 SP1.

                  I am using Visual Studio 2010 SP1.

                  好吧,srand() 真的那么简单吗?我的意思是怎么会有人称它为随机函数?

                  OK, is srand() really that simple? I mean how would anyone call it a random function?

                  srand(1) => rand() = 41
                  srand(2) => rand() = 45
                  srand(3) => rand() = 48
                  srand(4) => rand() = 51
                  ....
                  

                  推荐答案

                  一、srand() 不是随机函数;它设置了起点伪随机序列.有点令人惊讶的是,你的rand() 的实现似乎是返回一个基于之前的状态,而不是新计算的状态,所以第一个调用 srand() 后的值在很大程度上取决于传递给的值srand().如果你要写:

                  First, srand() isn't a random function; it sets up the starting point of a pseudo-random sequence. And somewhat surprisingly, your implementation of rand() seems to be returning a value based on the previous state, and not on the newly calculated state, so that the first value after a call to srand() depends very much on the value passed to srand(). If you were to write:

                  srand( time( NULL ) );
                  rand();
                  std::cout << rand() << std::endl;
                  

                  ,我相信你会看到更多的不同.

                  , I'm sure you'll see a lot more difference.

                  FWIW:我在 Windows 和 Linux 上都尝试了以下方法:

                  FWIW: I tried the following on both Windows and Linux:

                  int
                  main()
                  {
                      srand( time( NULL ) );
                      int r1 = rand();
                      std::cout << r1 << ' ' << rand() << std::endl;
                      return 0;
                  }
                  

                  以一秒为间隔调用 10 次,我得到:

                  Invoked 10 times at a one second interval, I got:

                  16391 14979
                  16394 25727
                  16397 3708
                  16404 25205
                  16407 3185
                  16410 13933
                  16417 2662
                  16420 13411
                  16427 2139
                  

                  在 Windows 下使用 VC++—您会注意到第一次调用 rand()—and

                  with VC++ under Windows—you'll note the very low variance of the first call to rand()—and

                  1256800221 286343522
                  955907524 101665620
                  1731118607 991002476
                  1428701871 807009391
                  44395298 1688573463
                  817243457 1506183315
                  507034261 1310184381
                  1278902902 54648487
                  2049484769 942368151
                  1749966544 1833343137
                  

                  在Windows下使用g++;在这种情况下,即使读取的第一个值也是相对随机.

                  with g++ under Windows; in this case, even the first value read is relatively random.

                  如果你需要一个好的随机生成器,你可能不得不使用一个来自 Boost;该标准并没有说明什么算法应该是使用,并且实现的质量差异很大.

                  If you need a good random generator, you'll probably have to use one from Boost; the standard doesn't say much about what algorithm should be used, and implementations have varied enormously in quality.

                  这篇关于srand(time(NULL)) 产生类似的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Visual Studio 中刷新自动完成 (IntelliSense) 数据库 下一篇:如何在 GoogleTest 中运行特定的测试用例

                  相关文章

                  最新文章

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

                  2. <small id='tqEke'></small><noframes id='tqEke'>

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