Hacker News new | ask | show | jobs
by blameless 5051 days ago
I'm unable to reproduce the results. Balance is always negative. What's wrong with my code?

  // for a graph
  var balances = [];

  // constants
  var winnings =  1, 
      losses   = -1, 
      epsilon  = 0.05;

  function play(probOfWinning) {
      return Math.random() < probOfWinning ? winnings : losses;
  }

  for (var experiment = 0; experiment < 100; experiment++) {
      var balance = 0;
      for (var flip = 0; flip < 1000000; flip++) {
          if (flip % 3 == 0) { // game A
              balance += play(0.5 - epsilon);
          } else { // game B
              balance += Math.round(balance) % 3 == 0 ? play(1/10 - epsilon)
                                                      : play(3/4  - epsilon)
          }
      }
      balances.push(balance);
  }
2 comments

The problem is you are using a rounding of the balance across the million tries to decide on game B. The choice of which game B to use need to not be dependent on the other experiments.
You are playing 100 games of 1 million rounds each.

The graph in the article shows the average state of each round over 1 million games.