<?php function get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); //概率数组循环 foreach ($proArr as $key => $proCur) { $randNum = mt_rand(1, $proSum); if ($randNum <= $proCur) { $result = $key; break; } else { $proSum -= $proCur; } } unset ($proArr); return $result; }
The prize array is a two-dimensional array that records all the prize information of this lottery, where 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 of v in the array (base). The larger the base, the more accurate the probability can be reflected. . In this example, the sum of v is 100, then the probability of winning for the tablet is 1%. If the sum of v is 10,000, the probability of winning is one in ten thousand. $proArr is a preset array. Assume that the array is: array(100,200,300,400). It starts by screening the first number from the probability range of 1,1000 to see if it is within its occurrence probability range. If not, it will The probability space is the probability space of the value of k minus the number just now. In this case, it is minus 100, which means that the second number is filtered within the range of 1,900. In this way, until the end of the screening, there will always be a number that meets the requirements. It's like touching something in a box. If the first one isn't there, the second one isn't there, and the third one isn't there, then the last one must be. This algorithm is simple and very efficient. The key is that this algorithm has been applied in our previous projects, especially in projects with large amounts of data. The efficiency is very good.
All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn
Related Article
13 Jul 2016
PHP winning probability algorithm, winning probability algorithm. PHP winning probability algorithm, winning probability algorithm. We first complete the backend PHP process. The main job of PHP is to configure the awards and the corresponding winning probability. Click on the current page to flip a certain direction.
12 Jul 2016
Winning probability algorithm (php can be used for scratch cards, big wheel and other lottery algorithms), winning probability scratch card. Winning probability algorithm (php can be used for scratch cards, big roulette and other lottery algorithms), winning probability scratch card php winning probability algorithm, can be used for scratch cards, big roulette and other lottery algorithms. Usage is very simple
08 Aug 2016
:This article mainly introduces the PHP winning probability algorithm. Students who are interested in PHP tutorials can refer to it.
13 Jul 2016
Lottery program written in PHP and winning probability algorithm. Winning Probability Algorithm for Lottery Program Written in PHP This article shares with you the PHP Lottery Probability Algorithm, which can be used for scratch cards, big roulette and other lottery algorithms. Usage is very simple and there are detailed comments in the code
15 Nov 2017
In our daily work, we often participate in some lottery activities, so today we will share with you the PHP 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, which can be understood at a glance. Friends who need it can refer to it.
13 Jul 2016
PHP winning probability algorithm and lottery algorithm such as big wheel. PHP winning probability algorithm, which can be used for scratch card, 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
Hot Tools
PHP library for dependency injection containers
PHP library for dependency injection containers
A collection of 50 excellent classic PHP algorithms
Classic PHP algorithm, learn excellent ideas and expand your thinking
Small PHP library for optimizing images
Small PHP library for optimizing images