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 6/49 with the same ticket until you win the jackpot. You have to insert code where it says HERE.
play_lotto <- function(){
n <-0 # n is the number of attempts to get our numbers ticket <- # HERE: create a vector of your 6 favourite numbers in increasing order! draw <- c(0,0,0,0,0,0) # initializes the vector of numbers drawn,
# so that the while loop doesnt complain while (!all(ticket==draw)){
n <- n +1 draw <- # HERE: draw a random sample of 6 numbers
}
return(n)}
# from 1:49 in *increasing order*. # How would you sort a vector? (Hint: Google!)
(The ticket==draw command creates a vector of TRUE and FALSE where it compared the two vectors component-wise. 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 (!all(ticket==draw)) as while (not all numbers in the ticket and draw arrays correspond))
(Another way to implement the while loop would be: while (sum(ticket==draw)<6). As ticket==draw is a vector of TRUE and FALSE, the sum of the vector will be 6 if and only if all entries are TRUE, as R handles TRUE as 1 and FALSE as 0.)
(ii)To test your code, first modify it so that it simulates the lotto 3/49(sample of 3 numbers instead of 6). Call this modified function play lotto three. It should complete relatively quickly, and help you debug any problems you may have.
(iii)Run your play lotto three function a 100 times, and find the mean and standard deviation.
(iv)Run the code for the lotto 6/49 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.
3
(v) Assuming you play once a week, how many years did it take before winning the jackpot in 6/49, according to your simulation?
(vi) Assuming each ticket cost $3, and that the jackpot is $16,000,000, 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!