The Shared Birthday Problem. Write a Monte Carlo simulation to answer the following question: how many people are required in a group before it is more likely than not that any two of them share a birthday? Conceptually speaking, your script should start with an empty group and add new people with random birthdays, one at a time, until a shared birthday occurs. Assume that a year has 365 days and that all birthdates are equally likely. If the Nth person has the birthday that produces a match, then your answer for that trial is N. In the following example
group = [124], No match, add new person
group = [124, 102], . . .
group = [124, 102, 217], . . .
group = [124, 102, 217, 26], . . .
group = [124, 102, 217, 26, 102], Match found!
The required number of people was 5. Regardless of your approach, you must come up with a reasonable way to pre-allocate your arrays for this problem; do not simply let MATLAB “grow” your array(s) at every iteration. Do not use the built-in functions find, ismember, etc., in your solution. Repeat this procedure for 10,000 trials and report the median number of people required using the exact formatting shown below: Median Number of People = 00 Where 00 will be replaced with your answer rounded up to the nearest integer. Consider what this means: if you are in a group larger than this, then there is > 50% chance of a shared birthday. Does this number seem plausible to you? Would this value increase or decrease if we correctly accounted for the fact that not all birthdates are equally likely?
일단 group(t) = randi([1,365]); 라고 만들어서
for 루프 돌리려고 하는데
t가 가장 큰 column number면 그전꺼 하고 다 비교해서 같으면 넘어가고 아니면 t를 하나 더 더해서 그룹을 늘려서 루프를 반복하려고 하는데
코드가 안짜져... ㅜㅜ
12시간 내로 다 해서 레포트 까지 써서 내야되는데 코드도 못짜고 있어서 맨붕중이야 ㅜㅜ
%shuffle the psuedorandon number creator creator
rng('shuffle')
%initialize empty matrix and other variables for the iteration
group = [];
number_of_people = zeros(1,10000);
count = 1;
t = 2;
s = 1;
n = 1;
for count = 1 : 10000
% initialize the first person's birthday.
group(t) = randi([1,365]);
%initialize the group and t
group = [];
t = 2;
end
median_NP = median(number_of_people);
fprintf('Median Number of People = %2i', median_NP)
이거는 지금까지 대충짜논건데 루프안쪽 하나도 안됨.. ㅜㅜ
어제 어떻게 해서 했는데 디버깅하느라 고치다가 완전히 다른형태가 되버렸어..
되지도 않고 아 진짜 멘붕와서 미칠거같아 형들 좀 도와줘 ㅜㅜㅜ
되버렸->돼버렸 (되어 = 돼임) [리듬 맞춤법 봇♬]
미리 안하구 뭐했어