Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the probability algorithm of lottery program by PHP

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "PHP how to realize the probability algorithm of lottery program". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Algorithm 1

The copy code is as follows:

/**

* total probability calculation

*

* @param array $p array('a'=>0.5,'b'=>0.2,'c'=>0.4)

* @return string Returns the key of the above array

*/

function random($ps){

static $arr = array();

$key = md5(serialize($ps));

if (! isset($arr[$key])) {

$max = array_sum($ps);

foreach ($ps as $k=>$v) {

$v = $v / $max * 10000;

for ($i=0; $i $proCur) {

$randNum = mt_rand(1, $proSum);

if ($randNum array ('id '=>1,'prize'=>' tablet','v'=>1),

'1' => array ('id '=>2,'prize'=>' digital camera','v'=>5),

'2' => array ('id '=>3,'prize'=>' slightly equipment','v'=>10),

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

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

'5' => array ('id '=>6,'prize'=>' maybe next time,'v'=>50),

);

is a two-dimensional array that records all the prize information for this lottery, where id represents the winning grade, prize represents the prize, and v represents the winning probability. Note that v must be an integer. You can set v to 0 for the corresponding award, which means that the probability of winning the award is 0. The sum (cardinality) of v in the array, the larger the cardinality, the more accurate the probability is. In this case, if v adds up to 100, then the tablet has a 1 percent chance of winning. If v adds up to 10000, then the probability of winning is 1 in 10000.

PHP loops through the prize-setting array for each front-end page request, using the probability calculation function get_rand to get the winning prize-id. The winning prizes are stored in the array $res ['yes '], and the rest of the unwinning information is stored in $res ['no']. Finally, the json number data is output to the front page.

The copy code is as follows:

//If the winning data is stored in the database, the number of winners needs to be determined here.

//In the 1st, 2nd and 3rd prizes, if the maximum number is reached, the corresponding prize will be unset to avoid repeating the big prize.

//code here eg:unset($prize_arr['0'])

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

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

}

$rid = get_rand($arr); //get prize id based on probability

$res <$'yes '] = $prize_arr[$rid-1]<$'prize']; //winning prize

//Remove the winning items from the array, leaving the unwinning items. If it is database verification, you can omit them here.

unset($prize_arr[$rid-1]);

shuffle($prize_arr); //shuffle array

for($i=0;$i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report