php实现概率性随机抽奖代码

 更新时间:2016年01月02日 15:51:14   投稿:hebedich  
本文给大家分享的是使用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对接API安全性的常见方法

    保证PHP对接API安全性的常见方法

    在对接API时,保证其安全性是非常重要的,本文给大家介绍了一些保证PHP对接API安全性的常见措施,文中有详细的代码示例供大家参考,需要的朋友可以参考下
    2023-11-11
  • PHP递归的三种常用方式

    PHP递归的三种常用方式

    今天小编就为大家分享一篇关于PHP递归的三种常用方式,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • PHP编程函数安全篇

    PHP编程函数安全篇

    关于脚本安全这个话题好像永远没完没了,如果你经常到国外的各种各样的bugtraq上,你会发现有一半以上都和脚本相关,诸如SQL injection,XSS,Path Disclosure,Remote commands execution这样的字眼比比皆是,我们看了之后的用途难道仅仅是抓肉鸡?
    2013-01-01
  • PHP滚动日志的代码实现

    PHP滚动日志的代码实现

    所谓滚动日志,顾名思义,就是记录一个模块的日志用一系列的日志文件,同一模块文件个数有限制,最多maxNum个,大小也有限制,最大maxSize字节,文件名有一定的命名方式,比如:testlog.log、testlog_1.log,testlog_2.log、、、、、、
    2015-06-06
  • PHP_Cooikes不同页面无法传递的解决方法

    PHP_Cooikes不同页面无法传递的解决方法

    这篇文章主要介绍了PHP_Cooikes不同页面无法传递的解决方法,需要的朋友可以参考下
    2014-03-03
  • PHP编写文件多服务器同步程序

    PHP编写文件多服务器同步程序

    这篇文章主要为大家详细介绍了PHP编写文件多服务器同步程序的相关资料,感兴趣的朋友可以参考一下
    2016-07-07
  • PHP中常见文本文件操作总结

    PHP中常见文本文件操作总结

    在PHP中,我们可以使用一些内置的函数来进行文本文件操作,本文主要为大家详细介绍了一些常见的文本文件操作,有需要的小伙伴可以参考一下
    2023-12-12
  • PHP5.5在windows安装使用memcached服务端的方法

    PHP5.5在windows安装使用memcached服务端的方法

    这篇文章主要介绍了PHP5.5在windows安装使用memcached服务端的方法,需要的朋友可以参考下
    2014-04-04
  • PHP Post获取不到非表单数据的问题解决办法

    PHP Post获取不到非表单数据的问题解决办法

    这篇文章主要介绍了PHP Post获取不到非表单数据的问题的解决办法以及代码分享,需要的朋友参考下。
    2018-02-02
  • PHP超级全局变量【$GLOBALS,$_SERVER,$_REQUEST等】用法实例分析

    PHP超级全局变量【$GLOBALS,$_SERVER,$_REQUEST等】用法实例分析

    这篇文章主要介绍了PHP超级全局变量用法,结合实例形式分析了PHP中$GLOBALS,$_SERVER,$_REQUEST等超级全局变量相关概念、功能、使用方法及操作注意事项,需要的朋友可以参考下
    2019-12-12

最新评论