1、使用单独的方法生成随机数代码
public static int[] shuffle(int n, BigInteger random_index) {
int[] perm = new int[n];
BigInteger[] fact = new BigInteger[n];
fact[0] = BigInteger.ONE;
for (int k = 1; k < n; ++k) {
fact[k] = fact[k - 1].multiply(BigInteger.valueOf(k));
}
//计算阶乘的代码
for (int k = 0; k < n; ++k) {
BigInteger[] divmod = random_index.divideAndRemainder(fact[n - 1 - k]);
perm[k] = divmod[0].intValue();
random_index = divmod[1];
}
// 重新调整值以获得排列
// 从末尾开始,检查前面的值是否更低
for (int k = n - 1; k > 0; --k) {
for (int j = k - 1; j >= 0; --j) {
if (perm[j] <= perm[k]) {
perm[k]++;
}
}
}
return perm;
}
public static void main (String[] args) {
System.out.printf("%s\n", Arrays.toString(
shuffle(50, new BigInteger(
"7890123456789012345678901234567890123456789012345678901234567890"))));
}
2、使用SecureRandom生成随机数
byte seed[] = random.generateSeed(20);
Random rnd = new SecureRandom(seed);
Collections.shuffle(deck, rnd);
或
//Java 7 SecureRandom random = new SecureRandom(); //Java 8 SecureRandom random = SecureRandom.getInstanceStrong(); Collections.shuffle(deck, random);