• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

php抽奖算法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
<?php
 require '../vendor/autoload.php';

class Draw
{
    //奖品池
    public static $prizes = [
        ['id' => 1, 'name' => 'iphone', 'num' => 1, 'surplusNum' => 1],
        ['id' => 2, 'name' => '小米max', 'num' => 100, 'surplusNum' => 100],
        ['id' => 3, 'name' => '小米3', 'num' => 200, 'surplusNum' => 200],
        ['id' => 4, 'name' => '山寨机', 'num' => 1000, 'surplusNum' => 1000],
        ['id' => 5, 'name' => '安慰奖', 'num' => 50000, 'surplusNum' => 50000],
    ];
    //参加人数
    public static $maxNum = 0;
    //设置中奖名单
    public static $luckList = [];

    /**
     * 设定权重
     */
    public function settingDraw()
    {
        return array(
            array('id' => 1, 'name' => '一等奖', 'pid' => 1, 'v' => 1.5),
            array('id' => 2, 'name' => '二等奖', 'pid' => 2, 'v' => 2.5),
            array('id' => 3, 'name' => '三等奖', 'pid' => 3, 'v' => 10),
            array('id' => 4, 'name' => '四等奖', 'pid' => 4, 'v' => 100),
            array('id' => 5, 'name' => '五等奖', 'pid' => 5, 'v' => 100000),
            array('id' => 6, 'name' => '没中奖', 'pid' => 6, 'v' => 1000000)
        );
    }

    /**
     * 获得第一次抽奖结果
     * @param $proArr
     * @return array
     */
    public function drawResultOne($proArr)
    {
        $result = array();
        foreach ($proArr as $key => $val) {
            $arr[$key] = $val['v'];
        }
        // 概率数组的总概率
        $proSum = array_sum($arr);
        asort($arr);
        // 概率数组循环
        foreach ($arr as $k => $v) {
            $randNum = mt_rand(1, $proSum);
            if ($randNum <= $v) {
                $result = $proArr[$k];
                break;
            } else {
                $proSum -= $v;
            }
        }
        return $result;
    }

    /**
     *
     * 获取抽奖结果
     */
    public function run($user)
    {
        self::$maxNum += 1;
        //获取权重设置
        $drawRules = $this->settingDraw();
        //获得第一次抽奖结果
        $drawResult = $this->drawResultOne($drawRules);
        //读取奖池
        if ((array_search($drawResult, $drawRules) + 1) == count($drawRules)) {
            $user['get'] = $drawResult['name'];
            return $user;
        } else {
            foreach (self::$prizes as $k => &$v) {
                if ($drawResult['pid'] == $v['id']) {
                    if ($v['surplusNum'] <= 0) {
                        $user['get'] = '没中奖';
                        break;
                    } else {
                        array_push(self::$luckList, ['username' => $user['name'], 'good' => $drawResult['name'].'---'.$v['name']]);
                        $user['get'] = $drawResult['name'];
                        $v['surplusNum'] -= 1;
                    }
                }
            }
            return $user;
        }
    }

}

function dd($data)
{
    echo "<pre/>";
    print_r($data);
}


//随机生成用户数量
$users = [];
$userNum = mt_rand(1000, 100000);
$users = array_pad($users, $userNum, ['id' => '', 'name' => '', 'get' => '']);
//$users = array_chunk($users,100);
$obj = new Draw();
foreach ($users as $k => $v) {
    $v['id'] = $k + 1;
    $v['name'] = 'user' . ((string)$k + 1);
    $users[$k] = $obj->run($v);
}

//中奖结果
dd($users);
echo "<hr/>";
//奖品剩余情况
dd($obj::$prizes);
////参加人数
echo "<hr/>";
dd($obj::$maxNum);
////获奖名单
echo "<hr/>";
dd($obj::$luckList);

  计算部分不是原创 参考 https://segmentfault.com/a/1190000007431893


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php下载文件发布时间:2022-07-10
下一篇:
php 下载图片到服务器发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap