---
# System prepended metadata

title: 生日悖論
tags: [math, Public]

---

###### tags: `math` `Public`
# 生日悖論

## 命題
至少幾人的教室，有５０％概率會有其中兩個人同一天生日？

## 基本假設
- 一年365天
- 每天生日概率相等

## 解題

- 兩個人同一天生日的機率 $\frac{1}{365} \times \frac{1}{365} = 0.00000750609$

- N個人都沒有人同一天生日的機率
  1. N>=365 必定有兩個人在同一天生日
  2. N<365 $\Pi_{n=1}^N(\frac{365-n}{365}) = \frac{365}{365}\times\frac{365-1}{365}\times...\times\frac{365-N}{365}$


### 模擬程式
```golang
package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano())

	for i := 1; i <= 100; i++ {
		fmt.Println(i, ",", stat(i))
	}

}
func stat(n int) float64 {
	measure_time := 10000.0
	hit_time := 0.0
	for t := 0.0; t < measure_time; t++ {
		if once(n) {
			hit_time++
		}
	}
	return hit_time / measure_time
}

func once(n int) bool {
	m := map[int]int{}
	for i := 0; i < n; i++ {
		rng := rand.Intn(365)
		m[rng]++
		if m[rng] >= 2 {
			return true
		}
	}
	return false
}

```




### func PI(N)

$PI(N) =  \Pi_{n=1}^N(\frac{365-n}{365}) = \frac{365}{365}\times\frac{365-1}{365}\times...\times\frac{365-N}{365}$ 
```golang
func PI(n int) float64 {
	if n >= 365 {
		return 0.0
	}
	ans := 1.0
	for i := 1; i <= n; i++ {
		ans *= (365.0 - float64(i-1))
		ans /= 365.0
	}
	return ans
}
```
## 整合表格

index | 1-PI(N)  | 模擬結果 | index | 1-PI(N)  | 模擬結果
------|----------|----------|-------|----------|---------
1     | 0.000000 | 0.000000 | 51    | 0.974432 | 0.971100
2     | 0.002740 | 0.002600 | 52    | 0.978005 | 0.976800
3     | 0.008204 | 0.010300 | 53    | 0.981138 | 0.981100
4     | 0.016356 | 0.015100 | 54    | 0.983877 | 0.983700
5     | 0.027136 | 0.028400 | 55    | 0.986262 | 0.986200
6     | 0.040462 | 0.041800 | 56    | 0.988332 | 0.989900
7     | 0.056236 | 0.057200 | 57    | 0.990122 | 0.988000
8     | 0.074335 | 0.069100 | 58    | 0.991665 | 0.994400
9     | 0.094624 | 0.088400 | 59    | 0.992989 | 0.992300
10    | 0.116948 | 0.120200 | 60    | 0.994123 | 0.994100
11    | 0.141141 | 0.138500 | 61    | 0.995089 | 0.994300
12    | 0.167025 | 0.168700 | 62    | 0.995910 | 0.995500
13    | 0.194410 | 0.199300 | 63    | 0.996604 | 0.997100
14    | 0.223103 | 0.224000 | 64    | 0.997190 | 0.997200
15    | 0.252901 | 0.249900 | 65    | 0.997683 | 0.998400
16    | 0.283604 | 0.289400 | 66    | 0.998096 | 0.998000
17    | 0.315008 | 0.315500 | 67    | 0.998440 | 0.998800
18    | 0.346911 | 0.350100 | 68    | 0.998726 | 0.999200
19    | 0.379119 | 0.368700 | 69    | 0.998964 | 0.998700
20    | 0.411438 | 0.415500 | 70    | 0.999160 | 0.999300
21    | 0.443688 | 0.444900 | 71    | 0.999321 | 0.999200
22    | 0.475695 | 0.476900 | 72    | 0.999453 | 0.999700
23    | 0.507297 | 0.499800 | 73    | 0.999561 | 0.999700
24    | 0.538344 | 0.546600 | 74    | 0.999649 | 0.999700
25    | 0.568700 | 0.574800 | 75    | 0.999720 | 0.999700
26    | 0.598241 | 0.595500 | 76    | 0.999777 | 0.999700
27    | 0.626859 | 0.625500 | 77    | 0.999824 | 0.999800
28    | 0.654461 | 0.654300 | 78    | 0.999861 | 0.999900
29    | 0.680969 | 0.680900 | 79    | 0.999891 | 0.999700
30    | 0.706316 | 0.701500 | 80    | 0.999914 | 1.000000
31    | 0.730455 | 0.730200 | 81    | 0.999933 | 1.000000
32    | 0.753348 | 0.751900 | 82    | 0.999948 | 1.000000
33    | 0.774972 | 0.775300 | 83    | 0.999960 | 1.000000
34    | 0.795317 | 0.797600 | 84    | 0.999969 | 1.000000
35    | 0.814383 | 0.821400 | 85    | 0.999976 | 1.000000
36    | 0.832182 | 0.838200 | 86    | 0.999982 | 0.999900
37    | 0.848734 | 0.846700 | 87    | 0.999986 | 1.000000
38    | 0.864068 | 0.862700 | 88    | 0.999989 | 1.000000
39    | 0.878220 | 0.877300 | 89    | 0.999992 | 1.000000
40    | 0.891232 | 0.891800 | 90    | 0.999994 | 1.000000
41    | 0.903152 | 0.905100 | 91    | 0.999995 | 1.000000
42    | 0.914030 | 0.918900 | 92    | 0.999997 | 1.000000
43    | 0.923923 | 0.923300 | 93    | 0.999997 | 1.000000
44    | 0.932885 | 0.932400 | 94    | 0.999998 | 1.000000
45    | 0.940976 | 0.945300 | 95    | 0.999999 | 1.000000
46    | 0.948253 | 0.946200 | 96    | 0.999999 | 1.000000
47    | 0.954774 | 0.955300 | 97    | 0.999999 | 1.000000
48    | 0.960598 | 0.960700 | 98    | 0.999999 | 1.000000
49    | 0.965780 | 0.965100 | 99    | 1.000000 | 1.000000
50    | 0.970374 | 0.972600 | 100   | 1.000000 | 1.000000


## 結論

約23人即可滿足要求，常見國中小的班級人數已超過此數量


>感覺不能量化，但數字可以幫助我們理解～