import java.awt.*; import java.applet.*; import java.util.*; import java.awt.event.*; import java.lang.String; public class BirthdayProb extends Applet { Button Run, Reset; Image backImg, offScreenImg; Graphics offScreenG; int randomBirthDate; int numPeople, numTrials; int numDateMatchesFound; int oneDateMatch, atleastOneDateMatch, atleastTwoDateMatch; boolean showProb; TextField people, trials; int dates[] = new int[365]; public void init() { backImg = getImage(getCodeBase(),"back.gif"); offScreenImg = createImage(this.size().width, this.size().height); offScreenG = offScreenImg.getGraphics(); repaint(); randomBirthDate = 0; numPeople = numTrials = 0; numDateMatchesFound = 0; oneDateMatch = atleastOneDateMatch = atleastTwoDateMatch = 0; setLayout(null); Run = new Button("Find Probabilities"); Reset = new Button("Reset"); Run.setBounds(350, 70, 110, 25); Reset.setBounds(350, 110, 110, 25); add(Run); add(Reset); people = new TextField(4); trials = new TextField(7); trials.setBounds(165,70,75,30); people.setBounds(165,110,75,30); add(people); add(trials); people.setText("10"); trials.setText("50000"); showProb = false; for (int i = 0; i < 365; i++) dates[i] = 0; } public void paint (Graphics g) { offScreenG.drawImage(backImg,0,0,this); show(offScreenG); g.drawImage(offScreenImg,0,0,this); } public void findProb() { oneDateMatch = atleastOneDateMatch = atleastTwoDateMatch = 0; numPeople = Integer.parseInt(people.getText()); numTrials = Integer.parseInt(trials.getText()); for (int i = 0; i < numTrials; i++) { for (int j = 0; j < numPeople; j++) { randomBirthDate = (int) (Math.random() * 365); dates[randomBirthDate]++; } for (int j = 0; j < 365; j++) if (dates[j] >= 2) numDateMatchesFound++; if (numDateMatchesFound == 1) oneDateMatch++; if (numDateMatchesFound >= 1) atleastOneDateMatch++; if (numDateMatchesFound >= 2) atleastTwoDateMatch++; numDateMatchesFound = 0; for (int k = 0; k < 365; k++) dates[k] = 0; } showProb = true; repaint(); } public double actual() { double ans = 1; for(int i = 365; i >= (366 - numPeople); i--) ans = ans*i; ans = ans/(Math.pow(365, numPeople)); ans = 1 - ans; for (int i = 2; i <= numPeople; i++) { double comb = 1; for (int j = numPeople; j > i; j--) comb = comb*j; for (int k = 2; k <= (numPeople - i); k++) comb = comb/k; for (int l = 365; l >= (365 - numPeople + i); l--) comb = comb*l; comb = comb/(Math.pow(365, numPeople)); ans = ans - comb; } return ans*100; // answer: P(Multiple Birthdays >= 2) = 1 - (365) (364) (363) (362) *** (365 - numPeople + 1)/365^numPeople // - The Sum For q=2 to numPeople (C(numPeople, q) (365) (364) (363) (362) *** (365 - numPeople + q) / 365^numPeople) // // This equation is explained on the webpage along with this applet } public float round(float x) { x *= 10000; x = (int)x; x = x/10000; return x; } public double round(double x) { x *= 10000; x = (int)x; x = x/10000; return x; } public void reset() { people.setText("10"); trials.setText("50000"); showProb = false; repaint(); } public void update (Graphics g) { paint(g); } public boolean mouseDown(Event evt, int x, int y) { return true; } public void show(Graphics offScreenG) { Font f = new Font("Comic Sans MS", Font.BOLD, 15); offScreenG.setFont(f); offScreenG.drawString("Number of trials:", 25, 90); offScreenG.drawString("Number of people: ", 25, 130); if (showProb == true) { offScreenG.setColor(Color.blue); offScreenG.drawString("" + round(((float)oneDateMatch/numTrials)*100) + "%" + " of your trials resulted in exactly one duplicated birthday.", 25, 230); offScreenG.setColor(Color.red); offScreenG.drawString("" + round(((float)atleastOneDateMatch/numTrials)*100) + "%" + " of your trials resulted in atleast one duplicated birthday.", 25, 260); offScreenG.setColor(Color.blue); offScreenG.drawString("" + round(((float)atleastTwoDateMatch/numTrials)*100) + "%" + " of your trials resulted in atleast two duplicated birthdays.", 25, 290); offScreenG.drawString("Mathematical Probability: " + round(actual()) + "%", 25, 310); offScreenG.setColor(Color.black); } } public boolean action(Event evt, Object arg) { if (arg.equals("Find Probabilities")) findProb(); if (arg.equals("Reset")) reset(); return true; } }