you cannot shuffle double numbers
Would you like to justify that somewhat surprising statement?
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Create an array of doubles.
loop quite a few times
generate two random ints 0 to (array.length-1)
swap the array elements at those two indexes
result: a shuffled array of doubles.
What's so hard?
ps Or use Collections.shuffle with your doubles wrapped in Doubles
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
In that case, here's a slightly harder to understand, but much faster algorithm:
for i = array.length-1; i>=1; i--
get random int r = 0 to i
swap element i with element r
(this is the algorithm used in Collections.shuffle)This implementation traverses the list backwards, from the last element up to the second, repeatedly swapping a randomly selected element into the "current position". Elements are randomly selected from the portion of the list that runs from the first element to the current position, inclusive. This method runs in linear time Source: JavaDoc for Collections
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
what does get random int r = 0? how do I do that?
that was "r = 0 to i" - a random int >=0 and <= i
Have a look the java.util.Random class and its nextInt(int n) method. (clue - it does exactly what you need).
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Getting close... you need the version of nextInt that lets you specify an upper bound for the random number. Maybe that's what you thought the 26 on line 3 does? - sorry it doesn't do that.
and
amounts[i] = amounts[r];
doesn't exactly swap the two values, although it is part of it.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Getting closer and closer... although I have no idea why you changed the for loop to go in the wrong direction when you had that right the first time.
line 5 - the upper bound for the random number isn't what I said in the pseudocode
Just take a minute to put the pseudocode and your code side by side and compare every detail. Computer programming requires that every tiny detail is right.
ps I'm not saying that your code won't work. Your own testing will tell you that. It looks like a less efficient version of the algorithm I showed you, and looks like it will work, but I don't have time now to validate that.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
That's not exactly the algorithm that I discussed before, but my guess is that it will work OK anyway. On the other hand, my opinion is not important; either it gives the right result or it doesn't. Certainly it's clean code without any obvious flaws. So run some tests. Shuffle your array a few times and print them all out and see if it looks randomised enough for you.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
OK, just for fun - I haven't checked this for typos, but...
This has the algorithm as previously defined - starts at the top end and works down
swaps with elements lower than the current one.
I also pass in the array as a parameter to make this generally useful, rather than being tied to one app. Finally it's static because it doesn't depend on any particular instance (because the array is a parameter)
public static void Shuffle(float[] data) {
Random rand = new Random();
for (int i = data.length; i >= 1; i--) {
int r= rand.nextInt(i+1);
double temp = amounts[i];
amounts[i] = amounts[r];
amounts[r] = temp;
}
}
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073