那里我有在Java中的方法随机化数组中的整数。但它花了太长时间,我正在试图找到更快的方法来做到这一点,我认为fisher Yates算法是解决方案,但我不知道如何用我的代码实现这一点。实现Fisher Yates算法
protected void randomise() {
int[] copy = new int[getArray().length];
// used to indicate if elements have been used
boolean[] used = new boolean[getArray().length];
Arrays.fill(used,false);
for (int index = 0; index < getArray().length; index++) {
int randomIndex;
do {
randomIndex = getRandomIndex();
} while (used[randomIndex]);
copy[index] = getArray()[randomIndex];
used[randomIndex] = true;
}
for (int index = 0; index < getArray().length; index++) {
getArray()[index] = copy[index];
}
}
/*
* A method which prints out the list of nubers
*/
public static void main(String[] args) {
RandomListing count = new SimpleRandomListing(1000000);
System.out.println(Arrays.toString(count.getArray()));
}
我以前见过这个。它是功课吗? – vz0
你最终使用任何答案?因为如果你这样做了,请标记你使用的答案是正确的。 – Gikkman