Rand() % 14 only generates the values 6 or 13

时光怂恿深爱的人放手 提交于 2020-01-18 04:44:31

问题


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?

Thank you.

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


回答1:


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.

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;
}

Test:

$ 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
$



回答2:


Per wikipedia, the multiplier being used in Apple's MCG random number generator is 16807. This is divisible by 7, so the first random number produced after srand() will have only one bit of entropy mod 14 (that is, it can only take on two values).

It's a crappy RNG they've got there. An easy solution, though, is just to call rand() a few times right after srand, and discard the results.




回答3:


rand() % 14 is often a poor random number generator. You'll probably get better results with this

(int)(14*(rand()/(RAND_MAX + 1.0)))


来源:https://stackoverflow.com/questions/20263187/rand-14-only-generates-the-values-6-or-13

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!