user210592
user210592

Reputation: 13

Trying to simulate Deal or No Deal in R

I've looked through here to find a solution to my question, but I didn't see one.

In R, I'm trying to simulate Deal or No Deal for a project that gets us familiar with the software. I planned for this to be one big for loop but I can't get my second for loop to work. I broke it apart to make it easier to read (and for me to debug/test).

This loop simulates our original case picked that we keep throughout the game.

    for(i in 1){
    cases <- 1:26
    originalpick <- sample(cases, 1, replace = FALSE) #choose a case
    casesremaining <- (length(cases) - 1) #subtract original case from cases
    }

This loop simulates our first case picked to play.

    for(i in 1){
    casepicked <- replicate(1,sample(cases[-c(originalpick)], 1, replace = FALSE))
   }

My problem is that I am stuck with the possibility that I can choose my original case again. I used sample because I could turn replace off for this very reason, and since that didn't work, I thought that replicate would help. I have seen the update function but I cannot seem to get that to work either.

This is essentially the game, because I have to pick cases until I am left with 2 cases (originally picked case and the one left from cases variable).

I am still fairly new to R, so it could be just my misunderstanding of the functions? Any help would be greatly appreciated!

Upvotes: 1

Views: 125

Answers (1)

jbaums
jbaums

Reputation: 27388

Assuming you want to simulate the order in which cases are picked, you can do this with a single call to sample (wrapped in replicate if you want to perform multiple simulations).

For example:

set.seed(1)
cases <- 1:26
picks <- sample(cases)

picks
## [1]  7 10 14 21  5 19 23 13 12  2  4  3 25 22 24  6  8  9 16 11 26 17 15  1 18 20

When no additional arguments are provided, sample(x) just permutes the vector x. Above, the case originally picked is case 7, and the remaining elements of picks represent the cases picked subsequently.

To perform multiple simulations:

picks3 <- replicate(3, sample(cases))

##       [,1] [,2] [,3]
##  [1,]    1   12   21
##  [2,]   10    7   25
##  [3,]   21    2   11
##  [4,]    8    3   17
##  [5,]   11   25    9
##  [6,]   13   11    7
##  [7,]   25   14   16
##  [8,]    4    8    4
##  [9,]   15   17   13
## [10,]   12    5    3
## [11,]   24   19   19
## [12,]    2   18   23
## [13,]   22   10   20
## [14,]    6    4    1
## [15,]   20    6    8
## [16,]   23    9   10
## [17,]   14    1   12
## [18,]    5   16   24
## [19,]    9   23   14
## [20,]   16   26   15
## [21,]   26   21    5
## [22,]    3   24   22
## [23,]   17   15   26
## [24,]   19   22    2
## [25,]    7   13   18
## [26,]   18   20    6

Now, each column is an independent simulation of the pick order.


For interest, to simulate one pick at a time with a for loop (e.g. if you want to insert other steps in the process), you could do something like the following (though there are many ways to do it):

picks <- sample(cases, 1) # original pick
for (i in 2:length(cases)) { # subsequent picks
  remaining <- setdiff(cases, picks)
  picks[i] <- remaining[sample(length(remaining), 1)]
}

NB: we use remaining[sample(length(remaining), 1)] instead of just sample(remaining, 1) because at the last iteration, remaining will be a single number (the number of the last remaining case). Passing a single number, e.g. 4, to sample results in sample(1:4, 1) instead of the desired sample(4, 1). See this post for more on that.

Upvotes: 2

Related Questions