php随机抽奖实例分析
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了php随机抽奖实现方法,实例分析了php抽奖类lottery_tool及其具体使用技巧,需要的朋友可以参考下
本文实例讲述了php随机抽奖用法。分享给大家供大家参考。具体分析如下
1. 按照设定的概率,得到随机抽奖的结果。
代码如下:
<?php
/
抽奖工具
/
class lottery_tool {
protected static $awardsArr;
protected static $proField = 'probability';
protected static $proSum = 0;
protected static $checkAward = false;
const SUCCESS_CODE = 0;
const FAIL_CODE = -1;
//检查抽奖数据
protected static function checkAwards(){
if (!is_array(self::$awardsArr) || empty(self::$awardsArr)) {
return self::$checkAward = false;
}
self::$proSum = 0;
foreach (self::$awardsArr as $_key => $award) {
self::$proSum += $award[self::$proField];
}
if (empty(self::$proSum)) {
return self::$checkAward = false;
}
return self::$checkAward = true;
}
protected static function suessRoll($rollKey){
return array('code' => self::SUCCESS_CODE, 'roll_key' => $rollKey, 'msg' => 'roll suess');
}
protected static function failRoll($msg = 'roll fail'){
return array('code' => self::FAIL_CODE, 'msg' => $msg );
}
//抽奖
public static function roll () {
if (false == self::$checkAward) {
return self::failRoll('awards data is not the right format!');
}
$result = mt_rand(0, self::$proSum);
$proValue = 0;
foreach (self::$awardsArr as $_key => $value) {
$proValue += $value[self::$proField];
if ($result <= $proValue) {
return self::suessRoll($_key);
}
}
return self::failRoll('wrong');
}
//改变概率字段名
public static function setProField($field = null) {
if (!empty($field)) {
self::$proField = $field;
}
}
//设置奖品
public static function setAwards($awards){
self::$awardsArr = $awards;
self::checkAwards();
}
}
/
抽奖工具
/
class lottery_tool {
protected static $awardsArr;
protected static $proField = 'probability';
protected static $proSum = 0;
protected static $checkAward = false;
const SUCCESS_CODE = 0;
const FAIL_CODE = -1;
//检查抽奖数据
protected static function checkAwards(){
if (!is_array(self::$awardsArr) || empty(self::$awardsArr)) {
return self::$checkAward = false;
}
self::$proSum = 0;
foreach (self::$awardsArr as $_key => $award) {
self::$proSum += $award[self::$proField];
}
if (empty(self::$proSum)) {
return self::$checkAward = false;
}
return self::$checkAward = true;
}
protected static function suessRoll($rollKey){
return array('code' => self::SUCCESS_CODE, 'roll_key' => $rollKey, 'msg' => 'roll suess');
}
protected static function failRoll($msg = 'roll fail'){
return array('code' => self::FAIL_CODE, 'msg' => $msg );
}
//抽奖
public static function roll () {
if (false == self::$checkAward) {
return self::failRoll('awards data is not the right format!');
}
$result = mt_rand(0, self::$proSum);
$proValue = 0;
foreach (self::$awardsArr as $_key => $value) {
$proValue += $value[self::$proField];
if ($result <= $proValue) {
return self::suessRoll($_key);
}
}
return self::failRoll('wrong');
}
//改变概率字段名
public static function setProField($field = null) {
if (!empty($field)) {
self::$proField = $field;
}
}
//设置奖品
public static function setAwards($awards){
self::$awardsArr = $awards;
self::checkAwards();
}
}
2. 示例代码
代码如下:
$awards = array(
'0' => array('pro' => 15, 'info' => '15%的可能性'),
'1' => array('pro' => 25, 'info' => '25%的可能性'),
'2' => array('pro' => 40, 'info' => '40%的可能性'),
'3' => array('pro' => 20, 'info' => '20%的可能性'),
);
lottery_tool::setProField('pro');
lottery_tool::setAwards($awards);
$result = array();
for ($i = 10000; $i --;) {
$result[] = lottery_tool::roll();
}
foreach ($result as $key => $value) {
$awards[$value['roll_key']]['num'] ++;
}
echo '<pre>';
var_dump($awards);
'0' => array('pro' => 15, 'info' => '15%的可能性'),
'1' => array('pro' => 25, 'info' => '25%的可能性'),
'2' => array('pro' => 40, 'info' => '40%的可能性'),
'3' => array('pro' => 20, 'info' => '20%的可能性'),
);
lottery_tool::setProField('pro');
lottery_tool::setAwards($awards);
$result = array();
for ($i = 10000; $i --;) {
$result[] = lottery_tool::roll();
}
foreach ($result as $key => $value) {
$awards[$value['roll_key']]['num'] ++;
}
echo '<pre>';
var_dump($awards);
运行结果如下
代码如下:
array
0 =>
array
'pro' => int 15
'info' => string '15%的可能性' (length=15)
'num' => int 1596
1 =>
array
'pro' => int 25
'info' => string '25%的可能性' (length=15)
'num' => int 2484
2 =>
array
'pro' => int 40
'info' => string '40%的可能性' (length=15)
'num' => int 3939
3 =>
array
'pro' => int 20
'info' => string '20%的可能性' (length=15)
'num' => int 1981
0 =>
array
'pro' => int 15
'info' => string '15%的可能性' (length=15)
'num' => int 1596
1 =>
array
'pro' => int 25
'info' => string '25%的可能性' (length=15)
'num' => int 2484
2 =>
array
'pro' => int 40
'info' => string '40%的可能性' (length=15)
'num' => int 3939
3 =>
array
'pro' => int 20
'info' => string '20%的可能性' (length=15)
'num' => int 1981
希望本文所述对大家的php程序设计有所帮助。
上一篇:php银联网页支付实现方法
下一篇:php二维数组合并及去重复的方法
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程