Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1.

(a) Write a Matlab function that simulates the random experiment of throwing a red and
green die. The output of your function should be a vector with two elements, the first of
which is the number of dots on the top of the red die and the second element is the number
of dots on the top of the green die.

Solution:
function [R] = dies(numberOfThrows)
red = randi(6, numberOfThrows, 1);
green = randi(6, numberOfThrows,1);
red_output = sum(red);
green_output = sum(green);
R = [red_output, green_output];
end

(b) Use the function you wrote in part (a) to simulate 10,000 times the random experiment
of throwing two dice and summing the number of dots on the top of the two dice. Use the
Matlab function hist to make a histogram. Comment on the relationship between the
histogram, the long-term relative frequencies, and the theoretically calculated probabilities.

Solution:
histogram = zeros(numberOfThrows*12, 1);
numberOfExperiments = 10000;
for rolls = 1 : numberOfExperiments
sumOfPoints=sum(dies(1));
histogram(sumOfPoints) = histogram(sumOfPoints) + 1;
end
bar(histogram);
grid on;
caption = sprintf('Results of %d Experiments', ...
numberOfExperiments);
title(caption, 'FontSize', 13);
caption = sprintf('Sum of Points');
xlabel(caption, 'FontSize', 20);
ylabel('Count', 'FontSize', 20);

1
The histogram reflects the fact that as the number of experiments grows large, the histogram for the
sum of two 6-sided dice approaches a triangle. Indeed, the mode of the distribution is the number 7.
This behaviour is consistent with the theoretical distribution
6−|x−7|
P ( X=x )=
{ 36
,∧x ∈{2 , .. ,12 }
0 ,∧otherwise .

1. (a) Write a Matlab function to simulate the random experiment of having n people in a room
and observing their birthdays (month and day, not the year). You may assume that people
born on February 29th report their birthday as being on March 1st.

Solution:
%The number represents the day of the year
function birthdays = birthdays(n)
birthdays = randi(365, n, 1);
end

(b) Encode outcomes from the random experiment simulated in part (a) with 0 if there are no
shared birthdays and 1 if at least two people share a birthday. Let p be the probability that at

2
least two people in the room share a birthday. Use your simulation written in part (a) (and
the relationship between long term relative frequency and probability) to estimate p for the
case n = 5, 10, 20, 23, 30, 40, and 60 and compare your estimates for p with the theoretical
values for p derived in class.

Solution:
n_room=[5 10 20 23 30 40 60];
N=100000;
p_l=length(n_room);
probs=zeros(p_l,1);
for c = 1:p_l
A=zeros(N,1);
for r = 1:N
counts = histc(birthdays(n_room(c)), 1:365);
if max(counts) >=2
A(r,1)=1;
endif
end
probs(c,1)=sum(A)/N;
end
probs

n Estimated probability Theoretical probability

5 0.027020 0.027

10 0.117400 0.117

20 0.413640 0.411

23 0.507640 0.507

30 0.706820 0.706

40 0.890160 0.891

60 0.994110 0.994

1. This class has been contracted by the California Lottery Commission to write the code that
chooses the 5 numbers and the mega number for the Super Lotto Plus game. They will run
your code every Wednesday and Saturday to determine what that night's winning numbers
will be.
a. Write a Matlab function the input to your code is a positive integer, n, which is the
number of groups of 5 winning numbers plus a mega number they want your code to
produce, and the output should be an n x 6 array, each row being a particular choice of 5
winning numbers plus a mega number (column 6 contains the mega numbers). The
following links may be of some help to you:
https://www.calottery.com/play/draw-games/superlotto-plus

3
https://www.calottery.com/play/draw-games/superlotto-plus/how-to-play
The function statement for your code should be
function [Winning_Numbers] = Super_Lotto_Plus (n),
where Winning_Numbers is an n x 6 array and n is a positive integer.

Solution:
function [Winning_Numbers] = Super_Lotto_Plus (n)
Winning_Numbers = zeros(n,6);
for r=1:n
Winning_Numbers(r,1:5) = randperm(47,5,1);
end
for c=1:n
Winning_Numbers(c,6) = randi(27,1,1);
end
end

a. Read the New York Times Magazine article at the link:


https://www.nytimes.com/interactive/2018/05/03/magazine/money-issue-iowa-lottery-fraud-
mystery.html
a. To see if your code is vulnerable to the same hack as the one discussed in the NYT Sunday
Magazine article, see if you can surreptitiously insert some code into the function you wrote in part
(a) so that you, and only you, will know what the winning numbers will be before they run the
program for the drawing on Saturday November 24th, 2020. Note that the lottery commission has
smart people working for them and that they will look at your code to make sure that the numbers
have not simply and explicitly been chosen by you, and that your code does make use of the rand
function. However, they are not nearly as familiar with Matlab and coding in general as Rosen's
students are (or have been taught nearly as well as they have!), so it is possible to fool them. (Hint:
It will be of some help to you to take a look at the Matlab help files for the function rand and for the
functions date and datenum. Also in the Matlab command window type date and hit enter, then type
datenum(date) and hit enter, then type datenum('19-Nov-2020') and hit enter, then type
datenum('20-Nov-2020') and hit enter, then type datenum('21-Nov-2020') and hit enter, and by then
you should get the idea!)

Solution:
In order to avoid the hack, one can use this variant of the wrote code in part (a):
function [Winning_Numbers] = Super_Lotto_Plus (n)
Winning_Numbers = zeros(n,6);
for r=1:n
rng(sum(100*clock));
Winning_Numbers(r,1:5) = randperm(47,5);
end
for c=1:n
rng(sum(100*clock));
Winning_Numbers(c,6) = randi(27,1,1);
end
end

The line rng(sum(100*clock)) changes the random number generator for the randi function in every

4
iteration.

b. You have been told that sometimes the lottery commission will run your code asking for up to,
but no more than, 10 groups of winning numbers before they select one of the groups from the (up
to 10) produced by your code to be that day's winning numbers plus the mega number. Under these
circumstances one cannot of course predict with certainty what the winning numbers and the mega
number will be. So instead, produce a list of 10 groups of 5 winning numbers plus a mega number
that we could then send with Keenan to the 7-11 and tell them to buy a lottery ticket for each of the
10 groups so that it is certain that they have a winning ticket.

Solution:

Winning numbers Mega

34 30 20 15 6 8

44 33 30 1 14 24

14 18 17 1 38 16

36 6 14 18 29 26

38 31 11 17 45 6

40 32 33 39 27 15

16 23 12 19 5 16

32 7 25 13 3 18

20 5 17 11 40 25

44 3 9 29 2 16

You might also like