### Problem with probability

I ran into a problem using rand.
 ``1234567`` ``````int Prob (int num) {int Cnc; Cnc=(rand() % MAX)+1; if (Cnc <= num) {return 1;} else {return 0;}}``````

When MAX is 10000 and num is 5000, the experimental probability of the function returning 1 is almost always between 0.55 and 0.53, even though it should be very close to 0.5 when one trial consists of running it 10000 times. I really don't understand why this is happening. I probably won't be using the function enough for it to actually make a difference, but it still bothers me for some reason. Any help would be appreciated.
rand() is bad, not not that bad. Do you have a complete example?

For example,
 ``123456789101112131415161718192021`` ``````#include #include #include const int MAX = 10000; int Prob (int num) { int Cnc = (std::rand() % MAX)+1; if (Cnc <= num) return 1; else return 0; } int main() { std::srand(std::time(NULL)); double positives = 0 ; for(int n = 0; n < 10000; ++n) positives += Prob(5000); std::cout << "positives/MAX = " << positives / MAX << '\n'; }``````

run online: http://ideone.com/K9ZnPE
Here's the complete code
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include #include #include using namespace std; #define MAX 10000 int Prob (int num) {int Cnc; Cnc=(rand() % MAX)+1; if (Cnc <= num) {return 1;} else {return 0;}} float ent; string rep ("y"); void main () { float Acount=0; float Bcount=0; srand(time(NULL)); int n=MAX; int won; cin >> ent; while (rep=="y") { while (n>0){ won = Prob(ent); if (won==1) {cout << "1" << endl; Acount++;} else {cout << "0" << endl; Bcount++;} n--;} cout << "---------------------------------" << endl; cout << "Value entered: " << ent << endl; cout << "Number of 1s: " << Acount << endl; cout << "Number of 0s: " << Bcount << endl; cout << "Theoretical Probability: " << ent/MAX << endl; cout << "Experimental Probability: " << Acount/MAX << endl; cout << "---------------------------------" << endl; cout << "Repeat?" << endl; cin >> rep; n=MAX; Acount = 0; Bcount = 0; }}``````
I see what the problem is, your OS must be Windows (otherwise how would you compile "void main"?), and RAND_MAX there is only 32767.
Instead of `rand() % MAX`, try `(double)rand() / RAND_MAX * MAX` or C++ random number machinery (`std::uniform_int_distribution` etc)
Last edited on
If you are are using Visual C++ RAND_MAX is 32767.

There are 3*5000 values of rand() in the range 1-30000 that will make Prob return 1, plus 2768 values in the range 30001-32768. That makes a total of 17768 values of 32768 possible. 17768/32768 ≈ 54%

The problem is the values in the range 30001-32768. What you can do to fix this is to not use values in that range. Just keep generate random numbers until you get a number in the range 1-30000.
 ``12345`` ``````int Cnc; do { Cnc=(rand() % MAX)+1; } while (Cnc > RAND_MAX - RAND_MAX % (2*num));``````

Last edited on
Yeah I'm using Win7 and VC++ 2008.
Thanks Cubbi and Peter, that solved my problem. I never would have guessed that that was causing the extra 4%.
Last edited on
Topic archived. No new replies allowed.