Comp Sci C++ Probability of Birthday Paradox for Room of 50 People

  • Thread starter Thread starter arkturus
  • Start date Start date
  • Tags Tags
    Array C++ Issues
AI Thread Summary
The discussion focuses on a C++ program designed to approximate the probability of at least two people sharing a birthday in a room of 50. The program runs 5,000 trials, counting coincidences of birthdays and calculating the probability based on the results. Key issues identified include incorrect array size, the need for probability to be a floating-point number, and the initialization of the random number generator to avoid repeated sequences. Additionally, there is a query about how to adjust the program to handle varying group sizes from 2 to 50 people, with suggestions to explore pointers and dynamic memory allocation for implementation. The conversation emphasizes the importance of accurate probability calculations and proper coding practices.
arkturus
Messages
27
Reaction score
0

Homework Statement


Write a program that approximates the probability that at least two people in a room of 50 people have the same birthday. Run 5,000 trials, count up the number at least two people have the same birthday, and divide by the number of trials to get a probability.


Homework Equations





The Attempt at a Solution



Code:
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int bDayParadox (int people[]);

///////////////////////////////////////////////////

int main ()

{
  
  int probability;
  int sum = 0;
  const int trials = 5000;
  int people[49];
 
 for (int j = 0; j < trials; j++)
   {
     sum = sum + bDayParadox(people); 
   }
 
 probability = (sum / trials);

 cout <<  bDayParadox(people) << endl;
 cout << sum << endl;
 cout << trials << endl;
 cout << probability << endl;
   
  


}

/////////////////////////////////////////////////

int bDayParadox (int people[])
{

  int coincidence = 0;
  srand(time(NULL)); //generates seed for random numbers

  for (int a = 0; a < 50; a++)
      {
       people[a] = rand() % 365 + 1;
      }

  for (int b = 0; b < 50; b++)
      {
        for (int c = b+1; c < 50; c++)
	  { 
	   if (people[c] == people[b])
	     {     
	       coincidence = 1;
	     }


	   }
      }
  return coincidence;
 
}

The function I made, bDayParadox, puts random numbers (from 1 - 365) into an array of 50. The function then checks to see if at least two of the values are similar, and if so, counts it as 1 (coincidence variable). Up top, I'm running the function 5000 times and adding the values together to get sum. Sum divided by trials should give me the probability.

My output looks like this:

1 (bDayParadox run once)
5000 (sum)
5000 (trials, the constant)
353 (probability)

The value that is spit out for probability is unique each time.

My main problem with this is that probability should not be what it says. Also, my sum shouldn't always be 5000.
 
Physics news on Phys.org
1. Correct your array size.
2. Probability is not an integer.
3. Initialize the random number generator once. You are getting the same sequence over and over again.
 
Thank you very much. That cleared up my issues perfectly.
 
I've come apon another small issue.

I want to make arrays of different sizes, from 2 - 50. In int main(), there is something like this:

Code:
for (numPeople = 2; numPeople <= 50, numPeople++)
              bDayParadox(people)

In the function definition of bDayParadox, I have:

Code:
for (int a = 0; a < numPeople; a++)
      {
       people[a] = rand() % 365 + 1;
      }

How do I go about getting the numPeople number from the int main() for loop into my function definition? Apparently I'm doing something wrong.
 
Look up pointers, dynamical allocation and operators new/delete.
 

Similar threads

Replies
3
Views
2K
Replies
7
Views
2K
Replies
7
Views
7K
Replies
4
Views
1K
Replies
14
Views
4K
Back
Top