title: Shuffling a Deck of Cards , Redundancy after swapping two values
body: <p>I was asked to write a program(mainly a method) for shuffling a deck of cards. I wrote the following program:</p>
  
  <pre><code>public class Deck {
  
  ////////////////////////////////////////
  // Data Members
  ////////////////////////////////////////
  
  private Card[] cards; // array holding all 52 cards
  private int cardsInDeck; // the current number of cards in the deck
  
  public static final int DECK_SIZE = 52;
  
  /**
   * Shuffles the deck (i.e. randomly reorders the cards in the deck). 
   */
  public void shuffle() {
      int newI;
      Card temp;
      Random randIndex = new Random();
  
      for (int i = 0; i &lt; cardsInDeck; i++) {
  
          // pick a random index between 0 and cardsInDeck - 1
          newI = randIndex.nextInt(cardsInDeck);
  
          // swap cards[i] and cards[newI]
          temp = cards[i];
          cards[i] = cards[newI];
          cards[newI] = temp;
      }
  }
  
  }
  </code></pre>
  
  <p>But there is a logical error in the above shuffle method which is as follows: Suppose I replace Card Number 4 with Card Number 42, then I'm swapping
  two times. I'm wondering is there any way of not doing this?</p>
  
  <p>I checked one post here :<a href="https://stackoverflow.com/questions/4075439/shuffling-a-deck-of-cards">Shuffling a deck of cards</a></p>
  
  <p>But it didn't make sense to me.</p>