# Using a Shuffle Algorithm on a Deck of Cards

### Shuffle a Deck of Cards

Let’s try to write a method which will shuffle a deck of cards. The deck should be perfectly mixed. The card permutations should be equally probable.

Solution

We will use the perfect random number generator. Let’s solve the problem using the most obvious way. We can select random cards and put them into a new deck. In fact, a deck is an array; therefore, we need a way to lock some particular elements.

Original deck [1] [2] [3] [4] [5]

/* Choose a random element to put it in the beginning of the shuffled deck */
/* Mark it in the original deck as “locked” so we will not take it again */

Shuffled deck [4] [?] [?] [?] [?]
Original deck [1] [2] [3] [X] [5]

If we mark item [4] will that prevent selecting it again? One way is to swap the “locked” ([4]) and the first elements of the deck:

Original deck [1] [2] [3] [4] [5]

/* Choose a random element to put it in the beginning of the shuffled deck and
change the current element with the 1st one */

Shuffled deck [4] [?] [?] [?] [?]
Original deck [X] [2] [3] [1] [5]

/* Choose a random element to put it in the beginning of the shuffled deck
and change the current element with the 2nd one */

Shuffled deck [4] [3] [?] [?] [?]
Original deck [X] [X] [2] [1] [5]

The algorithm is easier to implement for the situation where the first k cards are “locked” than for the situation where, for example, the third, fourth, and ninth cards are “locked”.

We can optimize the algorithm combining the shuffled and the original deck.

Original deck [1] [2] [3] [4] [5]

/* Choose a random element to put it between 1 and 5 and swap it with 1.
In this example we chose 4th;
after that, the 1st element is locked */

Original deck [4] [2] [3] [1] [5]

/* Element 1 is locked. Choose a random element to swap it with element 2.
In this case it will be number 3*/

Original deck [4] [3] [2] [1] [5]
/* repeat */

This algorithm is easy to implement iteratively:

```public void shuffle(int[] array) {
int tmp, number;
for (int i = 0; &lt; array.length; i++) {

number = (int) (Math.random() * (array.length - i)) + i;
tmp = array[i];
array[i] = array[number];
array[number] = tmp;
}
}
```

Thanks for your attention!

This sample with a shuffle algorithm can also be used to deal with your own homework. Future programmers will find this guide useful. Now we have come very close to your problem of not knowing how to deal with your assignment. AssignmentShark.com represents a service where you can get qualified help. We help students from all over the world, so it doesn’t matter what country you are from. What happens if you use our service? Or, is it a good idea to place an order on our site?

By ordering, you will get a completed assignment without any errors. Since it is necessary to hand in your assignment on time, our experts work fast so that you will get a completed order as soon as possible. Our expert will take care of your order and complete it according to your requirements and academic standards. Place an order on our site if any assignment problem occurs. We can protect you from failures in studying. Do my computer science assignment – Easily!