Answer to Question #215682 in MatLAB for livy

Question #215682

If there is a group of x people in a room, what is the probability that two or more of them have the same birthday? It is possible to determine the answer to this question by simulation.


a) Make a getRandom function , then edit as follows to:  Take as input n, which is the maximum random number that may be generated  Return (output) a number in the range 1 to n Tip: Still use the round(rand()) construct, but this time, you are multiplying by n and adding 1. (note that you are NOT using the randi built-in function here!)


2 b) Write a new getRandomArray function :  Take as input the number of people in a room (x) and the maximum number that can be used (n)  Return an array of b numbers (i.e. the birthdays)  Use the getRandom function to generate a random birthday for each person, from 1 to n Tip: initialise x and use a loop to generate the required number of elements and build the array.


1
Expert's answer
2021-07-11T14:38:03-0400

getRandom.m:

function x = getRandom(n)
    x = round(n * rand()) + 1;
end


getRandomArray.m:

function birthdays = getRandomArray(x, n)
birthdays = zeros(x, 1);
for i=1:x
    birthdays(i) = getRandom(n);
end


theSameBithday.m:

function p = theSameBirthday(x, nTrial)
p = 0;
for i=1:nTrial
    b = sort(getRandomArray(x, 365));
    for j=1:(x-1)
        if b(j) == b(j+1)
            p = p + 1;
            break
        end
    end
end

p = p / nTrial;


To get probability among 30 people any two has the same birthday:

theSameBirthday(30, 10000)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS