Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
30 lines (28 loc) · 1.17 KB

File metadata and controls

30 lines (28 loc) · 1.17 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package ch11;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
public class BirthdayProblemExample {
public static void main(String[] args) {
// 같은 날 횟수를 기록하는 변수
int sameBirthdays = 0;
// 10만 번 실험 진행
for (int i = 0; i < 100000; i++) {
int[] birthdays = new int[23];
// 23명이 모이는 경우
for (int j = 0; j < 23; j++) {
// 365일 중 랜덤하게 지정
int birthday = ThreadLocalRandom.current().nextInt(1, 365 + 1);
// 만약 같은 날이 존재한다면 일치 여부 +1 이후 다음 실험 진행
if (IntStream.of(birthdays).anyMatch(x -> x == birthday)) {
sameBirthdays++;
break;
}
// 일치하지 않은 경우 저장하고 다음 사람으로
birthdays[j] = birthday;
}
}
System.out.print("10만 번 실험 중 생일이 같은 실험의 확률: ");
System.out.print((double) sameBirthdays / 100000 * 100);
System.out.println("%");
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.