gpt4 book ai didi

java 随机数生成器 - 彩票

转载 作者:行者123 更新时间:2023-12-01 19:05:54 26 4
gpt4 key购买 nike

我的代码看起来很业余,因为我是一名二年级的软件工程学生。

我创建了一个彩票号码生成器,并注意到奇怪但一致的结果。我的程序尝试匹配之前的欧洲百万彩票抽奖号码。我记录了尝试的次数,还记录了匹配 3、4、5 和 6 个数字的次数。

尝试次数在 100 万到 4.22 亿之间。也就是说,我会运行该程序 10 次,并且会达到一个范围,我还会跟踪每次运行所需的时间长度。

我考虑了很多事情,例如防止随机数被多次使用,并且此检查是针对可能的彩票号码的 HashMap 进行的。如果我在 HashMap 中找到随机数,我会将该数字添加到数组列表中,然后从 HashMap 中删除该数字。

我的问题围绕结果。

在所有匹配彩票号码的尝试中,我获得 3 个号码的机会平均为 3.13%。 4 个数字下降到 0.28%,5 个数字下降到 0.00012%,6 个数字下降到 0.00022%。

可以理解的是,随着彩票号码数量的增加,中奖的机会将会减少,但无论我尝试 100 万次还是 1 亿次,比率都是相同或非常接近。

如果您感兴趣的话,我的最小尝试次数是 1,088,157 次,大约花费了 6 秒或 6612 毫秒。

最大尝试次数为 422,036,905 次,耗时 26 分钟或 1589867 毫秒。

由于我使用的是 Java Random 库,所以我只是想弄清楚这一点。或者我应该简单地将其归结为概率?

我的代码是不必要的 225 行,如果您想查看特定部分或希望查看整个内容,请提出请求。下面是前 5 个数字的随机数生成示例。

//stores all possible lottery numbers
public static HashMap<Integer,Integer> randRange = new HashMap<Integer,Integer>();

//stores bonus ball numbers
public static HashMap<Integer,Integer> boRange = new HashMap<Integer,Integer>();

//stores lottery number output
public static ArrayList<Integer> lotNum = new ArrayList<Integer>();

//stores bonus ball output
public static ArrayList<Integer> boNum = new ArrayList<Integer>();

public static void randomInt(){

Random rand = new Random();

//generate a random number
int RandInt = rand.nextInt(51);
int boInt = rand.nextInt(12);

//loop used to get unique random number
int count=0;
while(count!=5){

//check if random number exists
if(randRange.get(RandInt)!=null)
{
//finalise random number
RandInt=randRange.get(RandInt);

//add to ArrayList
lotNum.add(RandInt);

//remove number
//ensures next random number is unique
randRange.remove(RandInt);

count++;
}

else
{
//get a new random number
//and start process again
RandInt = rand.nextInt(51);
}
}
}

编辑:

首先抱歉,我无法投票,因为我的声誉低于 15。所有答案都有帮助,包括评论。

感谢所有成员的建议,我改进了我的程序,并毫不奇怪地发现了我的代码中的错误。 @digitaljoel 你对匹配 5 和 6 个数字的概率是正确的。我的计算设置不正确,例如对于欧洲百万抽奖的数字 11,20 30,35,45,2,3,匹配 3 为 0.7%,4 为 0.05%,5 为 0.00273%,6 为 0.000076%。

感谢@maybewecouldstealavan,我改变了我的洗牌方法,简单地填充一个ArrayList并洗牌列表,获取前五个数字并对奖金球执行相同的操作。好处在于每秒检查数量从每秒 150 - 20 万次检查增加到每秒 250 - 70 万次检查。

感谢@trutheality,在某些情况下,如果我检查 1000 或 1,000,000 个匹配项,变化是相似的或微小的。

@LeviX 再次欣赏可能组合的计算。我在程序中使用了这个,发现需要超过组合总数才能中奖。我很可能产生重复的随机数。由此,我可能会创建所有可能的组合并随机选择每个组合,直到程​​序找到匹配项。

最佳答案

In all attempts to match the lottery numbers my chance of getting 3 numbers was 3.13% on average. For 4 numbers it dropped to 0.28%, 5 numbers 0.00012% and 6 numbers 0.00022%.

Understandably The chance of winning as the number of lottery numbers increase is going to decrease however whether I had 1 million or 100 million attempts the ratio was the same or extremely close.

这实际上一点也不奇怪。您最终要做的是估计正确猜测 3、4、5 或 6 个数字的概率。拥有更多的样本只会使您的估计值的变化更小,但即使样本“少”为 100 万个,您的估计值也有望接近准确的概率(您可以通过做一些数学计算来计算)。

关于java 随机数生成器 - 彩票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114401/

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