2017-04-13 2 views
1

Ich habe einen Slot Machine Simulator (siehe Code unten) Ich möchte den Simulator 5000 laufen lassen und das Ergebnis (Preis) in einen Datensatz speichern.Slot Maschine - Speichern Ergebnisse von Simulationen in R

Meine Idee ist es, drei Variablen im Datensatz zu haben: sim_number; Preis; kumulativ.

sim_number: ist die Anzahl der Simulation (1 bis 5000)

Preis: ist das Ergebnis der Simulation

kumulativ ist das Verhältnis zwischen der Anzahl der Simulation und der Gesamtzahl der Simulations (zB für die erste Simulation, wäre es 1/5000 = 0,0002

Wie kann ich das erreichen? ich bin in meinem letzten Zeile Code stecken.

 #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 

Vielen Dank, Fede

+0

Hallo Fede heraus gesperrt, Sie so etwas wie eine 'lapply benutzen konnte (1: 5000, play)' und Ihre Funktionen ändern, so dass sie zurückgegeben werden in einem Listen- oder Vektorformat? Sogar zurückgegeben als ein Zeilen-Datenframe würde funktionieren und dann könnten Sie einfach 'do.call ('rbind', lapply_output)'? – Jenks

Antwort

0

Wenn Sie mit dem Code halten, die Ihnen präsentiert, wie etwa die Info Kombinieren Sie (Ausgabe, die Sie wollten in einem Datenrahmen), wie dies gewünscht:

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

danke! Es war sehr einfach und ich dachte nach. –

+0

irgendwie üblich, dass wir überdenken; irgendwann brauchen unsere Arbeiten nur frische Augen = D – din

0

Ich glaube, ich wie folgt würde formatieren Sie Ihre play Funktion:

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)) 
} 

Dann täte ich die folgenden für die Rückkehr

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) 

Sie können ein paar der Dinge, Inline, aber ich habe sie Art von

Verwandte Themen