Help getting birthday paradox simulation working
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
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 comentarios
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
el 2 de Jun. de 2017
Can you show the working solution?
KSSV
el 2 de Jun. de 2017
You see this method is following the same which is given in wiki link and in the link given in answer.
Respuesta aceptada
Más respuestas (1)
KSSV
el 2 de Jun. de 2017
1 voto
La pregunta está cerrada.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!