Question: ( b ) Playing the lottery! ( i ) Complete the following code of a function that simulates playing the lotto 6 / 4 9
b Playing the lottery!
i Complete the following code of a function that simulates playing the lotto with the same ticket until you win the jackpot. You have to insert code where it says HERE.
playlotto function
n # n is the number of attempts to get our numbers ticket # HERE: create a vector of your favourite numbers in increasing order! draw c # initializes the vector of numbers drawn,
# so that the while loop doesnt complain while allticketdraw
n n draw # HERE: draw a random sample of numbers
returnn
# from : in increasing order # How would you sort a vector? Hint: Google!
The ticketdraw command creates a vector of TRUE and FALSE where it compared the two vectors componentwise. If the two vectors have the same numbers in the same spots, this will give a vector full of TRUEs all then checks whether each spot is TRUE. The exclamation mark stands for not: you can read the line while allticketdraw as while not all numbers in the ticket and draw arrays correspond
Another way to implement the while loop would be: while sumticketdraw As ticketdraw is a vector of TRUE and FALSE, the sum of the vector will be if and only if all entries are TRUE, as R handles TRUE as and FALSE as
iiTo test your code, first modify it so that it simulates the lotto sample of numbers instead of Call this modified function play lotto three. It should complete relatively quickly, and help you debug any problems you may have.
iiiRun your play lotto three function a times, and find the mean and standard deviation.
ivRun the code for the lotto once. The value returned by the function gives the number of times you had to play before winning the jackpot. The code will probably take several minutes to terminate, so be patient.
v Assuming you play once a week, how many years did it take before winning the jackpot in according to your simulation?
vi Assuming each ticket cost $ and that the jackpot is $ what is your net earning after playing this many times?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
