Below is the syntax highlighted version of Birthday.java
from §1.4 Arrays.
/****************************************************************************** * Compilation: javac Birthday.java * Execution: java Birthday days * * Computes the number of people (by simulation) that must enter a room * until two of them share a birthday. Assumes birthdays are uniform * and independent from 0 to days-1. * * When n = 365, the expected number is 24.61659. This is slightly more * than 23, which is the minimum number of people required for there to * be a >= 50% chance of two (or more) having the same birthday. * * % java Birthday 365 * 25 * * % java Birthday 365 * 22 * ******************************************************************************/ public class Birthday { public static void main(String[] args) { int days = Integer.parseInt(args[0]); // number of days int people = 0; // total number of people // hasBirthday[d] = true if someone born on day d; false otherwise // auto-initialized to false boolean[] hasBirthday = new boolean[days]; while (true) { people++; int d = (int) (days * Math.random()); // integer between 0 and days-1 if (hasBirthday[d]) break; // two people with the same birthday, so break out of loop hasBirthday[d] = true; // update array } System.out.println(people); } }