Average People Entered Before Birthday Match in Room

  • Thread starter magnifik
  • Start date
  • Tags
    Probability
In summary: endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;endif;
  • #1
magnifik
360
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
  • #2
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.
 
  • #3
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;
	}
}
 
  • #4
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.
 
  • #5
hmm.. i am getting a different number every time.
 
  • #6
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.
 
  • #7
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.
 

1. What is the purpose of studying average people entered before birthday match in a room?

The purpose of studying average people entered before birthday match in a room is to understand the probability of two or more people sharing the same birthday in a group of individuals. This can help in predicting the likelihood of common birthdays in a population and has applications in fields such as statistics, sociology, and probability theory.

2. How is the average number of people entered before a birthday match calculated?

The average number of people entered before a birthday match is calculated using the Birthday Problem formula, which takes into account the number of people in the room and the number of possible birthdays. This formula is 1 - (365!/[(365-n)!*365^n]), where n is the number of people in the room.

3. What factors can affect the average number of people entered before a birthday match?

The average number of people entered before a birthday match can be affected by the number of people in the room, the number of possible birthdays, and the distribution of birthdays among the individuals. Other factors such as cultural, geographic, and social factors may also play a role in the likelihood of a birthday match.

4. How does the birthday paradox relate to the average people entered before a birthday match in a room?

The birthday paradox is the phenomenon where in a group of 23 people, there is a 50% chance that two people will share the same birthday. This is counterintuitive as many people assume a larger group is needed for this probability. The average people entered before a birthday match in a room is closely related to the birthday paradox as it calculates the average number of people needed for a birthday match to occur in a given group size.

5. What are some real-world applications of studying average people entered before a birthday match in a room?

Studying average people entered before a birthday match in a room has applications in fields such as cryptography, where it is used to assess the strength of encryption methods based on birthdays. It can also be used in marketing and business strategies to predict customer behavior and preferences. Additionally, it has implications in event planning and scheduling to avoid potential conflicts in shared birthdays.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
884
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top