Help getting birthday paradox simulation working
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
So I'm trying to simulate the birthday paradox showing the chance that 2 people in a room share their birthday. We have to use loops and not just enter the known function for the problem.
This is the last part of a 3 part project and I've been stuck for days. Here is the piece of code I need help with. I understand the first loop is iterations and the second loop runs through the chance with n amount of people. I'm using the unique command to see if the length is different; if they don't match it ads +1 to the nth element of my p (probability) array.
Please somehow clue me in where I'm going wrong. The command window just says memory exceeded so I'm not sure what to do next.
clc, clear, close all;
n = 1:365; %number of people
m = 100; %iteratiions
p = zeros(1,365); %probability
for i = 1:m;
for n = 1:365;
group = rand(1:n);
if length(unique(group)) ~= n;
p(n)=p(n)+1;
else
end
end
end
3 个评论
Have you checked what this line does?
group = rand(1:n);
This is the culprit...your group will be a n dimensions matrix with size 1,2,3,4,....n-1. Your usage of rand is wrong. YOu have to read it and implement, check this link: https://en.wikipedia.org/wiki/Birthday_problem
KSSV
2017-6-2
Can you show the working solution?
KSSV
2017-6-2
You see this method is following the same which is given in wiki link and in the link given in answer.
采纳的回答
更多回答(1 个)
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!