• <bdo id='3HmNd'></bdo><ul id='3HmNd'></ul>
    <tfoot id='3HmNd'></tfoot>

    <small id='3HmNd'></small><noframes id='3HmNd'>

    1. <legend id='3HmNd'><style id='3HmNd'><dir id='3HmNd'><q id='3HmNd'></q></dir></style></legend>
      <i id='3HmNd'><tr id='3HmNd'><dt id='3HmNd'><q id='3HmNd'><span id='3HmNd'><b id='3HmNd'><form id='3HmNd'><ins id='3HmNd'></ins><ul id='3HmNd'></ul><sub id='3HmNd'></sub></form><legend id='3HmNd'></legend><bdo id='3HmNd'><pre id='3HmNd'><center id='3HmNd'></center></pre></bdo></b><th id='3HmNd'></th></span></q></dt></tr></i><div id='3HmNd'><tfoot id='3HmNd'></tfoot><dl id='3HmNd'><fieldset id='3HmNd'></fieldset></dl></div>
    2. 为什么 rand() 每次运行都会产生相同的数字序列

      时间:2023-10-07
      • <i id='gGhjb'><tr id='gGhjb'><dt id='gGhjb'><q id='gGhjb'><span id='gGhjb'><b id='gGhjb'><form id='gGhjb'><ins id='gGhjb'></ins><ul id='gGhjb'></ul><sub id='gGhjb'></sub></form><legend id='gGhjb'></legend><bdo id='gGhjb'><pre id='gGhjb'><center id='gGhjb'></center></pre></bdo></b><th id='gGhjb'></th></span></q></dt></tr></i><div id='gGhjb'><tfoot id='gGhjb'></tfoot><dl id='gGhjb'><fieldset id='gGhjb'></fieldset></dl></div>
          <bdo id='gGhjb'></bdo><ul id='gGhjb'></ul>

                  <tbody id='gGhjb'></tbody>
                <legend id='gGhjb'><style id='gGhjb'><dir id='gGhjb'><q id='gGhjb'></q></dir></style></legend>
              1. <small id='gGhjb'></small><noframes id='gGhjb'>

              2. <tfoot id='gGhjb'></tfoot>

                本文介绍了为什么 rand() 每次运行都会产生相同的数字序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                每次我用 rand() 运行程序时,它都会给我相同的结果.

                示例:

                #include #include 使用命名空间标准;int随机(int低,int高){如果(低>高)回报高;返回低 + (rand() % (高 - 低 + 1));}int main (int argc, char* argv []) {for (int i = 0; i <5; i++)cout<<随机 (2, 5) <<结束;}

                输出:

                35423

                每次我运行程序时,它每次都会输出相同的数字.有没有办法解决这个问题?

                解决方案

                未设置随机数生成器的种子.

                如果你调用 srand((unsigned int)time(NULL)) 那么你会得到更多的随机结果:

                #include #include #include <ctime>使用命名空间标准;int main() {srand((unsigned int)time(NULL));cout<<兰特()<<结束;返回0;}

                原因是从 rand() 函数生成的随机数实际上并不是随机的.这简直就是一种转变.维基百科对伪随机数生成器的含义给出了更好的解释:确定性随机位生成器.每次调用 rand() 时,它都会获取生成的种子和/或最后一个随机数(C 标准没有指定使用的算法,尽管 C++11 具有指定一些流行的算法),对这些数字运行数学运算,并返回结果.因此,如果种子状态每次都相同(就像您不使用真正的随机数调用 srand 一样),那么您将始终得到相同的随机"数.

                如果您想了解更多,可以阅读以下内容:

                http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

                http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

                Every time I run a program with rand() it gives me the same results.

                Example:

                #include <iostream>
                #include <cstdlib>
                
                using namespace std;
                
                int random (int low, int high) {
                    if (low > high)
                        return high;
                    return low + (rand() % (high - low + 1));
                }
                
                int main (int argc, char* argv []) {
                    for (int i = 0; i < 5; i++)
                        cout << random (2, 5) << endl;
                }
                

                Output:

                3
                5
                4
                2
                3
                

                Each time I run the program it outputs the same numbers every time. Is there a way around this?

                解决方案

                The seed for the random number generator is not set.

                If you call srand((unsigned int)time(NULL)) then you will get more random results:

                #include <iostream>
                #include <cstdlib>
                #include <ctime>
                using namespace std;
                
                int main() {
                    srand((unsigned int)time(NULL));
                    cout << rand() << endl;
                    return 0;
                }
                

                The reason is that a random number generated from the rand() function isn't actually random. It simply is a transformation. Wikipedia gives a better explanation of the meaning of pseudorandom number generator: deterministic random bit generator. Every time you call rand() it takes the seed and/or the last random number(s) generated (the C standard doesn't specify the algorithm used, though C++11 has facilities for specifying some popular algorithms), runs a mathematical operation on those numbers, and returns the result. So if the seed state is the same each time (as it is if you don't call srand with a truly random number), then you will always get the same 'random' numbers out.

                If you want to know more, you can read the following:

                http://www.dreamincode.net/forums/topic/24225-random-number-generation-102/

                http://www.dreamincode.net/forums/topic/29294-making-pseudo-random-number-generators-more-random/

                这篇关于为什么 rand() 每次运行都会产生相同的数字序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:rand() 在单个函数中调用时返回相同的值 下一篇:在 C/C++ 中生成服从正态分布的随机数

                相关文章

                最新文章

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

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

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

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