-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBirthday.java
More file actions
27 lines (27 loc) · 896 Bytes
/
Copy pathBirthday.java
File metadata and controls
27 lines (27 loc) · 896 Bytes
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
public class Birthday {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int trials = Integer.parseInt(args[1]);
int cumulative_sum = 0;
int[] count = new int[n + 2];
for (int i = 0; i < trials; i++) {
boolean[] birthdays = new boolean[n];
int c = 0;
while (true) {
int r = (int) (Math.random() * (n));
c++;
if (birthdays[r]) {
count[c]++;
break;
}
birthdays[r] = true;
}
}
for (int i = 1; i < n + 2; i++) {
cumulative_sum += count[i];
double fraction = (double) cumulative_sum / trials;
System.out.println(i + "\t" + count[i] + "\t" + fraction);
if (fraction >= 0.5) break;
}
}
}