Average People Entered Before Birthday Match in Room

  • Thread starter Thread starter magnifik
  • Start date Start date
  • Tags Tags
    Probability
AI Thread Summary
The discussion revolves around calculating the average number of people entering a room before a birthday match occurs. The initial approach involved using two arrays to store the birthdays of 100 people each, but the code needed adjustments for accuracy and efficiency. Suggestions included consolidating array initialization into a single loop and ensuring the function correctly returns the number of iterations until a match is found. Additionally, it was emphasized that to compute an average, the return values from multiple function calls should be accumulated and divided properly to avoid integer division issues. The randomness of the results was acknowledged, with a note that variations are expected due to the nature of random number generation.
magnifik
Messages
350
Reaction score
0
suppose that people enter an empty room one by one until a pair of people share a birthday. on average, how many people will enter before there is a match?

my approach was to have 200 people entering; the birthdays of the first 100 people were stored into one array while the second 100 people's birthdays were stored in a different array. then i compared these arrays until they had the same value. my problems are -- 1) I'm not sure if my code is right for the approach i have, 2) i want to get an average so would the best way be move what i already have inside another for loop, 3) should the value be the same every time i execute it?

Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int figure(int s[], int p[]);

int main(){
	const int MAX_SIZE = 100;
	int p[MAX_SIZE] = {0};
	int s[MAX_SIZE] = {0};
	srand(time(0));
	for (int i = 0; i < MAX_SIZE; i++){
		s[i] = rand()%364;
	}
	for (int k = 0; k < MAX_SIZE; k++){
		p[k] = rand()%364;
	}
	cout << figure(s,p);
}

int figure(int s[], int p[]){
	for (int j = 0; j < 100; j++){
		if (s[j] == p[j])
			return j;
			break;
	}
}
 
Physics news on Phys.org
magnifik said:
suppose that people enter an empty room one by one until a pair of people share a birthday. on average, how many people will enter before there is a match?

my approach was to have 200 people entering; the birthdays of the first 100 people were stored into one array while the second 100 people's birthdays were stored in a different array. then i compared these arrays until they had the same value. my problems are -- 1) I'm not sure if my code is right for the approach i have, 2) i want to get an average so would the best way be move what i already have inside another for loop, 3) should the value be the same every time i execute it?
1) Your approach is a little off, but not too far off, but it only does one calculation.
2) To get an average, keep a counter variable in main, and call your figure function inside a loop that runs some number of iterations.
3) Not sure what you're asking here.
magnifik said:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int figure(int s[], int p[]);

int main(){
	const int MAX_SIZE = 100;
	int p[MAX_SIZE] = {0};
	int s[MAX_SIZE] = {0};
	srand(time(0));
	for (int i = 0; i < MAX_SIZE; i++){
		s[i] = rand()%364;
	}
	for (int k = 0; k < MAX_SIZE; k++){
		p[k] = rand()%364;
	}
	cout << figure(s,p);
}

int figure(int s[], int p[]){
	for (int j = 0; j < 100; j++){
		if (s[j] == p[j])
			return j;
			break;
	}
}

Other comments:
1) Make your MAX_SIZE constant global so you can use it in figure(), instead of hard-coding 100 in your loop.
2) Why use two loops to initialize your two arrays? You can do both in one loop, like this:
Code:
for (int i = 0; i < MAX_SIZE; i++)
{
   s[i] = rand()%364;
   p[i] = rand()%364;
}
I favor loop and other braces on their own lines (which is something of a religious preference, but makes it easier IMO to make sure they match), and much less indentation.
3) Your random numbers will be in the range 0...363. You want them in the range 1 ... 365. If you have rand()%365, the range will be 0...364. Someone showed you how to do this in another thread recently.
 
i tried to run figure in a loop, but i get 0 as the answer. what else do i need to fix?

Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int figure(int s[], int p[]);
const int MAX_SIZE = 1000;

int main(){
	int p[MAX_SIZE] = {0};
	int s[MAX_SIZE] = {0};
	srand(time(0));
	for (int i = 0; i < MAX_SIZE; i++){
		s[i] = rand()%364;
	}
	for (int k = 0; k < MAX_SIZE; k++){
		p[k] = rand()%364;
	}
	for (int j = 0; j < MAX_SIZE; j++){
		figure(s,p);
	}
	cout << figure(s,p)/MAX_SIZE;
}

int figure(int s[], int p[]){
	for (int j = 0; j < MAX_SIZE; j++){
		if (s[j] == p[j])
			return j;
			break;
	}
}
 
You haven't addressed some of the points I mentioned in my previous mail, some of which will cause incorrect output. Please take another look at what I wrote before.

1. You are calling figure() as if it were a void function. In this loop
Code:
for (int j = 0; j < MAX_SIZE; j++){
   figure(s,p);
}
you call figure() MAX_SIZE times, but each time you are essentially discarding the return value. As I said before, you need to have a variable, say total, that you increment with the calculated value, like this.
Code:
total += figure(s, p);
Be sure you initialize total to 0.

2. The expression in this statement
Code:
cout << figure(s,p)/MAX_SIZE;
will be zero if figure(s, p) is less than MAX_SIZE for the same reason that 4/3 == 1 and 5/6 == 0 in C and C++. There are two kinds of division--integer division and floating point division. If you want the result to be floating point division, make at least one of the expressions involved float or double, using a cast.
 
hmm.. i am getting a different number every time.
 
What do you mean, "every time?" Each iteration of the loop that calls figure() or each time you run the program? You're dealing with (pseudo)random numbers, so some variation should be expected.
 
With 30 people, you have a probability of more than 50% that two or more of them share their birthday.
If your program shows a significantly different figure, it must be wrong.
 

Similar threads

Back
Top