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 comentarios

KSSV
KSSV el 2 de Jun. de 2017
Editada: KSSV el 2 de Jun. de 2017
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
KSSV el 2 de Jun. de 2017
Can you show the working solution?
KSSV
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

Torsten
Torsten el 2 de Jun. de 2017

0 votos

Generate "nop" random integers between 1 and 365 using MATLAB's "randi" and use the "unique" command to see whether the generated array contains repeating numbers. Repeat this for m trials.
Here, "nop" is the number of people in the room.
Best wishes
Torsten.

1 comentario

any chance you could explain how that solves it i have no clue

Más respuestas (1)

La pregunta está cerrada.

Preguntada:

el 2 de Jun. de 2017

Cerrada:

el 29 de Mayo de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by