-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBirthdayProblem.java
More file actions
38 lines (32 loc) · 1.36 KB
/
BirthdayProblem.java
File metadata and controls
38 lines (32 loc) · 1.36 KB
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
/*
* Simulate choosing people at random and checking the day of the year they
* were born on. if the birthday is the same as one that was seen previously,
* stop, and output the number of people who were checked
*/
public class BirthdayProblem{
public static void main(String[] args){
boolean[] used; //for recording the possiable birthdays
//that hava been seen so far. A value
//of true in used[i] means that a person
// whose birthday is the i-th day of the
// year has been found
int count; //the number of people who have been checked
used = new boolean[365]; // Initially, all entries are false.
count = 0;
while (true) {
//select a birthday at random, from 0 to 365.
//if the birthday has already been used quit.
//Otherwise, record the birthday as used.
int birthday;
birthday = (int)(Math.random()*365);
count++;
System.out.printf("Person %d has birthday number %d%n", count, birthday);
if ( used[birthday]) {
break;
}
used[birthday ] = true;
} //
System.out.println();
System.out.println("A duplicate birthday was found after " + count + " tries.");
}
}