Problem with probability

I ran into a problem using rand.
1
2
3
4
5
6
7
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,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdlib>
#include <ctime>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <ctime>
#include <time.h> 
#include <string>
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.
1
2
3
4
5
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.