title: Slot machine - store results of simulations in R
body: <p>I have a slot machine simulator (see code below)
  I would like to run the simulator 5000 and store the result (prize) into a dataset.</p>
  
  <p>My idea is to have three variables in the dataset:
  sim_number; prize; cumulative.</p>
  
  <p>sim_number: is the number of the simulation (1 to 5000)</p>
  
  <p>prize: is the outcome of the simulation</p>
  
  <p>cumulative: is the ratio between the number of the simulation and the total number of simulation (eg for the first simulation, it would be 1/5000 = 0.0002</p>
  
  <p>How can I achieve this? I'm stuck at my last line of code.</p>
  
  <pre><code>        #Slot machine simulator
          #Reels and symbols
      get_symbols &lt;- function() {
              wheel &lt;- 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 &lt;- function (symbols) {
              # identify case
              same &lt;- symbols[1] == symbols[2] &amp;&amp; symbols[2] == symbols[3]
              bars &lt;- symbols %in% c("B", "BB", "BBB")
  
              # get prize
              if (same) {
                      payouts &lt;- c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25,
                                   "B" = 10, "C" = 10, "0" = 0)
                      prize &lt;- unname(payouts[symbols[1]])
  
              } else if (all(bars)) {
                      prize &lt;- 5
  
              } else {
                      cherries &lt;- sum(symbols == "C")
                      prize &lt;- c(0, 2, 5)[cherries + 1]
              }
  
              # adjust for diamonds
              diamonds &lt;- sum(symbols == "DD")
              prize * 2 ^ diamonds
      }
  
  
      # Slot machine game play
      play &lt;- function() {
  
              # step1: generate symbols
              symbols &lt;- get_symbols()
  
              #step2: display symbols
              #print(symbols)
  
              #step3: display symbols
              #score(symbols)
  
              structure(score(symbols), symbols = symbols, class = "slots")
  
      }
  
  
      #Format output
      slot_display &lt;- function(prize){
  
              # extract symbols
              symbols &lt;- attr(prize, "symbols")
  
              # collapse symbols into single string
              symbols &lt;- paste(symbols, collapse = " ")
  
              # combine symbol with prize as a regular expression
              # \n is regular expression for new line (i.e. return or enter)
              string &lt;- paste(symbols, prize, sep = "\n£")
  
              # display regular expression in console without quotes
              cat(string)
      }
  
      print.slots &lt;- function(x,...) {
              slot_display(x)
      }
  
  
      # Have fun and gamble responsibly!
      play()
  
  
      #Monte Carlo simulation
      runs &lt;- 10
      set.seed(9876)
  
      mc.out &lt;- replicate(runs,play()) # outcome
  </code></pre>
  
  <p>Thank you,
  Fede</p>