title: How to write a probability algorithm that can be maintained easily?
body: <p>Supposed I want to create a game. At the start of the game, the player will pick a monster.</p>
  
  <p><strong>It's easy to picks the monster fairly.</strong></p>
  
  <pre><code>// get all monsters with equal chance
  public Monster getMonsterFair(){
      Monster[] monsters = {new GoldMonster(), new SilverMonster(), new BronzeMonster()};
      int winIndex = random.nextInt(monsters.length);
      return monsters[winIndex];
  }
  </code></pre>
  
  <p><strong>And picks the monster unfairly.</strong></p>
  
  <pre><code>// get monsters with unequal chance
  public Monster getMonsterUnFair(){
      double r = Math.random();
      // about 10% to win the gold one
      if (r &lt; 0.1){
          return new GoldMonster();
      }
      // about 30% to winthe silver one
      else if ( r &lt; 0.1 + 0.2){
          return new SilverMonster();
      }
      // about 70% to win the bronze one
      else {
          return new BronzeMonster();
      }   
  }
  </code></pre>
  
  <p>The problem is that, when I add a new monster to the game, I have to edit the <strong>if-else</strong>.
  Or I change the chance of winning GoldMonster to 0.2, I have to change all 0.1 into 0.2
  .It's ugly, and not easily maintained.</p>
  
  <pre><code>// get monsters with unequal change &amp; special monster
  public Monster getMonsterSpecial(){
      double r = Math.random();
      // about 10% to win the gold one
      if (r &lt; 0.1){
          return new GoldMonster();
      }
      // about 30% to win the silver one
      else if ( r &lt; 0.1 + 0.2){
          return new SilverMonster();
      }
      // about 50% to win the special one
      else if ( r &lt; 0.1 + 0.2 + 0.2){
          return new SpecialMonster();
      }
      // about 50% to win the bronze one
      else {
          return new BronzeMonster();
      }
  }
  </code></pre>
  
  <p>How can this probability algorithm can be refactored so that the codes can be maintained easily when new monster is added and the chances of winning monsters are adjusted?</p>