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 5 of 5

Thread: Shuffling an Array is putting the same integer in more than one place

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Shuffling an Array is putting the same integer in more than one place

    I have to shuffle a deck (array) of 52 integers but I started with 3 for testing if it was an even shuffle and it will place the same integer in more than one spot in the random array. I'm not sure what I'm doing wrong.
    import java.util.Random;
     
    public class shuffleDeck 
    {
    	public static void main(String[] args) 
    	{
    		int[] Deck = new int[3];
     
    		for (int i=0; i<3; i++)
    		{
    			Deck[i] = i;
    		}
     
    		Random r = new Random();		
    		int[] RandomDeck = new int[3];
     
    		for (int i=0; i<3; i++) 
    		{
    		    int randomPosition = r.nextInt(3);
    		    RandomDeck[randomPosition] = Deck[i];
    		}
     
    		System.out.println("Original Deck \t Random Deck");
     
    		for (int i=0; i<3; i++)
    		{
    			System.out.println(Deck[i] + "\t\t " + RandomDeck[i]);
    		}
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Shuffling an Array is putting the same integer in more than one place

    Think about what this for loop is doing:

    for (int i=0; i<3; i++) 
    {
       int randomPosition = r.nextInt(3);
       RandomDeck[randomPosition] = Deck[i];
    }

    You're going through the original deck, and copying each element of the original deck to a random index in the new deck. What happens if your random positions are all zeros? Or two 1s and a 2?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Shuffling an Array is putting the same integer in more than one place

    Should I be doing it the other way around?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Shuffling an Array is putting the same integer in more than one place

    What happened when you tried?

    What you need to do is figure out *what you're trying to do* first. How would you do this in real life, with a real deck of cards?

    Pretend you have a friend who has no idea how to shuffle cards. Your job is to tell that friend to take a deck of cards and then *copy* that deck of cards (your friend loves drawing) into a new shuffled deck of cards. Write down instructions that your friend can follow- remember, your friend has no idea how to shuffle cards, so the steps you write have to be very small and easy to follow. If a step is too complicated, then break it down into smaller steps.

    When you have a list of instructions that your friend can follow, then you'll have an algorithm that you can think about implementing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Shuffling an Array is putting the same integer in more than one place

    Another way to think of it is that dealing from any place in the deck randomly chosen is the same as shuffling before dealing the cards in order.

    Also posted here.

Similar Threads

  1. How to convert String array to Integer array...?'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 25th, 2012, 08:50 AM
  2. [SOLVED] array not shuffling
    By captain in forum Collections and Generics
    Replies: 2
    Last Post: February 27th, 2012, 08:32 AM
  3. Shuffling a 2D array
    By Buzzins in forum Object Oriented Programming
    Replies: 6
    Last Post: January 16th, 2012, 01:47 PM
  4. Reading a file, then putting it in an array
    By Gondee in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 16th, 2010, 11:11 AM
  5. Putting text fields in an array
    By Movies32 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2010, 07:46 PM