<tfoot id='8JfZL'></tfoot>

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

  2. <small id='8JfZL'></small><noframes id='8JfZL'>

    • <bdo id='8JfZL'></bdo><ul id='8JfZL'></ul>

      Rand() % 14 只生成值 6 或 13

      时间:2023-06-05
      <tfoot id='DdebA'></tfoot>
          <tbody id='DdebA'></tbody>

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

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

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

                本文介绍了Rand() % 14 只生成值 6 或 13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                每当我运行以下程序时,返回的值总是 6 或 13.

                Whenever I run the following program the returned values are always 6 or 13.

                #include <iostream>
                #include <fstream>
                #include <ctime>
                #include <cstdlib>
                using namespace std;
                
                //void randomLegs();
                //void randomPush();
                //void randomPull();
                //void randomMisc();
                
                
                int main(int argc, const char * argv[])
                {
                    srand(time(NULL));
                    //randomLegs();
                    cout << rand() % 14;
                    return 0;
                }
                

                今天和昨天我已经运行了将近一百次这个程序.

                I have run the program close to a hundred times during today and yesterday.

                谁能告诉我我做错了什么?

                Can anyone tell me what I'm doing wrong?

                谢谢.

                顺便说一下,如果我将 rand() 的范围更改为 13 或 15,它就可以正常工作.

                By the way, if I change the range of rand() to say 13 or 15 it works just fine.

                推荐答案

                我可以使用 Xcode 5 在 Mac OS X 10.9 上重现该问题 - 看起来它实际上可能是一个错误,或者至少是 的限制rand()/srand() 在 OS X 10.9 上.

                I can reproduce the problem on Mac OS X 10.9 with Xcode 5 - it looks like it might actually be a bug, or at least a limitation with rand()/srand() on OS X 10.9.

                我建议您使用 arc4random()相反,它比 rand() 效果更好,并且不需要随机化种子:

                I recommend you use arc4random() instead, which works a lot better than rand(), and which doesn't require that you randomize the seed:

                #include <iostream>
                #include <cstdlib>
                
                using namespace std;
                
                int main(int argc, const char * argv[])
                {
                    cout << (arc4random() % 14) << endl;
                    return 0;
                }
                

                测试:

                $ g++ -Wall -O3 srand.cpp && ./a.out
                5
                $ ./a.out
                8
                $ ./a.out
                0
                $ ./a.out
                8
                $ ./a.out
                11
                $ ./a.out
                8
                $ ./a.out
                3
                $ ./a.out
                13
                $ ./a.out
                9
                $
                

                这篇关于Rand() % 14 只生成值 6 或 13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 C++ 中创建一个随机的字母数字字符串? 下一篇:在 C++ 中生成随机双数

                相关文章

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

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

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

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