• <small id='vI2cn'></small><noframes id='vI2cn'>

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

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

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

        <tfoot id='vI2cn'></tfoot>
      1. rand() 生成相同的数字——即使在我的主要内容中

        时间:2023-10-07
        • <small id='GVcaJ'></small><noframes id='GVcaJ'>

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

            <tfoot id='GVcaJ'></tfoot>
              <tbody id='GVcaJ'></tbody>

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

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

                  本文介绍了rand() 生成相同的数字——即使在我的主要内容中有 srand(time(NULL)) !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以,我正在尝试创建一个随机向量(想想几何图形,而不是一个可扩展的数组),每次我调用随机向量函数时,我都会得到相同的 x 值,尽管 y 和 z 不同.

                  So, I'm trying to create a random vector (think geometry, not an expandable array), and every time I call my random vector function I get the same x value, though y and z are different.

                  int main () {
                      srand ( (unsigned)time(NULL));
                      Vector<double> a;
                      a.randvec();
                      cout << a << endl;
                      return 0;
                  }
                  

                  使用函数

                  //random Vector
                  template <class T>
                  void Vector<T>::randvec()
                  {
                      const int min=-10, max=10;
                      int randx, randy, randz;
                  
                      const int bucket_size = RAND_MAX/(max-min);
                  
                      do randx = (rand()/bucket_size)+min;
                      while (randx <= min && randx >= max);
                      x = randx;
                  
                      do randy = (rand()/bucket_size)+min;
                      while (randy <= min && randy >= max);
                      y = randy;
                  
                      do randz = (rand()/bucket_size)+min;
                      while (randz <= min && randz >= max);
                      z = randz;
                  }
                  

                  出于某种原因,randx 将始终返回 8,而其他数字似乎完全遵循(伪)随机性.但是,如果我在 randx 之前调用定义,比如兰迪,兰迪将始终返回 8.

                  For some reason, randx will consistently return 8, whereas the other numbers seem to be following the (pseudo) randomness perfectly. However, if I put the call to define, say, randy before randx, randy will always return 8.

                  为什么我的第一个随机数总是 8?是我播种不正确吗?

                  Why is my first random number always 8? Am I seeding incorrectly?

                  推荐答案

                  问题是随机数生成器的种子值非常接近 - 程序的每次运行只会改变时间的返回值() 少量 - 可能是 1 秒,甚至可能没有!相当差的标准随机数生成器然后使用这些相似的种子值来生成明显相同的初始随机数.基本上,您需要一个比 time() 更好的初始种子生成器和一个比 rand() 更好的随机数生成器.

                  The issue is that the random number generator is being seeded with a values that are very close together - each run of the program only changes the return value of time() by a small amount - maybe 1 second, maybe even none! The rather poor standard random number generator then uses these similar seed values to generate apparently identical initial random numbers. Basically, you need a better initial seed generator than time() and a better random number generator than rand().

                  我认为实际使用的循环算法是从 Accelerated C++ 中提取的,旨在在所需范围内产生更好的数字分布,而不是使用 mod 运算符.但它无法弥补总是(有效地)给予相同的种子.

                  The actual looping algorithm used is I think lifted from Accelerated C++ and is intended to produce a better spread of numbers over the required range than say using the mod operator would. But it can't compensate for always being (effectively) given the same seed.

                  这篇关于rand() 生成相同的数字——即使在我的主要内容中有 srand(time(NULL)) !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:每次运行程序时都是相同的随机数 下一篇:在 C++ 应用程序中,我应该多久调用一次 srand()

                  相关文章

                  最新文章

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

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

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