Why my function is only returning a vector of length 930-50 in R?

Viewed 34

I have a function that is meant to simulate the game of Chuck-a-luck. As a means of displaying the expected value of a bet empirically, I'd like a vector containing the result of 1000 bets. I have come up with the following:

chuck.a.luck.wager.game.limit.record <- function(bankroll) {
games = 0 
changes = vector()

repeat{
   games <- games + 1
   results <- 0
   bankroll <- bankroll - 10
   pred = sample(1:6, 1)
   rolls = n.di.roll(3,6)

   if (rolls[1] == pred) {results <- results +1}
   if (rolls[2] == pred) {results <- results +2}
   if (rolls[3] == pred) {results <- results +3}

   minus_10 = -10
   plus_10 = 10
   plus_20 = 20
   plus_100 = 100



   if (results == 1) {bankroll <- bankroll + 20;
   changes <- append(changes, plus_10)}
   else if (results == 2) {bankroll <- bankroll +30;
   changes <- append(changes, plus_20)}
   else if (results == 3) {bankroll <- bankroll + 110;
   changes <- append(changes, plus_100)}
   else if (results == 0) {changes <- append(changes, minus_10)}



   if (games == 1000)
   {
        break
     }



 }
 return(changes)}

I apologise for the spaghetti code (I normally use Python). However, when I create an instance of the function, it returns a vector of variable length e.g 930, 940, 950, as opposed to the desired 1000.

Does anyone have any ideas as to why this is happening?

1 Answers

What if more than one of rolls equals pred? Then result can be larger than 3 and thus nothing is appended to changes.

Related