1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
A Birthday Puzzle Variant The birthday puzzle is a famous quirk of elementary probability theory that goes like this: How many people need to be in a room (people chosen at random) before the likelihood that at least two have the same birthday? (Ignore leap years here: a year is 365 days long for the purposes of this problem). While the naive answer might be 183, it turns out that the actual answer is 23. The attached code solves this problem, and in fact solves the more general problem: if there are K people in a room, where K is any positive integer, what is the probability that at least two of the K people present have the same birthday. /* Given any number of people in a room (people ct) * what is the experimental probability that two people * have the same birthday? Code runs ten-thousand experiments */ import javax.swing.JOptionPane; public class BDayTester{ public static void main(String[] args){ final int trials = 10000; String s = JOptionPane.showInputDialog("Enter people ct"); int count = Integer.parseInt(s); BDay b = new BDay(count); int match = 0; for (int j = 0; j < trials; j++) if (b.runTest() == true) match++; System.out.print("match chance for " + count + " people: "); System.out.println((double)match / trials); } } public class BDay{ private int roomFolks; // people count in room public BDay(int people){roomFolks = people;} public boolean runTest(){ boolean[] days = new boolean[365]; // boolean scoreboard for(int d = 0; d < 365; d++) days[d] = false; // all false int ranDay; for(int p = 0; p < roomFolks; p++){ ranDay = genDay(); // generate a random day // if ranDay location already true, then there's a match if (days[ranDay] == true) return true; // if ranDay is false then set to true else days[ranDay] = true; } return false; } private int genDay(){ // no leap year - just gen a random day 0-364 return((int)(365*Math.random())); } } Your job for this assignment is to write a two-class java program that solves the following variant of the birthday puzzle. Suppose first that a month has exactly 30 days. If there are K people in a room (any positive integral K, K provided interactively), what is the experimental likelihood that at least M of them (any positive integral M, M provided interactively) were born on the same day of the month? This means that if Jack was born on December 17, and Jill on May 17, then they qualify for a match. Here is output from my solution: > run BDayMTester Enter duplicate count Ok Cancel Now enter the number of people in the room Ok Cancel match chance for 90 people: looking for 7 duplicates 0.655565 That is, if there are 90 people in a room, the probability that at least 7 of them were born on the same day of the month is (experimentally) .655565. Your two classes should be called BDayMTester and BDayM. Use JOptionPane to obtain inputs. However for output you MUST use System.out.print or System.out.println statements. As usual, do NOT include any import statements with the classes you submit. Comment your code (roughly in the way that I've commented BDay and BDayTester, above). Base your results, as I have above, on 10,000 trials. |