我正在尝试用 java 创建一个彩票模拟器,我开始担心我生成的数字是否足够随机。
例如,我用于生成不重复的白球号码的代码如下所示。
public static int[] genWhiteNums()
{
int[] whitePicks = new int[5];
Collections.shuffle(whiteDrawNums); //whiteDrawNums is an arraylist of numbers 1-59
for(int i = 0; i < 5; i++)
{
whitePicks[i] = whiteDrawNums.get(i);
}
whiteDrawNums = createWhiteNums(); //Reordering
return whitePicks;
}
从 Collections.shuffle 中选择前 5 个值是否足够随机?
最佳答案
根据文档here
Randomly permutes the specified list using a default source of randomness. All permutations occur with approximately equal likelihood.
The hedge "approximately" is used in the foregoing description because default source of randomenss is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm would choose permutations with perfect uniformity.
它使用线性时间混洗算法。我认为它的随机性足以满足大多数人的需求。
关于java - Collections.shuffle的有效性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741005/