Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: shuffling help

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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
    So instead of
    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
    //I'm using the LinkedList class
    public void shuffle()
    {
    for (int i=0;i<26;i+=1)
    {
    temporarydeck.add(deck.get(i));
    /* 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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?

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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).

  6. #6
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default 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.

    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
    		}
    	}

  7. #7
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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:

    for (int i=size; i>1; i--)
        swap(arr, i-1, rnd.nextInt(i));
    Last edited by pbrockway2; April 15th, 2012 at 05:22 PM.

  8. #8
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: shuffling help

    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.

  9. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.

  10. #10
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: shuffling help

    Double post removed

Similar Threads

  1. [SOLVED] array not shuffling
    By captain in forum Collections and Generics
    Replies: 2
    Last Post: February 27th, 2012, 08:32 AM
  2. Shuffling a 2D array
    By Buzzins in forum Object Oriented Programming
    Replies: 6
    Last Post: January 16th, 2012, 01:47 PM
  3. Shuffling elements in a linked list.
    By xecure in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2010, 01:25 PM