Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shuffling an Array (without Repetition...)

 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21126
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, she does say
I ended up with some colors more than or less than twice...
 
Vanessa Astle
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic