【问题标题】:Scratchcard PHP script刮刮卡 PHP 脚本
【发布时间】:2014-02-02 23:04:17
【问题描述】:

我正在编写一个刮刮卡脚本,我想知道是否有人可以帮助我,如果你不明白这可能会使你的大脑有点融化!

因此,几率可能会有所不同:1/$x; 现在假设:$x = 36;

所以这就是我想要理解的......

  • 我想在1 and 5之间生成9随机数。
  • 我想要3 数字与1/36 匹配的几率。
  • 必须不能一次生成超过 3 个重复数字。

我可以想象某种数组循环可能是正确的通过方式?

【问题讨论】:

  • 尽量每篇文章只问一个问题

标签: php random numbers


【解决方案1】:

有时——这是其中之一——作弊是做你想做的事的最佳方式。

a) Set up an array of your 9 numbers, and a 2nd frequency array (5 elements) that counts which number occurs how often.
b) Generate a random number 1-5. Set the 1st and 2nd card to this number, and mark this number with 2 in your freqency array.
c) If random(36) < 1 (1/36 probability), set your 3rd card to the same number and mark this number with 3 in your frequency array.
d) Generate the rest of the cards - find a random number, repeat while frequency of the found number >=2, set the next card to number, increase frequency of the found number.
e) When finished, shuffle the cards (generate 2 random numbers between 1 and 9, swap the 2 cards, repeat 20-30 times).

d) 部分就是我所说的作弊——您在步骤 c) 中输入了 1/36 的概率,而在 d) 部分中,您只需确保不会产生另一场比赛。 e) 用于向用户隐藏它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多