生日悖论(birthday paradox)是指在不少于 23 个人中至少有两人生日相同的概率大于 50%。
用两千次的随机数据模拟证明这一结论:
clear all;
clc;
simTimes = 2000;
x = 0.5;
for n = 2:365
count = 0;
i = 0;
while i<simTimes
N = unidrnd(365,1,n);
N = sort(N);
dN = diff(N);
same = find(dN==0);
if length(same)
count = count + 1;
end
i = i+1;
end
pro = count/simTimes; %the probability of two people sharing the same birthday when there ara n people
if pro > x
fprintf('The number of people we need is: %d\n',n);
break
end
end
若要找到两人生日相同的概率为x需要至少多少人,修改x即可,simTimes越多越准确。