Home > Backend Development > PHP Tutorial > Detailed explanation of the lottery function for implementing customized number and probability of winning prizes in PHP

Detailed explanation of the lottery function for implementing customized number and probability of winning prizes in PHP

墨辰丷
Release: 2023-03-27 11:16:01
Original
1547 people have browsed it

This article mainly introduces the lottery function for implementing custom winning numbers and probabilities in PHP, involving operating techniques related to probability operations of PHP strings and arrays. Friends in need can refer to the following

. The details are as follows:

<?php
/*
* 一个抽奖类,精确到万分之一
* 三个步骤:1.接受一个中奖概率数组;2.接受一个抽奖种子;3.返回中奖等级
*/
class Lottery {
/*
* 中奖概率数组,自动判断奖项数目
* 数组键值和为100,自动计算出不中奖的概率,若初始是超过100抛出一个错误
*/
protected $_rate = array();
/*
* 设置中奖概率,
* @param Array,中奖概率,以数组形式传入
*/
public function setRate($rate = array(12.1, 34)) {
$this->_rate = $rate;
if (array_sum($this->_rate) > 100)//检测概率设置是否有问题
throw new Exception(&#39;Winning rate upto 100%&#39;);
if (array_sum($this->_rate) < 100)
//定义未中奖情况的概率,用户给的概率只和为100时,则忽略0
$this->_rate[] = 100 - array_sum($this->_rate);
}
/*
* 随机生成一个1-10000的整数种子,提交给中奖判断函数
* @return int,按传入的概率排序,返回中奖的项数
*/
public function runOnce() {
return $this->judge(mt_rand(0, 10000));
}
/*
* 按所设置的概率,判断一个传入的随机值是否中奖
* @param int,$seed 10000以内的随机数
* @return int,$i 按传入的概率排序,返回中奖的项数
*/
protected function judge($seed) {
foreach ($this->_rate as $key => $value) {
$tmpArr[$key + 1] = $value * 100;
}
//将概率乘十后累计,以便随机选择,组合成
$tmpArr[0] = 0;
foreach ($tmpArr as $key => $value) {
if ($key > 0) {
$tmpArr[$key] += $tmpArr[$key - 1];
}
}
for ($i = 1; $i < count($tmpArr); $i++) {
if ($tmpArr[$i - 1] < $seed && $seed <= $tmpArr[$i]) {
return $i; //返回中奖的项数(按概率的设置顺序)
}
}
}
}
$rate = array(33, 20, 2, 0.95, 12, 4.55);
$a = new Lottery;
$a->setRate($rate);
for ($i = 0; $i <= 10000; $i++) {
$b = $a->runOnce();
@$rewards[$b]++;
}
unset($rewards[&#39;&#39;]);
echo array_sum($rewards);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
</head>
<body>
<table>
<thead>运行10000次,对比设置概率和中奖次数</thead>
<tr><th>设置概率</th><th>中奖次数</th></tr>
<tr><td><?php echo $rate[0]; ?>%</td><td><?php echo $rewards[1] ?></td></tr>
<tr><td><?php echo $rate[1]; ?>%</td><td><?php echo $rewards[2] ?></td></tr>
<tr><td><?php echo $rate[2]; ?>%</td><td><?php echo $rewards[3] ?></td></tr>
<tr><td><?php echo $rate[3]; ?>%</td><td><?php echo $rewards[4] ?></td></tr>
<tr><td><?php echo $rate[4]; ?>%</td><td><?php echo $rewards[5] ?></td></tr>
<tr><td><?php echo $rate[5]; ?>%</td><td><?php echo $rewards[6] ?></td></tr>
<tr><td><?php echo &#39;miss&#39;; ?></td><td><?php echo $rewards[7] ?></td></tr>
</table>
</body>
</html>
Copy after login

Related recommendations:

PHP implementation of calculation lottery Probability Algorithm

## Generate numbers by Probability

js implements numbersProbabilityCalculation

##

The above is the detailed content of Detailed explanation of the lottery function for implementing customized number and probability of winning prizes in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template