老虎机 - 在R中存储模拟结果

时间:2017-04-13 15:01:49

标签: r simulation montecarlo

我有一台老虎机模拟器(见下面的代码) 我想运行模拟器5000并将结果(奖品)存储到数据集中。

我的想法是在数据集中有三个变量: 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

谢谢你, FEDE

3 个答案:

答案 0 :(得分:0)

如果您坚持使用您提供的代码,那么如何将您想要的信息(想要输出到数据框中)组合起来,如下所示:

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

答案 1 :(得分: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)

你可以内联其中的一些东西,但我把它们分开了

答案 2 :(得分:0)

library(dplyr)
library(ggplot2)

# Clear workspace
rm(list = ls())

# Generate 3 random symbols
get_symbols <- function(){

# All symbols and their probs
symbols <- c("DD", "7", "BBB", "BB", "B", "C", "0")
symbols_p <- c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.52)

# Random sampling with replacement
sample(symbols, 3, prob = symbols_p, replace = TRUE)

}

# Generic prize function
score <- function(symbols) {
  diamonds <- sum(symbols == "DD")
  cherries <- sum(symbols == "C")

  # identify case
  # since diamonds are wild, only nondiamonds
  # matter for three of a kind and all bars
  slots <- symbols[symbols != "DD"]
  same <- length(unique(slots)) == 1
  bars <- slots %in% c("B", "BB", "BBB")

  # assign prize
  if (diamonds == 3) {
    prize <- 100
  } else if (same) {
    payouts <- c("7" = 80, "BBB" = 40, "BB" = 25,"B" = 10, "C" = 10, "0" = 0)
    prize <- unname(payouts[slots[1]])
  } else if (all(bars)) {
    prize <- 5
  } else if (cherries > 0) {
    # diamonds count as cherries
    # so long as there is one real cherry
    prize <- c(0, 2, 5)[cherries + diamonds + 1]
  } else {
    prize <- 0
  }
  # double for each diamond
  prize * 2^diamonds
}

# Play the slot machine
play <- function(){

  #Test with following symbols
  #symbols <- c("DD", "BB", "BB")
  #symbols <- c("DD", "BB", "B")
  #symbols <- c("DD", "B", "C")
  #symbols <- c("C","BB", "BB")
  #symbols <- c("B","BB", "BBB")
  #symbols <- c("0","0", "DD")
  #symbols <- c("BBB", "DD", "DD")

  # Generate random symbols
  symbols <- get_symbols()

  # Find prize
  score(symbols)
}