大转盘php程序CI框架抽奖程序随机概率
function bingo()
{
//probability
$where = ['stock>'=>0];
$result = $this->db->order_by("probability asc")->get_where('dzp_options', $where)->result_array();
// echo $this->db->last_query();
// print_r($result);
$all_nums = .0;
$result_new = [];//新数组 [id=>[0, 10], id=>[10, 300], id=>[300, 1000]];判断中奖所属区间
$begin = 0;//上一个 all: 1,1,3,5; [0,1], [1, 2], [2, 5], [5, 10],
foreach ($result as $row)
{
$end = $begin+$row['probability'];
$result_new[$row['id']] = [$begin, $end];
$begin += $row['probability'];
$all_nums += $row['probability'];
}
$rand_nums = rand(0, $all_nums);
// echo $all_nums.'<br>';
// echo $rand_nums;
// print_r($result_new);
$bingo_id = FALSE;
foreach ($result_new as $id=>$row)
{
$begin = $row[0];
$end = $row[1];
if(($rand_nums>$begin) && ($rand_nums<=$end))
{
$bingo_id = $id;
break;
}
}
if(!$bingo_id)
{
exit('活动已结束');
}
$row_bingo = $this->db->get_where('dzp_options', ['id'=>$bingo_id])->result_array();
print_r($row_bingo);exit;
return $bingo;
}
表:
CREATE TABLE `lm_dzp_options` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(50) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`detail` text,
`type` varchar(50) DEFAULT NULL,
`tcode` text,
`probability` int(255) DEFAULT NULL,
`stock` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of lm_dzp_options
-- ----------------------------
INSERT INTO `lm_dzp_options` VALUES ('1', 'test1', '1', null, null, null, '16', '10');
INSERT INTO `lm_dzp_options` VALUES ('2', 'test1', '2', null, null, null, '26', '10');
INSERT INTO `lm_dzp_options` VALUES ('3', 'test1', '3', null, null, null, '360', '30');
INSERT INTO `lm_dzp_options` VALUES ('4', 'test1', '4', null, null, null, '5600', '50');
INSERT INTO `lm_dzp_options` VALUES ('5', 'test1', '5', null, null, null, '76000', '100');
INSERT INTO `lm_dzp_options` VALUES ('6', 'test1', '6', null, null, null, '6000', '100');
用tp5改了下代码,增加测试函数方法
<?php
namespace app\index\controller;
use app\common\controller\Frontend;
class Index extends Frontend
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
protected $layout = '';
public function index()
{
return $this->view->fetch();
}
function bingo()
{
$where['stock'] = ['>',0];
$result = db('dzp')->where($where)->order("probability asc")->select();
// print_r($result);exit;
$all_nums = .0;
$result_new = [];//新数组 [id=>[0, 10], id=>[10, 300], id=>[300, 1000]];判断中奖所属区间
$begin = 0;//上一个 all: 1,1,3,5; [0,1], [1, 2], [2, 5], [5, 10],
$end = 0;
foreach ($result as $row)
{
$end = $begin+$row['probability'];
$result_new[$row['id']] = [$begin, $end];
$begin += $row['probability'];
$all_nums += $row['probability'];
}
$rand_nums = rand(1, $all_nums);
$bingo_id = FALSE;
foreach ($result_new as $id=>$row)
{
$begin = $row[0];
$end = $row[1];
if(($rand_nums>$begin) && ($rand_nums<=$end))
{
$bingo_id = $id;
break;
}
}
if(!$bingo_id)
{
echo 'rand_nums:';
print_r($rand_nums);
echo 'begin:';
print_r($begin);
echo 'end:';
print_r($end);
exit('活动已结束');
}
$row_bingo = db('dzp')->where(['id'=>$bingo_id])->find();
// print_r($row_bingo);exit;
return $row_bingo;
}
/**
* 测试 循环1000 次 看下概率分配
*/
function aa()
{
$show = [];
for($i=0; $i<1000; $i++)
{
$bg = $this->bingo();
if(!isset($show[$bg['id']])) $show[$bg['id']] = 0;
$show[$bg['id']]++;
}
print_r($show);
}
}