2017-04-13 136 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) 
以下

可以内联几次这样的事情,但我没有他们那种隔开