php实现概率性随机抽奖代码
1、初始数据:
权重越大,抽取的几率越高
[奖品1, 权重 5], [ 奖品2, 权重6], [ 奖品3, 权重 7], [ 奖品4, 权重2] 2、处理步骤:
1)N = 5 + 6 + 7 + 2 = 20
2)然后取1-N的随机数M
3)界定各 奖品的权重范围值 奖品 1 : 1-5 ; 奖品2 : 6-11; 奖品3: 12-18; 奖品4: 19-20
4) 如果M在某个奖品的权重范围值内,标识这个奖品被抽取到 <?php
/**
* 奖品
*/
class Prize {
# ID
public $id = null;
# 权重
public $weight = null;
# 奖品名
public $name = null; # 权重范围区间起始值
protected $start = 0;
# 权重范围区间结束值
protected $end = 0; public function __construct($id, $weight, $name) {
if (!$id) {
throw new Exception('奖品ID为空.');
}
$this->id = $id;
$this->weight = $weight ? $weight : 0;
$this->name = $name ? $name : '随机奖品' . $id;
} # id
public function getId() {
return $this->id;
} # 权重
public function getWeight() {
return $this->weight;
} # 设置权重范围区间
public function setRange($start, $end) {
$this->start = $start;
$this->end = $end;
} # 判断随机数是否在权重范围区间
public function inRange($num) {
return ($num >= $this->start) && ($num <= $this->end);
}
} /**
* 奖品池
*/
class PrizePoll implements IteratorAggregate, Countable {
# 奖品集
protected $items = array(); # 加入奖品
public function addItem(Prize $item) {
$this->items[$item->getId()] = $item;
return $this;
} # 删除奖品
public function removeItem($itemId) {
if (isset($this->items[$itemId])) {
unset($this->items[$itemId]);
}
return $this;
} # 更新奖品
public function updateItem(Prize $item) {
if (isset($this->items[$item->getId()])) {
$this->items[$item->getId()] = $item;
}
return $this;
} # 获取所有奖品
public function getItems() {
return $this->items;
} # 所有所有可用奖品(如果权重为0,说明这个奖品永远不可能抽到)
public function getVisibleItems() {
$items = array();
foreach ($this->items as $item) {
if ($item->getWeight()) {
$items[$item->getId()] = $item;
}
}
return $items;
} # Countable::count
public function count() {
return count($this->items);
} # IteratorAggregate::getIterator()
public function getIterator() {
return new ArrayIterator($this->items);
}
} /**
* 简单的抽奖类
*/
class SimpleTurn {
# 奖池
protected $poll = null; public function __construct(PrizePoll $poll) {
if ($poll) {
$this->setPoll($poll);
}
} # 抽奖
public function run(PrizePoll $poll) {
$poll = $poll ? $poll : $this->poll;
if ( ! $poll) {
throw new Exception('奖池未初始化');
} if ($poll->count() <= 0) {
throw new Exception('奖池为空');
} $items = $poll->getVisibleItems();
if (count($items) <= 0) {
throw new Exception('奖池为空');
} $sum = 0;
foreach ($items as $item) {
$start = $sum + 1;
$sum += $item->getWeight();
$end = $sum; # 设置奖品的权重范围区间
$item->setRange($start, $end);
} # 随机数
$rand = $this->getRandNum(1, $sum); # 区间段判断
foreach ($items as $item) {
if ($item->inRange($rand)) {
return $item;
}
}
return null;
} # 获取随机数
public function getRandNum($min, $max) {
return mt_rand($min ? $min : 1, $max);
} # 设置奖池
public function setPoll(PrizePoll $poll) {
$this->poll = $poll;
}
} # 示例
try {
$prizePoll = new PrizePoll();
$prizePoll->addItem(new Prize(1, 5))
->addItem(new Prize(2, 6))
->addItem(new Prize(3, 7))
->addItem(new Prize(4, 2)); $turn = new SimpleTurn($prizePoll);
$prize = $turn->run();
var_dump($prize);
} catch (Exception $e) {
print_r($e);
}
php实现概率性随机抽奖代码的更多相关文章
- php按照奖品百分比随机抽奖代码分析
这个忘记从哪里copy过来了 /** * 概率算法 * @param array $probability * @return integer|string */ function get_rand( ...
- jquery.rotate.js实现可选抽奖次数和中奖内容的转盘抽奖代码
需求: 抽奖代码最多可以抽奖5次,而且,每次只会中“2000元理财金”或者“谢谢参与”,其它的不会抽中(哈哈,果然都是套路). 效果如下: 一.页面结构: ? 1 2 3 4 5 6 7 8 9 10 ...
- js手机号批量滚动抽奖代码实现
我们平时在看一些选秀节目或一些歌唱类比赛节目时经常会看到在现场的大屏幕上会有观众的手机号在滚动来选出谁是幸运观众或谁中了什么奖项,这些手机号都是现场观众或场外观众在给选手投票时产生的,当主持人一声开始 ...
- MT6755 使用R63350 IC 出现唤醒概率性闪白,并导致ESD FAIL
现象描述. 手机自动灭屏后按power键或home 键点亮屏幕,概率性上方有白色的一道,还会闪两三下屏.使用的LCM IC是:r63350, (FHD VDO)屏,附件为mtklog看看是什么原因? ...
- JQ广告弹窗&随机抽奖————JQ
1.JQ广告弹窗 <div id="flo"> <img src="image.jpeg"> </div> <scri ...
- 解Bug之路-记一次调用外网服务概率性失败问题的排查
前言 和外部联调一直是令人困扰的问题,尤其是一些基础环境配置导致的问题.笔者在一次偶然情况下解决了一个调用外网服务概率性失败的问题.在此将排查过程发出来,希望读者遇到此问题的时候,能够知道如何入手. ...
- C#实现随机抽奖和冒泡排序
随机抽奖程序 string[] s = new string[] { "A", "B", "C", "D", " ...
- javascript 转盘抽奖代码和计数器代码
要介绍了javascript圆盘抽奖程序实现原理和完整代码例子,需要的朋友可以参考下 看到网页上有不少大转盘抽奖的应用,心血来潮也想弄个.于是找了点资料自己研究... 效果预览: 一.模拟抽奖的实 ...
- php抽奖代码
1.经典概率算法抽奖 $tmpItems = ['电脑'=>10, '相机'=>50, '100元现金'=>500]; $proSum = array_sum($tmpItems); ...
随机推荐
- pandas简短介绍
1.数据结构 维数 名称 描述 1 Series 一维带标签单一数据类型的数组 2 DataFrame 不同数据类型的列 2.十分钟学习pandas 2.1.导入所需模块 import pandas ...
- LoadRunner进行参数化的九种方式取值和连接数据库取值
一.连接mysql数据库取值 1.首先安装odbc驱动 链接: https://pan.baidu.com/s/1WAYd4ygQqIrbB08S01hSkg 提取码: gdfs 2.操作步骤如下图: ...
- springboot(十二):springboot单元测试、打包部署
单元测试 1.在pom包中添加spring-boot-starter-test包引用 <dependency> <groupId>org.springframework.boo ...
- Spring Boot 启动过程及 自定义 Listener等组件
一.启动过程 二.自定义组件 package com.example.jdbc.listener; import org.springframework.context.ApplicationCont ...
- Win10 x64 + CUDA 10.0 + cuDNN v7.5 + TensorFlow GPU 1.13 安装指南
Win10 x64 + CUDA 10.0 + cuDNN v7.5 + TensorFlow GPU 1.13 安装指南 Update : 2019.03.08 0. 环境说明 硬件:Ryzen R ...
- Linux - 文件操作
touch file # 创建空白文件 rm -rf 目录名 # 不提示删除非空目录(-r:递归删除 -f强制) dos2unix # windows文本转linux文本 unix2dos # lin ...
- MySQL 之 数据库自动生成ID格式化编号(字符串格式化填充/拼接/时间)
# 用户账号:1-4位:入职年份:5-6位:入职月份:7-11位:员工入职顺序号 select concat( date_format(now(),'%Y'), date_format(now(),' ...
- js 获取当前日期或者前、后N天yyyy-MM-dd的方法
//js获取当前日期.当前日期前.后N天的标准年月日 //day=0为当前天,day=7为前7天,day=-7为当前日期的后7天 function getstartdate(day) { ...
- 推荐几款在Windows中比较好用的软件
gif录制软件:LICEcap 下载地址:https://www.cockos.com/licecap/ 演示
- python - 发送邮件(email模块(内置))
发送邮件 import smtplib from email.mime.text import MIMEText #邮箱件内容 HTML = """ 发送邮件测试2,加密 ...