I have a game I am programming in java and I want my website to be able to verify that the shuffle used in the Java game was real using Javascript in the users browser.
I am using Collections.shuffle(array, seed);
I would like to be able to supply the same seed to an array that is in the same order with javascript to obtain the same outcome.
Here is the source for Collections.shuffle in Java:
public static void shuffle(List<?> list, Random rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i=size; i>1; i--)
swap(list, i-1, rnd.nextInt(i));
} else {
Object arr[] = list.toArray();
// Shuffle array
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
// Dump array back into list
ListIterator it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set(arr[i]);
}
}
}
Edit: I could also use a different shuffle mechanizm for Java. As long as the same seed can be used on Java and Javascript