title: Why does my output of randomly selected numbers contain duplicates?
body: <p>My code needs to randomly select 6 numbers from a list ranging from <code>1</code> to <code>45</code>.</p>
  
  <p>Once when I ran my code (below) the output was  <code>[4, 4, 17, 18, 27, 37]</code>. <strong>I was not expecting any duplicates in the output. How is it possible that there are duplicates?</strong> My code should be removing numbers from <code>list</code> as they are selected.</p>
  
  <pre><code>    Random rng = new Random(); 
      int size = 45;
      int sixList[] = new int[6];
      ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(size);
      ArrayList&lt;Integer&gt; list2 = new ArrayList&lt;Integer&gt;(6);
      for(int i = 1; i &lt;= size; i++) {
          list.add(i);
      }
      Random rand = new Random();
      for(int i = 0; list.size() &gt; 39; i++){
          int index = rand.nextInt(list.size());
          if (index == 0){
              index = rand.nextInt(list.size());
              list2.add(index);
              list.remove(index);
          }else{
              list2.add(index);
              list.remove(index);
          }
      }
      Collections.sort(list2);
      System.out.print(list2);
  </code></pre>