小编ver*_*jas的帖子

彩票算法 - PHP - 数学似乎不错,但功能是否有效?

我想进行自定义彩票提取,以激励用户参与在线实验.规则是:

  • 10%可获得10美元
  • 1%的几率获得50美元
  • 0.1%的几率获得500美元

彩票是一个PHP函数,它被调用一次并返回奖品(0,10,50或500).我创建了下面的函数,经过70 000次试验后,统计数据是:

  • 10美元9.11%
  • 50%的.91%
  • 500美元的.01%

我应该担心这个算法吗?有没有比mt_rand更好的方法来创造良好的机会分配?

function lottery() {
  // winnings before extraction
  $win=0;

  // choose a winning number between 1 and 10
  $target=mt_rand(1,10);

  // make three independent extractions, each with 1/10 probability
  if (mt_rand(1,10) == $target) {
    // if first extraction is the winning number -> prize=10
    // probability: 1/10
    $win=10;

    if (mt_rand(1,10) == $target) {
        // if second extraction is ALSO the winning number -> prize=50
        // probability: 1/10 * 1/10
        $win=50;

        if (mt_rand(1,10) …
Run Code Online (Sandbox Code Playgroud)

php algorithm

6
推荐指数
2
解决办法
2582
查看次数

标签 统计

algorithm ×1

php ×1