title: Equal probability in Java
body: <p>How can I get the Math.random() call to have absolute equal probability for 4 cases? (ie 0.25 for case 1, 0.25 for case 2, 0.25 for case 3, 0.25 for case 4)?</p>
  
  <p>I currently get 0.05 for Case 1, 0.075 for Case 2, 0.45 for Case 3 and 0.425 for Case 4.</p>
  
  <p>Been trying to work out why it does this for hours with not much success.</p>
  
  <pre><code>int direction = 0;
  
          // Select a random number
  
          randno = (int) Math.round(Math.random() * 3);
  
          // Convert this to a direction
  
          while (direction == 0) {
              if (randno == 0 &amp;&amp; !isWall(robot, Robot.LEFT)) direction = Robot.LEFT;
              else if (randno == 1 &amp;&amp; !isWall(robot, Robot.RIGHT)) direction = Robot.RIGHT;
              else if (randno == 2 &amp;&amp; !isWall(robot, Robot.BEHIND)) direction = Robot.BEHIND;
              else if (!isWall(robot, Robot.AHEAD)) direction = Robot.AHEAD;
              else randno = (int) Math.round(Math.random() * 3);
          }
  </code></pre>