File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Shuffling an Array (without Repetition...) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shuffling an Array (without Repetition...)" Watch "Shuffling an Array (without Repetition...)" New topic
Author

Shuffling an Array (without Repetition...)

Vanessa Astle
Ranch Hand

Joined: May 08, 2007
Posts: 37
I have an array of Colors that I wish to be shuffled:

Color[] colorArray = new Color[18];
colorArray[0] = Color.BLUE;
colorArray[1] = Color.BLUE;
colorArray[2] = Color.RED;
colorArray[3] = Color.RED;
colorArray[4] = Color.GREEN;
colorArray[5] = Color.GREEN;
colorArray[6] = Color.YELLOW;
colorArray[7] = Color.YELLOW;
colorArray[8] = Color.ORANGE;
colorArray[9] = Color.ORANGE;
colorArray[10] = Color.MAGENTA;
colorArray[11] = Color.MAGENTA;
colorArray[12] = Color.PINK;
colorArray[13] = Color.PINK;
colorArray[14] = Color.CYAN;
colorArray[15] = Color.CYAN;
colorArray[16] = Color.WHITE;
colorArray[17] = Color.WHITE;

I tried calling the collections.shuffle method (with the colors in both array and arraylist form) but some of the colors were lost, and I ended up with some colors more than or less than twice...

I was wondering if I would be able to get some help with this, or if there were any suggestions.

Thanks very much for your time!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Edit: your post title says "without repetition"... what's that all about? You deliberately have repetition in your array.
[ May 11, 2007: Message edited by: Paul Clapham ]
Vanessa Astle
Ranch Hand

Joined: May 08, 2007
Posts: 37
Repetition beyond what I specify in the array. Unfortunately I am still ending up with more than two copies of some colors and less than two copies of other colors.

Perhaps I need to approach the problem differently.
[ May 11, 2007: Message edited by: Vanessa Astle ]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

Paul, she does say
I ended up with some colors more than or less than twice...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vanessa Astle
Ranch Hand

Joined: May 08, 2007
Posts: 37
In case it helps, here's the method that I'm using:

protected Color generateRandomColor(int i)
{
Color[] colorArray = new Color[18];
colorArray[0] = Color.BLUE;
colorArray[1] = Color.BLUE;
colorArray[2] = Color.RED;
colorArray[3] = Color.RED;
colorArray[4] = Color.GREEN;
colorArray[5] = Color.GREEN;
colorArray[6] = Color.YELLOW;
colorArray[7] = Color.YELLOW;
colorArray[8] = Color.ORANGE;
colorArray[9] = Color.ORANGE;
colorArray[10] = Color.MAGENTA;
colorArray[11] = Color.MAGENTA;
colorArray[12] = Color.PINK;
colorArray[13] = Color.PINK;
colorArray[14] = Color.CYAN;
colorArray[15] = Color.CYAN;
colorArray[16] = Color.WHITE;
colorArray[17] = Color.WHITE;

List colorList = Arrays.asList(colorArray);
Collections.shuffle(colorList);
colorArray = (Color[]) colorList.toArray();
return colorArray[i];
}
It is called in a drawCards() method, and 'int i' represents the position of the card. I am using it in a MatchGame which is fully functional except for the fact that I cannot get the colors to generate in pairs and therefore am left with a number of "Old Maids" at the end...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

why do you shuffle the array, AND choose a random index? isn't one or the other sufficient?

And i'm not exactly sure how you are using this, but each time you call the generateRandoColor, you create a new random array of colors. it that really what you want?
Vanessa Astle
Ranch Hand

Joined: May 08, 2007
Posts: 37
I'm not passing it a random index, the index is a set position. After I shuffle the cards I place them (in order) in positions ranging from 0 to 17.

....However, you are completely right about generating a new random set of colors every time. I guess I'll take my array declaration out of my method and then split my generateRandomColor method into two methods to set the random array and to retrieve from the random array.

Perfect!!

Thanks so much!!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

sorry - that's what i get for scanning the code as i try to work... i saw "generateRandomColor(int i)" and just parsed that as "generate random integer i".

i need more sleep.

Glad i got PART of it right, though...
 
jQuery in Action, 2nd edition
 
subject: Shuffling an Array (without Repetition...)
 
Similar Threads
background color of a jframe
converting a string color to an object color
Creating an IndexColorModel with 8 Colours
While loops and Graphics
JColorChooser customization problem