PHP winning probability algorithm and big wheel lottery algorithm_PHP tutorial

WBOY
Release: 2016-07-13 10:28:00
Original
1015 people have browsed it

PHP winning probability algorithm, which can be used for scratch cards, big roulette and other lottery algorithms. Usage is very simple, the code has detailed comments and instructions, and you can understand it at a glance

 $proCur) {

$randNum = mt_rand(1, $proSum);

 if ($randNum <= $proCur) {

$result = $key;

break;

 } else {

$proSum -= $proCur;

 }

 }

unset ($proArr);

return $result;

 }

 /*

* Awards array

 * is a two-dimensional array that records all prize information for this lottery,

* Among them, id represents the winning level, prize represents the prize, and v represents the probability of winning.

* Note that v must be an integer. You can set v of the corresponding award to 0, which means that the probability of winning the award is 0,

* The sum (base) of v in the array. The larger the base, the more accurate the probability.

* In this example, the sum of v is 100, then the winning probability corresponding to the tablet is 1%,

* If the sum of v is 10,000, then the probability of winning is one in ten thousand.

*

 */

 $prize_arr = array(

 '0' => array('id'=>1,'prize'=>'tablet','v'=>1),

 '1' => array('id'=>2,'prize'=>'Digital Camera','v'=>5),

 '2' => array('id'=>3,'prize'=>'Speaker device','v'=>10),

 '3' => array('id'=>4,'prize'=>'4G USB flash drive','v'=>12),

 '4' => array('id'=>5,'prize'=>'10Q coins','v'=>22),

 '5' => array('id'=>6,'prize'=>'You might win next time','v'=>50),

 );

 /*

* Every time the front-end page is requested, PHP loops the award setting array,

* Obtain the drawn award ID through the probability calculation function get_rand.

* Save the winning prize in the array $res['yes'],

* The remaining unwinnable information is stored in $res['no'],

* Finally output the json number data to the front-end page.

 */

foreach ($prize_arr as $key => $val) {

$arr[$val['id']] = $val['v'];

 }

 $rid = get_rand($arr); //Get the award id based on probability

 $res['yes'] = $prize_arr[$rid-1]['prize']; //Win the prize

unset($prize_arr[$rid-1]); //Remove the winning prizes from the array, leaving the unwon prizes

shuffle($prize_arr); //Disrupt the order of the array

 for($i=0;$i

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/814672.htmlTechArticlephp winning probability algorithm, which can be used for scratch cards, big wheel and other lottery algorithms. The usage is very simple. There are detailed comments in the code, so you can understand it at a glance $proCur) { $randNum = mt_rand(1, $proSum); i...
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