当前位置:Gxlcms > PHP教程 > php实现概率性随机抽奖代码,php概率抽奖代码_PHP教程

php实现概率性随机抽奖代码,php概率抽奖代码_PHP教程

时间:2021-07-01 10:21:17 帮助过:9人阅读

php实现概率性随机抽奖代码,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在某个奖品的权重范围值内,标识这个奖品被抽取到

  1. <?php
  2. /**
  3. * 奖品
  4. */
  5. class Prize {
  6. # ID
  7. public $id = null;
  8. # 权重
  9. public $weight = null;
  10. # 奖品名
  11. public $name = null;
  12. # 权重范围区间起始值
  13. protected $start = 0;
  14. # 权重范围区间结束值
  15. protected $end = 0;
  16. public function __construct($id, $weight, $name) {
  17. if (!$id) {
  18. throw new Exception('奖品ID为空.');
  19. }
  20. $this->id = $id;
  21. $this->weight = $weight ? $weight : 0;
  22. $this->name = $name ? $name : '随机奖品' . $id;
  23. }
  24. # id
  25. public function getId() {
  26. return $this->id;
  27. }
  28. # 权重
  29. public function getWeight() {
  30. return $this->weight;
  31. }
  32. # 设置权重范围区间
  33. public function setRange($start, $end) {
  34. $this->start = $start;
  35. $this->end = $end;
  36. }
  37. # 判断随机数是否在权重范围区间
  38. public function inRange($num) {
  39. return ($num >= $this->start) && ($num <= $this->end);
  40. }
  41. }
  42. /**
  43. * 奖品池
  44. */
  45. class PrizePoll implements IteratorAggregate, Countable {
  46. # 奖品集
  47. protected $items = array();
  48. # 加入奖品
  49. public function addItem(Prize $item) {
  50. $this->items[$item->getId()] = $item;
  51. return $this;
  52. }
  53. # 删除奖品
  54. public function removeItem($itemId) {
  55. if (isset($this->items[$itemId])) {
  56. unset($this->items[$itemId]);
  57. }
  58. return $this;
  59. }
  60. # 更新奖品
  61. public function updateItem(Prize $item) {
  62. if (isset($this->items[$item->getId()])) {
  63. $this->items[$item->getId()] = $item;
  64. }
  65. return $this;
  66. }
  67. # 获取所有奖品
  68. public function getItems() {
  69. return $this->items;
  70. }
  71. # 所有所有可用奖品(如果权重为0,说明这个奖品永远不可能抽到)
  72. public function getVisibleItems() {
  73. $items = array();
  74. foreach ($this->items as $item) {
  75. if ($item->getWeight()) {
  76. $items[$item->getId()] = $item;
  77. }
  78. }
  79. return $items;
  80. }
  81. # Countable::count
  82. public function count() {
  83. return count($this->items);
  84. }
  85. # IteratorAggregate::getIterator()
  86. public function getIterator() {
  87. return new ArrayIterator($this->items);
  88. }
  89. }
  90. /**
  91. * 简单的抽奖类
  92. */
  93. class SimpleTurn {
  94. # 奖池
  95. protected $poll = null;
  96. public function __construct(PrizePoll $poll) {
  97. if ($poll) {
  98. $this->setPoll($poll);
  99. }
  100. }
  101. # 抽奖
  102. public function run(PrizePoll $poll) {
  103. $poll = $poll ? $poll : $this->poll;
  104. if ( ! $poll) {
  105. throw new Exception('奖池未初始化');
  106. }
  107. if ($poll->count() <= 0) {
  108. throw new Exception('奖池为空');
  109. }
  110. $items = $poll->getVisibleItems();
  111. if (count($items) <= 0) {
  112. throw new Exception('奖池为空');
  113. }
  114. $sum = 0;
  115. foreach ($items as $item) {
  116. $start = $sum + 1;
  117. $sum += $item->getWeight();
  118. $end = $sum;
  119. # 设置奖品的权重范围区间
  120. $item->setRange($start, $end);
  121. }
  122. # 随机数
  123. $rand = $this->getRandNum(1, $sum);
  124. # 区间段判断
  125. foreach ($items as $item) {
  126. if ($item->inRange($rand)) {
  127. return $item;
  128. }
  129. }
  130. return null;
  131. }
  132. # 获取随机数
  133. public function getRandNum($min, $max) {
  134. return mt_rand($min ? $min : 1, $max);
  135. }
  136. # 设置奖池
  137. public function setPoll(PrizePoll $poll) {
  138. $this->poll = $poll;
  139. }
  140. }
  141. # 示例
  142. try {
  143. $prizePoll = new PrizePoll();
  144. $prizePoll->addItem(new Prize(1, 5))
  145. ->addItem(new Prize(2, 6))
  146. ->addItem(new Prize(3, 7))
  147. ->addItem(new Prize(4, 2));
  148. $turn = new SimpleTurn($prizePoll);
  149. $prize = $turn->run();
  150. var_dump($prize);
  151. } catch (Exception $e) {
  152. print_r($e);
  153. }

您可能感兴趣的文章:

  • php抽奖小程序的实现代码
  • php实现可以设置中奖概率的抽奖程序代码分享
  • 适用于抽奖程序、随机广告的PHP概率算法实例
  • php解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题
  • PHP转盘抽奖接口实例
  • php编写的抽奖程序中奖概率算法
  • PHP+jQuery翻板抽奖功能实现

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1087267.htmlTechArticlephp实现概率性随机抽奖代码,php概率抽奖代码 1、初始数据: 权重越大,抽取的几率越高 [奖品1, 权重 5], [ 奖品2, 权重6], [ 奖品3, 权...

人气教程排行