业务需求,所以做了一个简单的原型测试,功能的主要需求是:用户账户内金额越高的用户中奖的机率就越大。
此脚本只能在命令行下使用,使用示例:
php lucky.php 100
-
/**
- * 抽奖程序
- *
- * @File: lucky.php
- * @Author: zzxworld
- * @Email: zzxworld@gmail.com
- * @Date: 2012-05-09
- */
-
- $timer = microtime(true);
-
- /** 手动配置的参数 **/
- # 奖池大小
- $lucky_size = 1000;
- # 最高金额用户的中奖机率,不能超过奖池大小
- $star_user_rate = 100;
- # 一等奖奖金
- $lucky_first = 100;
- # 二等奖奖金
- $lucky_second = 50;
- # 三等奖奖金
- $lucky_third = 25;
- # 普通奖最大金额
- $lucky_normal_max = 10;
- # 普通奖最小金额
- $lucky_normal_min = 1;
-
- /** 自动从系统取值的参数 **/
- # 奖金总金额
- $lucky_amount = 578;
- # 拥有最多资产的用户的账户金额
- $star_user_amount = 134;
-
- # 当前用户金额
- $user_amount = isset($argv[1]) ? intval($argv[1]) : 0;
- if ($user_amount die('请在命令后面设置一个用户金额.' . chr(10));
- }
-
- # 计算当前用户中奖机率
- $user_rate = $star_user_rate/$lucky_size/$star_user_amount*$user_amount;
-
- # 根据中奖机率计算奖金个数
- $lucky_num = intval($user_rate * $lucky_size);
-
- # 初始化奖池
- $lucky_pool = array_fill(0, $lucky_size, 0);
-
- # 中奖机率小于1的调整为1
- if ($lucky_num $lucky_num = 1;
- }
-
- # 安排奖池中的奖项
- $lucky_items = array('first', 'second', 'third', 'normal');
- $lucky_num += 3;
-
- # 按奖金总额去除一,二,三等奖项
- if ($lucky_amount $lucky_num -= 1;
- unset($lucky_items['first']);
- }
-
- if ($lucky_amount $lucky_num -= 1;
- unset($lucky_items['second']);
- }
-
- if ($lucky_amount $lucky_num -= 1;
- unset($lucky_items['third']);
- }
-
- # 在奖池中随机生成奖项
- foreach(array_rand($lucky_pool, $lucky_num) as $key) {
- $name = $lucky_items[array_rand($lucky_items)];
- if (!isset($create_lucky_first) && $name == 'first') {
- $lucky_pool[$key] = $lucky_first;
- $create_lucky_first = true;
- continue;
- }
-
- if (!isset($create_lucky_second) && $name == 'second') {
- $lucky_pool[$key] = $lucky_second;
- $create_lucky_second = true;
- continue;
- }
-
- if (!isset($create_lucky_third) && $name == 'third') {
- $lucky_pool[$key] = $lucky_third;
- $create_lucky_third = true;
- continue;
- }
-
- if ($name == 'normal') {
- $lucky_pool[$key] = rand($lucky_normal_min, $lucky_normal_max);
- }
-
- }
-
- # 抽奖
- $result = $lucky_pool[rand(0, $lucky_size)];
-
- echo '奖池大小:' . $lucky_size . chr(10);
- echo '奖金总金额:' . $lucky_amount . chr(10);
- echo '一等奖奖金:' . $lucky_first . chr(10);
- echo '二等奖奖金:' . $lucky_second . chr(10);
- echo '三等奖奖金:' . $lucky_third . chr(10);
- echo '普通奖奖金:' . $lucky_normal_min . ' - ' . $lucky_normal_max . chr(10);
- echo '明星用户资金总额:' . $star_user_amount . chr(10);
- echo '明星用户中奖机率:' . ($star_user_rate/$lucky_size) . chr(10) . chr(10);
-
- echo '当前用户资金总额:' . $user_amount . chr(10);
- echo '当前用户中奖机率:' . $user_rate . chr(10);
- echo '当前用户奖池中的奖项:';
- foreach($lucky_items as $name) {
- switch($name) {
- case 'first':
- echo '一等奖(1),';
- $lucky_num--;
- break;
- case 'second':
- echo '二等奖(1),';
- $lucky_num--;
- break;
- case 'third':
- echo '三等奖(1),';
- $lucky_num--;
- break;
- case 'normal':
- echo '普通奖(' . $lucky_num . ')';
- break;
- }
- }
- echo chr(10);
- echo '当前用户抽奖结果:';
- if ($result == $lucky_first) {
- echo '一等奖 ' . $result . '元';
- } elseif($result == $lucky_second) {
- echo '二等奖 ' . $result . '元';
- } elseif($result == $lucky_third) {
- echo '三等奖 ' . $result . '元';
- } elseif($result >= $lucky_normal_min && $result echo '普通奖 ' . $result . '元';
- } else {
- echo '没有中奖';
- }
- echo chr(10);
- echo chr(10);
-
- echo '执行耗时:' . (microtime(true) - $timer) . '秒' . chr(10);
复制代码
|