欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP+AJAX开发幸运大转盘抽奖

程序员文章站 2022-05-18 22:46:46
PHP+AJAX开发幸运大转盘抽奖,通过奖品库存、中奖次数来计算中奖概率 奖品设置 获取中奖概率,若是库存为0的则不计算 本文转自:http://www.sucaihuo.com/php/3301.html 转载请注明出处! ......

php+ajax开发幸运大转盘抽奖,通过奖品库存、中奖次数来计算中奖概率

PHP+AJAX开发幸运大转盘抽奖

奖品设置

 1 $prizes = array( 
 2         0 => array( 
 3             "id" => 0, //中奖id 
 4             "name" => "谢谢参与", //奖品名称 
 5             "count" => "100", //库存 
 6         ), 
 7         1 => array( 
 8             "id" => 1, 
 9             "name" => "罗浮山门票", 
10             "count" => "40", 
11         ), 
12         2 => array( 
13             "id" => 2, 
14             "name" => "罗浮山嘉宝田温泉体验券", 
15             "count" => "40", 
16         ), 
17         3 => array( 
18             "id" => 3, 
19             "name" => "精美旅游书籍《山水酿惠州》", 
20             "count" => "40", 
21         ), 
22         4 => array( 
23             "id" => 4, 
24             "name" => "碧海湾漂流门票", 
25             "count" => "40", 
26         ), 
27         5 => array( 
28             "id" => 5, 
29             "name" => "南昆山门票", 
30             "count" => "40", 
31         ), 
32         6 => array( 
33             "id" => 6, 
34             "name" => "云顶温泉精美礼品", 
35             "count" => "40", 
36         ), 
37     );

 

获取中奖概率,若是库存为0的则不计算

 1 function getrand($proarr, $procount) { 
 2     $result = ''; 
 3     $prosum = 0; 
 4     foreach ($procount as $key => $val) { 
 5         if ($val <= 0) { 
 6             continue; 
 7         } else { 
 8             $prosum = $prosum + $proarr[$key]; 
 9         } 
10     } 
11     foreach ($proarr as $key => $procur) { 
12         if ($procount[$key] <= 0) { 
13             continue; 
14         } else { 
15             $randnum = mt_rand(1, $prosum); 
16             if ($randnum <= $procur) { 
17                 $result = $key; 
18                 break; 
19             } else { 
20                 $prosum -= $procur; 
21             } 
22         } 
23     } 
24     unset($proarr); 
25     return $result; 
26 }

本文转自: 转载请注明出处!