2017-04-13 135 views
1

我有一個老虎機模擬器(見下面的代碼) 我想運行模擬器5000並將結果(獎)存儲到數據集中。老虎機 - 存儲模擬結果在R

我的想法是在數據集中有三個變量: sim_number;獎;累積的。

SIM_NUMBER:在模擬(1至5000)

獎金的數量:是仿真

累積的結果:是仿真的數量和仿真的總數量之間的比率(如第一個模擬,這將是1/5000 = 0.0002

我怎樣才能做到這一點?我卡在我的代碼最後一行。

 #Slot machine simulator 
     #Reels and symbols 
    get_symbols <- function() { 
      wheel <- c("DD", "7", "BBB", "BB", "B", "C", "0") 

      sample(wheel, size = 3, replace = TRUE, 
      prob = c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.52)) 
    } 

    get_symbols() 


    #note: A player will win a prize if he gets: 
      # Three of the same type of symbol (except for three zeroes) 
      # Three bars (of mixed variety) 
      # One or more cherries 
      # Otherwise, the player receives no prize. 

      #Diamonds are treated like 「wild cards,」 which means they can be considered any other symbol if it would increase a player’s prize. 

      #Diamonds are also special in another way. Every diamond that appears in a combination doubles the amount of the final prize. So 7 7 DD 
      #would actually be scored higher than 7 7 7. Three sevens would earn you 80, but two sevens and a diamond would earn you 160. One seven 
      #and two diamonds would be even better, resulting in a prize that has been doubled twice, or 320. A jackpot occurs when a player rolls DD DD DD. 
      #Then a player earns 100 doubled three times, which is 800 

    score <- function (symbols) { 
      # identify case 
      same <- symbols[1] == symbols[2] && symbols[2] == symbols[3] 
      bars <- symbols %in% c("B", "BB", "BBB") 

      # get prize 
      if (same) { 
        payouts <- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25, 
           "B" = 10, "C" = 10, "0" = 0) 
        prize <- unname(payouts[symbols[1]]) 

      } else if (all(bars)) { 
        prize <- 5 

      } else { 
        cherries <- sum(symbols == "C") 
        prize <- c(0, 2, 5)[cherries + 1] 
      } 

      # adjust for diamonds 
      diamonds <- sum(symbols == "DD") 
      prize * 2^diamonds 
    } 


    # Slot machine game play 
    play <- function() { 

      # step1: generate symbols 
      symbols <- get_symbols() 

      #step2: display symbols 
      #print(symbols) 

      #step3: display symbols 
      #score(symbols) 

      structure(score(symbols), symbols = symbols, class = "slots") 

    } 


    #Format output 
    slot_display <- function(prize){ 

      # extract symbols 
      symbols <- attr(prize, "symbols") 

      # collapse symbols into single string 
      symbols <- paste(symbols, collapse = " ") 

      # combine symbol with prize as a regular expression 
      # \n is regular expression for new line (i.e. return or enter) 
      string <- paste(symbols, prize, sep = "\n£") 

      # display regular expression in console without quotes 
      cat(string) 
    } 

    print.slots <- function(x,...) { 
      slot_display(x) 
    } 


    # Have fun and gamble responsibly! 
    play() 


    #Monte Carlo simulation 
    runs <- 10 
    set.seed(9876) 

    mc.out <- replicate(runs,play()) # outcome 

謝謝 菲德

+0

嗨菲德, 你可以使用類似的'lapply(1:5000,播放)'和改變你的功能,使它們返回在列表或矢量格式?甚至返回作爲一行數據框可以工作,然後你可以'do.call('rbind',lapply_output)'? – Jenks

回答

0

如果你堅持,你提供的代碼,如何結合你想要的信息(輸出你想成爲一個數據幀),像這樣:

mc.out <- data.frame(sim.num = 1:runs, prize = replicate(runs,play()), cumulative = (1:runs)/runs) # outcome 
+0

謝謝!這很容易,我一直在想。 –

+0

有點過分我們過度的常見;有時我們的作品只需要一些新鮮的眼睛= D – din

0

我想我如下會格式化play功能:

play <- function(holder_var) { #using holding var so lapply works 

      # step1: generate symbols 
      symbols <- get_symbols() 

      #step2: display symbols 
      #print(symbols) 

      #step3: display symbols 
      #score(symbols) 

      return(score(symbols)) 
} 

然後我會做回

library(dplyr) 

Output <- lapply(1:runs, play) 
Output_DF <- data.frame(score = unlist(output)) 

Output_DF <- Output_DF %>% mutate(Round = 1:runs, Cumulative = Round/runs) 
以下

可以內聯幾次這樣的事情,但我沒有他們那種隔開