Program to check how many people are born on the same day of the month
Assuming there are exactly 30 days in a month, I need to write a program that considers if there are K people in a room, what is the likelihood that at least M of them were born on the same day of the month. For example, if two people are both born on the 5th day of the month, they are a match.
So if a user selected the match chance for M=7 (7 duplicates) and K=90 (90 people), what is the probability that at least 7 of them were born on the same day of the month. The probability should be computed by 10,000 trials.
I'm just really confused about how to compute this. This is what I have so far:
Code :
import javax.swing.JOptionPane;
public class BDayMTester{
public static void main(String[] args){
final int trials = 10000;
String duplicateCt = JOptionPane.showInputDialog("Enter duplicate count");
String peopleCt = JOptionPane.showInputDialog("Now enter the number of people in the room");
int dupCt = Integer.parseInt(duplicateCt);
int pplCt = Integer.parseInt(peopleCt);
BDayM b = new BDayM(pplCt, dupCt);
double total = 0.0;
double s = 0.0;
for(int p=0; p<trials; p++){
s = b.runTest();
total = total + s;
}
System.out.println(total);
}
}
Code :
public class BDayM{
private int roomFolks;
private int dupPeople;
private int match = 0;
public BDayM(int people, int duplicates){
roomFolks = people;
dupPeople = duplicates;
}
public double runTest(){
boolean[] days = new boolean[30];
for(int d = 0; d<30; d++){
days[d] = false;
}
int ranDay;
for(int p=0; p<roomFolks; p++){
ranDay = genDay();
if(days[ranDay] == true){
days[ranDay] = true;
match++;
}
else days[ranDay] = true;
}
return dupPeople/match;
}
private int genDay(){
return((int)(30*Math.random()));
}
}
Re: Program to check how many people are born on the same day of the month
Quote:
if two people are both born on the 5th day of the month
Any month or the same month only? For example, does jan 20 'match' feb 20?
Re: Program to check how many people are born on the same day of the month
yes, as long as the day is the same, the month doesn't matter
Re: Program to check how many people are born on the same day of the month
Sorry to keep posting walls of code, but I think I've almost found the solution. I don't believe I'm getting a correct average from the 10,000 trials though. Here is my new code:
Code :
import javax.swing.JOptionPane;
public class BDayMTester{
public static void main(String[] args){
final int trials = 10000;
String duplicateCt = JOptionPane.showInputDialog("Enter duplicate count");
String peopleCt = JOptionPane.showInputDialog("Now enter the number of people in the room");
int dupCt = Integer.parseInt(duplicateCt);
int pplCt = Integer.parseInt(peopleCt);
BDayM b = new BDayM(pplCt, dupCt);
double average = 0.0;
double s = 0.0;
for(int j=0; j<trials; j++){
s = b.runTest();
average = average + s;
}
System.out.println((double)average/trials);
}
}
Code :
public class BDayM{
private int roomFolks;
private int dupPeople;
private int match = 0;
private int s = 0;
public BDayM(int people, int duplicates){
roomFolks = people;
dupPeople = duplicates;
}
public double runTest(){
int[] count = new int[30];
for(int j = 0; j<roomFolks; j++){
s = genDay();
count[s]++;
}
for(int n=0; n<30; n++){
if(count[n] > 1){
match++;
}
}
return (double)dupPeople/match;
}
private int genDay(){
return((int)(30*Math.random()));
}
}
With an input of 7 matches and 90 people, I'm getting an answer like 2.81458828680363E-4 when it should be something like 0.655565
EDIT: I think it's because I'm only including if there are 7 matches, and 7 matches alone in the group of people when I should be considering if there are 7 or MORE matches in the group.
Re: Program to check how many people are born on the same day of the month
Code :
.....
for(int n=0; n<30; n++){
if(count[n] > 1){
match++;
}
......
What exactly does the above code do? Is that what you meant for it to do?
Is the return value from the runTest() method a good value each run?
Try a println on dupPeople, match, and dupPeople/match before the return of runTest() and see what the values look like. Perhaps a fewer number of tests for this...
Re: Program to check how many people are born on the same day of the month
Quote:
Originally Posted by
ksahakian21
...
EDIT: I think it's because I'm only including if there are 7 matches
It's worse than that...
Let's go back and look at the problem from a higher point of view.
Here's the Big Picture for things like your assignment:
- You define an "experiment." The outcome of the experiment is either "true" or "false."
- You run the experiment N times, and count the number of successful ("true") outcomes.
- The probability of "success" is approximately equal to the number of "true" results divided by N.
In your case "success" is defined to be "true" if there are seven or more people in the room who celebrate birthdays on the same monthday (subject to your assumption that there are exactly 30 days in every month.)
So, looking at your BDayM class:
In the BDayM class, create a boolean function. Maybe call it runTest() or some such thing. You are going to run this test a bunch of times and count the number of times it returns "true."
In general, there will be a certain number of people and a certain "match point" integer value that we will use to define whether the outcome of the test is true or false.
Using your example numbers of 90 people and a "match point" of 7, here's the way one test of the experiment would run:
Code :
Declare an array of 30 integer counters.
All counters are initially zero.
Go through a loop that generates 90 random
numbers with value 0..29.
Each time through the loop, increment the
counter element corresponding to the
particular value of the random number.
(You have pretty much done all of the above, right?)
Then here's the Good Stuff:
Make another loop.
Go through the array of counters. If you find a
counter whose value is greater than or equal to 7,
the test is "success," so you return "true" at that
point.
There is no need to continue counting, and there is
no purpose of keeping track of the total number
of matches or anything else.
If, on the other hand, you get through the loop that
has inspected counter values and have not found any
one that is greater than or equal to 7, then
return "false."
Now, in main():
Code :
Create the BDayM class with a particular number
of people and a particular value of match point.
Declare an integer that will keep track of the
number of times the test returns "true."
Initialize it to zero.
Declare an int that will be the number of
times that you run the test. Call it N.
Make a loop that does the following:
Run that test N times.
That is, each time through the loop
run the test. If it returns "true" increment the
"success" counter.
Then the probability you are seeking is approximately
equal to the number of "true" outcomes divided by the
total number of tests. (Cast to floating point before
dividing integers.)
Cheers,
Z
Re: Program to check how many people are born on the same day of the month
I did eventual figure this out after getting my head wrapped around it.
Code :
import javax.swing.JOptionPane;
public class BDayMTester{
public static void main(String[] args){
final int trials = 10000;
String duplicateCt = JOptionPane.showInputDialog("Enter duplicate count");
String peopleCt = JOptionPane.showInputDialog("Now enter the number of people in the room");
int dupCt = Integer.parseInt(duplicateCt);
int pplCt = Integer.parseInt(peopleCt);
// Creates a new BDayM object with user inputed values
BDayM b = new BDayM(pplCt, dupCt);
int match = 0;
// Goes through the set number of trials determining
// how many true outcomes occur.
for(int j=0; j<trials; j++){
if(b.runTest() == true){
match++;
}
}
double toPercent = ((double)match/trials)*100;
System.out.println("Match chance for " + pplCt + " people: looking for " + dupCt + " duplicates");
System.out.println((double)match/trials);
System.out.println(toPercent + "%");
}
}
Code :
public class BDayM{
private int roomFolks;
private int dupPeople;
private int s = 0;
public BDayM(int people, int duplicates){
roomFolks = people;
dupPeople = duplicates;
}
public boolean runTest(){
int[] count = new int[30]; // array to hold each possible day
// This for loop generates a random day of the month for each person
// then applies that day to the counter array which keeps track of
// how many birthdays are on each day.
for(int j = 0; j<roomFolks; j++){
s = genDay();
count[s]++;
}
// This for loop goes through each counter array index and checks if that
// index meets the condition of being at least the value dupPeople (which
// is inputed by the user in the BDayTester class).
for(int n=0; n<30; n++){
if(count[n] >= dupPeople){
return true; // If the condition is met, runTest() returns true
}
}
return false; //otherwise runTest() will retun false
}
// Calculates a random day 0-29 for use in the runTest() method
private int genDay(){
return((int)(30*Math.random()));
}
}