简体   繁体   English

如何将Javascript中的数组随机排列为与Java中的Collections.shuffle(array,seed)相同的顺序

[英]How do I shuffle an Array in Javascript to be in the same order as Collections.shuffle(array, seed) in Java

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. 我有一个正在用Java编程的游戏,我希望我的网站能够使用用户浏览器中的Javascript验证Java游戏中使用的随机播放是真实的。

I am using Collections.shuffle(array, seed); 我正在使用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. 我希望能够将相同的种子提供给与javascript具有相同顺序的数组,以获得相同的结果。

Here is the source for Collections.shuffle in Java: 这是Java中Collections.shuffle的源代码:

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. 编辑:我也可以为Java使用不同的随机播放机制。 As long as the same seed can be used on Java and Javascript 只要可以在Java和Javascript上使用相同的种子

I'm not convinced that you could. 我不相信你可以。

Java uses an established algorithm with a 48-bit seed to generate pseudorandom numbers. Java使用带有48位种子的既定算法来生成伪随机数。 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). 其次,该数字可以与其他任何数字一起播种以生成另一个模式(并且对具有相同种子的Random任意两次调用都将生成相同的模式)。

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. Javascript不允许您从jump中植入随机数 ,并且在幕后实现的随机数算法可能与 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. 让Java处理集合的随机性,让Javascript代替该顺序。

Use md5 hash algorithm(or any other), it should give the same result on any platform. 使用md5哈希算法(或其他算法),在任何平台上都应提供相同的结果。 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. 可以在Google中找到随机播放算法的实现。

try 尝试

function fisherYates ( myArray ) {
  var i = myArray.length, j, tempi, tempj;
  if ( i == 0 ) return false;
  while ( --i ) {
     j = Math.floor( Math.random() * ( i + 1 ) );
     tempi = myArray[i];
     tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}

see this link 看到这个链接

http://sedition.com/perl/javascript-fy.html http://sedition.com/perl/javascript-fy.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM