做项目的时候涉及到抽奖概率的设置,以下是PHP实现中奖抽奖概率的计算算法,供大家参考 一、中奖概率方法:可以看到设置了每个中奖概率,合计中奖概率为100% 来源地址:https://blog.csdn.net/qq_37448303/ar
做项目的时候涉及到抽奖概率的设置,以下是PHP实现中奖抽奖概率的计算算法,供大家参考
一、中奖概率方法:可以看到设置了每个中奖概率,合计中奖概率为100%
['id' => 1, 'prize' => '奖品1', 'v' => 20],'1' => ['id' => 2, 'prize' => '奖品2', 'v' => 10],'2' => ['id' => 3, 'prize' => '奖品3', 'v' => 20],'3' => ['id' => 4, 'prize' => '奖品4', 'v' => 5],'4' => ['id' => 5, 'prize' => '奖品5', 'v' => 19],'5' => ['id' => 6, 'prize' => '奖品6', 'v' => 5],'6' => ['id' => 7, 'prize' => '奖品7', 'v' => 10],'7' => ['id' => 8, 'prize' => '奖品8', 'v' => 10],]; foreach ($prize_arr as $key => $val) {$arr[$val['id']] = $val['v'];} $rid = $this->get_prize($arr); //根据概率获取奖项id$data = $prize_arr[$rid-1];return JSON($data); }
二、对概率进行计算
$proCur) {$randNum = mt_rand(1, $proSum);if ($randNum <= $proCur) {$result = $key;break;} else {$proSum -= $proCur;}}unset ($proArr);return $result;}?>
来源地址:https://blog.csdn.net/qq_37448303/article/details/127003808
--结束END--
本文标题: PHP抽奖中奖概率算法设计
本文链接: https://lsjlt.com/news/393558.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0