rcv40
rcv40

Reputation: 37

Shuffling deck algorithm in Java

Hey guys I was just reading my notes and had a difficult time understanding the algorithm they used to shuffle cards. Here's what it looks like. Can someone please explain each line in as much detail as they can? Thank you very much.

public void shuffle()
{
    // next call to method dealCard should start at deck[0] again
    currentCard = 0;

    // for each Card, pick another random card (0-51) and swap them
    for (int first = 0; first < deck.length; first++)
    {
        // select a random number between 0 and 51
        int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

        // swap current Card with randomly selected Card
        Card temp = deck[first];
        deck[first] = deck[second];
        deck[second] = temp;
    }
}

Upvotes: 0

Views: 2401

Answers (3)

Sweeper
Sweeper

Reputation: 270758

The for loop

for (int first = 0; first < deck.length; first++)

loops through all the cards in the deck. It basically says

For each of the cards in the deck... do some stuff

And the "some stuff" is the code that is inside the for loop:

    // select a random number between 0 and 51
    int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

    // swap current Card with randomly selected Card
    Card temp = deck[first];
    deck[first] = deck[second];
    deck[second] = temp;

It first selects a random number from 0 to 51.

int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

"Why do you want to do that?" you asked. "Shuffling" a deck is just swapping the locations of cards in the deck, like

5 of diamonds goes to the top, 3 of clubs goes to somewhere in the middle, king of spades goes to the 23rd place...

So for each card, the program "swaps" it with the card at a random location in the deck. That's why it is picking a random number from 0 to 51.

Let's say that the random number is 25. It will then swap the first card with the one at index 25 of the deck array:

Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;

If you don't understand how does the swapping work. I can explain.

In Java, you can't just swap things by putting the stuff in variable B in variable A and the stuff in A in B:

// This code doesn't swap a and b
int a = 10;
int b = 20;
a = b;
b = a;

You need to first put the stuff in A somewhere,

Card temp = deck[first];

Then move the things in B to A,

deck[first] = deck[second];

Finally, pick up the stuff that was in A from that "somewhere" and put it in B:

deck[second] = temp;

Phew! That was a lot of steps!

Upvotes: 5

ankit249
ankit249

Reputation: 452

Click on this github link for the solution

Use the reservoirSampleShuffle, no need for the extra Data structure here and its in place shuffle

Upvotes: -1

Zeus
Zeus

Reputation: 2253

The algorithm does the following,

In a loop it traverses all 52 cards.
For each card picked, it randomly chooses one among the remaining 51.
It swaps the two.

Upvotes: -1

Related Questions