Consider a lotto game where the wining ticket has d distinct numbers selected fr
ID: 3209619 • Letter: C
Question
Consider a lotto game where the wining ticket has d distinct numbers selected from the set (1,2,...r). Let P(m,d,r) be the probability that there are exactly m matches between a random ticket and the winning ticket. It can be shown that P(m,d.r)=B(d,m)B(r-d,d-m)/B(r,d). Implement a function exactmatch(m,d,r) that returns this value. You can compute binomial coefficients using the Matlab function nchoosek. Write a script that prints a table for the choices d=3,4,5,6. Each table should display the value of P(m,d,r) for m=0:d and r=20:10:50.
Explanation / Answer
First let us define the function exactmatch here:
function probability = exactmatch(m,d,r)
% This function calculates the probability of winning a lotto game where the winning ticket has d distinct numbers selected from the set (1,2,..r).
B1 = nchoosek(d,m); % nchoosek(d,m) function gives the binomial coefficient d!/(d-m)!m!
B2 = nchoosek(r-d,d-m);
B3 = nchoosek(r,d);
probability = B1*B2/B3; % calculate the probability according to the given formula
end
Now let us write the code to actually calculate the probabilities
dvals = [3,4,5,6]; % Define the range of d values
rvals = 20:10:50; % Define the range of rvalues
% now write a for loop to iterate over all the d values, and corresponding m and r values
% since you wanted to print tables of probabilities for each value of d on the scree,
% we first accumulate all the values into a string and print it at the end of calculation for each value of d.
for dcnt = 1:length(dvals) % iterate over all d values
tablestr = sprintf('Probabilities for d value: %d ',dvals(dcnt));
tablestr = [tablestr,sprintf('m r P(m,d,r) ')];
mvals = 0:dcnt; % define the possible m values corresponding to this d value
P = zeros(length(mvals),length(rvals)); % Initiate a matrix P that stores the probabilities
for mcnt = 1:length(mvals) % Iterate over all the m values
for rcnt = 1:length(rvals) % Iterate over all the r values
P(mcnt,rcnt) = exactmatch(mvals(mcnt),dvals(dcnt),rvals(rcnt)); % Calculate the probability for this m, d, and r values
thisstring = sprintf('%d %d %f ',mvals(mcnt),dvals(dcnt),P(mcnt,rcnt)); % Add the newly calculated probability to our table string
tablestr = [tablestr,thisstring];
end
end
% Now print the table
% In case you want to print the output to a file, you will have to open a file for writing and write the output string to it
disp(tablestr)
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.