An example of winning probability algorithm for PHP big turntable

  • 2021-07-22 09:19:27
  • OfStack

This article describes the example of PHP big turntable winning probability algorithm, sharing for your reference. The details are as follows:

The big turntable is an interesting thing in many online games recently. Let's take a look at the algorithm and example of winning probability of this big turntable, hoping to help you.

This is an APP client has a big turntable lottery algorithm, and how to draw the lottery is of course realized in our server. Let's briefly share the implementation code under 1:

header("Content-type: text/html; charset=utf-8");
$prize_arr = array(
'0' => array('id'=>1,'prize'=>' Tablet PC ','v'=>1),
'1' => array('id'=>2,'prize'=>' Digital camera ','v'=>5),
'2' => array('id'=>3,'prize'=>' Sound box equipment ','v'=>10),
'3' => array('id'=>4,'prize'=>'4G USB flash drive ','v'=>12),
'4' => array('id'=>5,'prize'=>'10Q Currency ','v'=>22),
'5' => array('id'=>6,'prize'=>' Maybe you can win next time ','v'=>50),
);
 
$actor = 100;
 
foreach ($prize_arr as $v) {
$arr[$v['id']] = $v['v'];
}
foreach ($arr as &$v) {
$v = $v*$actor;
}
asort($arr);
$sum = array_sum($arr);   // Total probability
 
$rand = mt_rand(1,$sum);
 
$result = '';    // Winning product id
 
foreach ($arr as $k => $x)
{
if($rand <= $x)
{
$result = $k;
break;
}
else
{
$rand -= $x;
}
}
$res['yes'] = $prize_arr[$result-1]['prize']; // A winning prize
print_r($res);

I hope this article is helpful to everyone's PHP programming.


Related articles: