gpt4 book ai didi

java - Collections.shuffle的有效性

转载 作者:行者123 更新时间:2023-12-01 17:23:36 24 4
gpt4 key购买 nike

我正在尝试用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com