大转盘php程序CI框架抽奖程序随机概率

大转盘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);
    }




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值