# shuffling help

• March 6th, 2012, 05:37 PM
ronimacarroni
shuffling help
I want to make a shuffling class.
What it would do would be to switch the places of odd numbers and even numbers of cards
1
2
3
4
5
6
It'd be
2
1
4
3
6
5
You know, like when you shuffle cards
So far I've done this
Quote:

public void shuffle()
{
for (int i=0;i<26;i+=1)
{
/* gets odd number cards and stores them in a temporary list*/
deck.remove(i);
/*removes the odd numbers from the list*/
}
}
So far it gives me the even numbers but I don't know where to take it from there.
• March 6th, 2012, 07:04 PM
Norm
Re: shuffling help
To get the logic for the exchange of elements in the sample data you posted, take a piece of paper and write down the source and target elements for each swap that is done.
Then look at how the index for the source changes from one swap to the next
When you write the code Remember to use a temp variable when doing the swapping.
• March 6th, 2012, 07:31 PM
pbrockway2
Re: shuffling help
Norm is right about doing the shuffle "in place" - ie figure out which indices have to swap and use a temporary variable. But what you posted doesn't seem to have anything to do with swapping odd/even indices as you described: its more like how you might attack perfect "riffle" shuffle. Is there some particular exercise you're trying to answer?
• March 6th, 2012, 07:37 PM
ronimacarroni
Re: shuffling help
Yeah I'm trying to do the riffle shuffle.
I think sawpping the odd/even indices is how it would be done right?
• March 6th, 2012, 09:19 PM
pbrockway2
Re: shuffling help
A riffle shuffle doesn't doesn't involve swaps (in any obvious and straight forward sense).

Starting with a pack labelled

0-1-2-3-4-5-6-7-8-9- ... -49-50-51-52

You are attempting to form

0-26-2-27-4-28-6-29-8-30 ... -24-51-25-52

I think your idea of using temporary decks might be the easiest way of thinking about it. Have your code model what happens when your perform an out shuffle like this. Or an in shuffle (riffles come in two flavours).
• April 15th, 2012, 11:09 AM
aesguitar
Re: shuffling help
I recently was supposed to design a simple poker game, part of playing poker is shuffling. To shuffle the deck, I made the deck via an ArrayList<String> and to shuffle, I took a Random number 0<=x<=51 for all the cards in the deck. It took the card in that position and placed it on top of the deck.

Code :

```private void shuffle() { Random a = new Random();//gets a random number int b = 0; for(int i = 52 * 7; i >= 0; i--)//shuffles all 52 cards seven times { b = a.nextInt(deck.size());//gets a random number String c = (String) deck.get(b);//gets a random card deck.remove(b);//removes it deck.add(c);//adds it back to the deck } }```
• April 15th, 2012, 05:15 PM
pbrockway2
Re: shuffling help
A correction: from the OP's code (selecting 1/2 of the pack) I leapt - maybe too fast - to the conclusion he or she was after a riffle shuffle. Anyway the illustration I gave was wrong. It should have been from

0-1-2-3-...-51 to 0-25-1-26-2-...-23-50-24-51

-----

Your shuffle is interesting. Why seven (or 364) times?

If you do the put-to-the-top thing just once the result is not very shuffled. The more you do it, the more (in some sense) shuffled it becomes. But, when is it "safe" to stop?

Another - standard - approach is along these lines:

Choose a number 0->51, put that one to the top.
Choose a number 1->51, put that one to the top.
Choose a number 2->51, put that one to the top.
...

Just 52 put-to-the-top operations and it's perfectly shuffled. Perfect in the sense that every permutation is equally likely. The java.util.Collections class provides a shuffle() method for working with lists. The source code is in src.zip in the JDK directory. It is optimised to be really fast, but at its heart is a beautiful one line array shuffle, as above but cleverly working from the "other" end and relying on swaps rather than put-to-front:

Code :

```for (int i=size; i>1; i--) swap(arr, i-1, rnd.nextInt(i));```
• April 15th, 2012, 09:52 PM
aesguitar
Re: shuffling help
Quote:

Your shuffle is interesting. Why seven (or 364) times?
I have it shuffle through 7 times because of something I read somewhere that said that a deck is fully randomized or shuffled after about 7 or so shuffles.

Also, being relatively new to this, I didn't know about the Collections class. I'll take a peak at it sometime.
• April 15th, 2012, 11:22 PM
pbrockway2
Re: shuffling help
The "seven times" requirement actually applies to riffle-type shuffles where you divide the pack then interleave the two halves. It comes about because the original order is largely preserved by such shuffles. (Most clearly seen if you take an ordered pack and riffle shuffle it a couple of times.)

The Collections API documentation is the best place to see what "built in" methods are available. But this is one case where the actual source code is instructive.
• April 15th, 2012, 11:23 PM
pbrockway2
Re: shuffling help
Double post removed