Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

<R programming> We simulate the rolling of a die. To do this we use the function

ID: 3301634 • Letter: #

Question

<R programming>

We simulate the rolling of a die. To do this we use the function runif(1), which returns a 'random' number in the range (0,1). To get a random integer in the range {1,2,3,4,5,6}, we use ceiling(6*runif(1)), or if you prefer, sample(1:6, size=1) will do the same job.

(a) Suppose that you are playing the gambling game of the Chevalier de Mere. That is, you are betting that you get at least one six in 4 throws of a die. Write a program that simulates one round of this game and prints out whether you win or lose.

(b) Turn the program that you wrote in part (a) into a function sixes, which returns TRUE if you obtain at least one six in n rolls of a fair die, and returns FALSE otherwise. That is, the argument is the number of rolls n, and the value returned is TRUE if you get at least one six and FALSE otherwise. How would you give n the default value of 4?

(c) Now write a program that uses your function sixes from part (b), to simulate N plays of the game (each time you bet that you get at least 1 six in n rolls of a fair die). Your program should then determine the proportion of times you win the bet. This is proportion is an estimate of the probability of getting at least one 6 in n rolls of a fair die.

(d) In part (c), instead of processing the simulated runs as we go, suppose we first store the results of every game in a file, then later postprocess the results. Write a program to write the result of all N runs to a textfile sixes_sim.txt, with the result of each run on a separate line.

Explanation / Answer

Solution:

a)
> win <- FALSE
> for (i in 1:4) {
+ if (sample(1:6, size = 1) == 6) {
+ win <- TRUE
+ }
+ }
> if (win) {
+ print("win")
+ } else {
+ print("lose")
+ }
[1] "win"

b) > sixes <- function(n = 4) {
+ # plays the game of the Chevalier de Mere
+ # returns TRUE if at least one six in n rolls
+ # returns FALSE otherwise
+ win <- FALSE
+ for (i in 1:n) {
+ if (sample(1:6, size = 1) == 6) {
+ return(TRUE)
+ }

+ }
+ return(FALSE)
+ }
Here is a vectorised version.
> sixes <- function(n = 4) {
+ sum(sample(1:6, size = n, replace = TRUE) == 6) > 0
+ }


c) > p_estimate <- function(N, n = 4) {
+ # proportion of wins in N runs of sixes(n)
+ total_wins <- 0
+ for (i in 1:N) {
+ if (sixes(n)) total_wins <- total_wins + 1
+ }
+ return(total_wins/N)
+ }
> p_accuracy <- function(N, n = 4) {
+ # accuracy of p_estimate
+ total_wins <- 0
+ for (i in 1:N) {
+ if (sixes(n)) total_wins <- total_wins + 1
+ }
+ return(total_wins/N - 1 + (5/6)^n)
+ }

d)
> sixes_sim <- function(N, n = 4) {
+ # runs sixes(n) N times and saves the results in "sixes_sim.txt"

+ cat(file="sixes_sim.txt") # deletes contents of file
+ for (i in 1:N) {
+ cat(file = "sixes_sim.txt", sixes(n), " ", append = TRUE)
+ }
+ }
> sixes_sim(100)
> results <- scan("sixes_sim.txt", what = TRUE)
> mean(results)
[1] 0.6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote