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

  • Context: Comp Sci 
  • Thread starter Thread starter arkturus
  • Start date Start date
  • Tags Tags
    Array C++ Issues
Click For Summary

Discussion Overview

The discussion revolves around a programming task related to the Birthday Paradox, specifically focusing on writing a C++ program to estimate the probability that at least two people in a room of 50 share the same birthday. The scope includes coding, debugging, and understanding probability in a computational context.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • The initial program attempts to approximate the probability of shared birthdays by running a function that generates random birthdays and checks for coincidences.
  • One participant points out that the array size used in the program is incorrect.
  • Another participant notes that the probability should not be represented as an integer and suggests initializing the random number generator only once to avoid repeating the same sequence of random numbers.
  • A later post expresses gratitude for the corrections provided, indicating that they resolved previous issues.
  • Another participant raises a new issue regarding how to create arrays of varying sizes (from 2 to 50) and how to pass the size of the array to the function.
  • In response, a suggestion is made to look into pointers, dynamic allocation, and the use of new/delete operators for handling variable-sized arrays.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections in the initial code, but the discussion introduces new questions regarding dynamic array handling, indicating that the conversation remains unresolved on that topic.

Contextual Notes

Limitations include the initial misunderstanding of array sizes and probability representation, as well as the need for further exploration of dynamic memory management in C++.

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 17 ·
Replies
17
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
8K
  • · Replies 3 ·
Replies
3
Views
7K