Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. PlayGame <- function(winnings=0, turn=1) {
  2. if(sample(0:1, 1)) {
  3. winnings <- winnings + (2^turn)
  4. turn <- turn + 1
  5. PlayGame(winnings, turn)
  6. }
  7. else {
  8. winnings
  9. }
  10. }
  11.  
  12. RunSimulation <- function(timesToPlay=20000) {
  13. results <- rep(NA, timesToPlay)
  14. results1 <- rep(NA, timesToPlay)
  15. rollingAvg <- rep(NA, timesToPlay)
  16. rollingAvg1 <- rep(NA, timesToPlay)
  17.  
  18. for(i in 1:timesToPlay) {
  19. results[i] <- PlayGame()
  20. results[i] = results[i]
  21. rollingAvg[i] <- mean(results[1:i])
  22. }
  23. plot1 = plot(rollingAvg, type="l")
  24.  
  25. }
  26. RunSimulation()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement