2016-01-31 22:28
浏览 197


I'm writing a lottery draw simulation program as a project. The way the game works is you need to pick the 6 numbers that are draw from the 49 to win.

Your chance of winning is 1/13,983,816 because that's how many combinations of 6 in 49 there are. The demo program on Go playground generates six new numbers each time around the loop forever.

Each time a new set of numbers is generated I test to see if it already exists and if it does I break out of the loop. With 13,983,816 combinations you would think it would be a long time before the same 6 numbers would repeat but, in testing it fails always before 10000 iteration. Does anyone know why this is happening?

图片转代码服务由CSDN问答提供 功能建议

我正在编写一个彩票抽奖模拟程序作为一个项目。 游戏的工作方式是,您需要从49个中选择6个数字才能赢。

您的获胜机会是1 / 13,983,816,因为这是49中6的组合数量 有。 上的 演示程序 每次在 永远循环。

每次生成一组新的数字时,我都会进行测试以查看其是否已经存在,是否可以退出循环。 使用13,983,816个组合,您会认为重复相同的6个数字要花很长时间,但是在测试中,它总是在10000次迭代之前失败。 有人知道为什么会这样吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dougutuo9879 2016-01-31 23:11

    In my opinion you have a couple of problems here.

    1. You use Go playground, where your randomness is fixed. This line rand.Seed(time.Now().UnixNano()) always produce the same seed because time.Now() is the same.
    2. You test completely different things with your simulation. I will write about it in the end.
    3. if you want to do something similar to gambling - you have to use cryptographically secure PRNG and Go has it. If you want you can read more details here (the answer is to php question, but it explains the difference).

    On the probability part:

    The probability of winning your lottery is indeed 1/C(49, 6) = 1/13,983,816. But this is the probability that someone would select an already predefined set of numbers. For example you claim that your winner is {1, 5, 47, 3, 4, 5} and now the probability that someone would win is approximately 1 in 14 mln. So you have to do the following. Randomly select a set of 6 numbers and then compare your new selection in a loop to already found.

    But what you do is to check the probability of collision. That having N people some of them would select the same sets (not necessarily even the winning set). This is known as the birthday paradox. And as you see there, the probability of collision increase dramatically with the increase of number of people N.

    This is absolutely the same problem, but your number of days in the year is 13,983,816 and you can check here that for this number of days you need only 5000 iterations to guarantee with 0.59 percents that you will get a collision. And with 9000 iterations you will find the collision with probability 0.94.

    解决 无用
    打赏 举报

相关推荐 更多相似问题