Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Randomly shuffling an array.

 
Mick Flynn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I've racked my brains and torn the SDK documentation apart, but I can't find a way of randomly shuffling an array (in my case, it's an array of String objects - so that they display in a different order each time they're used).
I found a way of shuffling a List item, but I'm damned if I can find a way to get an array into a List and turn the shuffled List back into an array of Strings. I'd especially like to seed it, so that the random sort is different each time.
Any ideas how I could do this?
Mick Flynn
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mick, I don't know of a precreated method in Java that will do such a shuffle for you. Certainly you could write your own shuffle method using Math.random() or the Random class (which has a setSeed() method).
One way to do this would be to cycle through a loop n times (n = size of array), generating a random number from 1-n (or 0 - (n-1)) and copying the value in that position from the original array into a new "shuffled" array. You'd have to place your random numbers into a Hashmap or similar, and enclose the random number generator in a while loop checking for already-used values in the Hashmap using the containsKey() method.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why actually shuffle the array? Why not just choose a random index every time you access it? Though I guess you may have a problem with choosing the same index multiple times, randomizing an array is pretty inefficient, as the code below shows... though hopefully it will help you out if you really need it...



HTH,
-Nate
 
Junilu Lacar
Bartender
Posts: 7570
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Standard shuffle algorithm:
<pre><font size=2)<br /> int randPos;<br /> for ( int i = arrayToShuffle.length - 1; i > 0; i-- )
{
randPos = (int)(Math.random() * i);
swap( arrayToShuffle[i], arrayToShuffle[randPos])
}
</pre>
Junilu
 
Mick Flynn
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott, Nathan,
The idea of choosing a random index every time I access the array would work wouldn't it...?
But only if I had a way of flagging which indices have already been used... you see, I can't access the same one twice.
Would I create a boolean array, all set to false, and set each one to true as it is accessed? That would work, wouldn't it?
In any case, thanks a million to both of you!
MF
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic