Trying to simulate Deal or No Deal in R -


i've looked through here find solution question, didn't see one.

in r, i'm trying simulate deal or no deal project gets familiar software. planned 1 big loop can't second loop work. broke apart make easier read (and me debug/test).

this loop simulates our original case picked keep throughout game.

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

this loop simulates our first case picked play.

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

my problem stuck possibility can choose original case again. used sample because turn replace off reason, , since didn't work, thought replicate help. have seen update function cannot seem work either.

this game, because have pick cases until left 2 cases (originally picked case , 1 left cases variable).

i still new r, misunderstanding of functions? appreciated!

assuming want simulate order in cases picked, can single call sample (wrapped in replicate if want 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 provided, sample(x) permutes vector x. above, case picked case 7, , remaining elements of picks represent 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 independent simulation of pick order.


for interest, simulate 1 pick @ time for loop (e.g. if want insert other steps in process), following (though there many ways it):

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

nb: use remaining[sample(length(remaining), 1)] instead of sample(remaining, 1) because @ last iteration, remaining single number (the number of last remaining case). passing single number, e.g. 4, sample results in sample(1:4, 1) instead of desired sample(4, 1). see this post more on that.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -