1

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

  • What do you mean by "real", exactly? Do you want to verify that it took place? Do you want to display the shuffled cards in the browser? – Makoto Feb 28 '13 at 04:37
  • @makoto—I think the OP is expecting to shuffle in both environments using the same seed and get exactly the same result. – RobG Feb 28 '13 at 04:41
  • As in. I will show players a string of the unshuffled array. Then a random seed will be used to shuffle the array (In the game using java). On the games completion. The player is shown the unshuffled array, the shuffled array and the seed, and can use javascript in the browser to once again shuffle the original unshuffled deck with the generated seed to get the same result – Apple Cider Feb 28 '13 at 04:49

2 Answers2

2

I'm not convinced that you could.

Java uses an established algorithm with a 48-bit seed to generate pseudorandom numbers. Second, that number may be seeded with any other number to generate another pattern (and any two invocations of Random with the same seed are to generate the same pattern).

Javascript doesn't allow you to seed random numbers from jump, and the random number algorithm implemented under the hood may differ from the algorithm used in Java.

While I don't see a purpose to perform a random operation in two distinct places, why not collapse it into one? Let Java handle the randomization of the collection, and let Javascript relay that ordering instead.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Use md5 hash algorithm(or any other), it should give the same result on any platform. Or invent your own hash function, but it should be equally distributed. Thats a very complex task. Results of hash function could be recursively used to get next random number. That will give the same sequence of random numbers on any system. Based on its result implement shuffle algorithm. Shuffle algorithm implementations could be found in google.

Mikhail
  • 4,175
  • 15
  • 31