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); ...
随机推荐
- ping的作用
Ping是潜水艇人员的专用术语,表示回应的声纳脉冲,在网络中Ping 是一个十分好用的TCP/IP工具.它主要的功能是用来检测网络的连通情况和分析网络速度. Ping有好的善的一面也有恶的一面.先说一 ...
- POJ1742Coins(多重背包)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 32309 Accepted: 10986 Descripti ...
- 8、JPA-映射-双向一对一
一个管理对应一个部门,一个部门对应一个管理,例中由部门维护关联关系 实体类 Department package com.jpa.yingshe; import javax.persistence.* ...
- Hive记录-使用Hue管理Hive元数据
Hue是一个开源的Apache Hadoop UI系统,由Cloudera Desktop演化而来,最后Cloudera公司将其贡献给Apache基金会的Hadoop社区,它是基于Python Web ...
- Windows环境墙内搭建Go语言集成开发环境
1 安装go环境 太简单略 2 安装vs code 找到微软的官方网站,下载Visual Studio Code,官网地址https://code.visualstudio.com/ 安装完成后进入V ...
- Executor, ExecutorService 和 Executors 间的不同
java.util.concurrent.Executor, java.util.concurrent.ExecutorService, java.util.concurrent. Executors ...
- 996ICU与程序猿的个人成长
目录 规划 学习 专业领域知识 知识广度 第二职业 理财 借势 添砖加瓦 最近一段时间,996ICU在互联网界引发"大地震",从普通员工.行业大佬甚至官媒都进行了发声,大家对这个问 ...
- Ubuntu 16.04及以上 安装/卸载 Docker-CE
前言 本文仅针对Ubuntu 18.10.18.04.16.04的x86_64的OS与架构下的Docker-CE的安装 卸载老板本 如果已安装,请卸载它们: sudo apt-get remove d ...
- DIV仿textarea文本域,contenteditable如何只能输入纯文本
对于支持HTML5浏览器有2种方法: 1. HTML5 <div contenteditable="plaintext-only"></div> 2. C ...
- linux 网络之 bond 网卡模式
Linux bond模式通过多个网卡绑定技术既能增加服务器的可靠性,又增加了可用网络宽带,为用户提供不间断的网络服务: 七种bond模式: 第一种模式:mod=0 ,即:(balance-rr) Ro ...