title: Getting caught in loops - R
body: <p>I am looking at whether or not certain 'systems' for betting really do work as claimed, namely, that they have a positive expectation. One such system is based on the rebate on loss. You basically have a large master pot, say $1 million. Your bankroll for each game is $50k.</p>
  
  <p>The way it works, is as follows:
  1) Start with $50k, always bet on banker
  2) If you win, add the money to the master pot. Then play again with $50k.
  3) If you lose(now you're at $30k) play till you either: 
  (a) hit 0, you get a rebate of 10%. Begin playing again with $50k+5k=$55k.
  (b) If you win more than the initial bankroll, add the money to the master pot. Then play again with $50k.
  4) Continue until you double the master pot.</p>
  
  <p>I just cant find an easy way of programming out the possible cases in R, since you can eventually go down an improbable path.</p>
  
  <p>For example, you start at 50k, lose 20, win 19, now you're at 49, now you lose 20, lose 20, now youre at 9, you either lose 9 and get back 5k or you win and this cycle continues until you either end up with more than 50k or hit 0 and get the rebate on the 50k and start again with $50k +5k.</p>
  
  <p>Here's some code i started, but i havent figured out a good way of handling the cases where you get stuck and keeping track of the number of games played. Thanks again for your help. Obviously, I understand you may be busy and not have time.</p>
  
  <pre><code>p.loss &lt;- .4462466
  p.win &lt;- .4585974
  p.tie &lt;- 1 - (p.win+p.loss)
  
  prob &lt;- c(p.win,p.tie,p.loss)
  
  
  bet&lt;-20
  x &lt;- c(19,0,-20)
  
  r &lt;- 10 # rebate = 20%
  
  br.i &lt;- 50
  br&lt;-200
  
  
  #for(i in 1:100){
  # cbr.i&lt;-0
  y &lt;- sample(x,1,replace=T,prob)
  
  cbr.i&lt;-y+br.i
  
  if(cbr.i &gt; br.i){
  
      br&lt;-br+(cbr.i-br.i);
      cbr.i&lt;-br.i;
  
      }else{       
  
        y &lt;- sample(x,2,replace=T,prob);
        if( sum(y)&lt; cbr.i ){ cbr.i&lt;-br.i+(1/r)*br.i; br&lt;-br-br.i}
        cbr.i&lt;-y+
        }else{
  
        cbr.i&lt;- sum(y) + cbr.i;
  
        }if(cbr.i &lt;= bet){
  
          y &lt;- sample(x,1,replace=T,prob)
  
          if(abs(y)&gt;cbr.i){  cbr.i&lt;-br.i+(1/r)*br.i  }  }
  </code></pre>