场景四:简单计算权重的方法示例【适合抽奖类应用】

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在某个奖品的权重范围值内,标识这个奖品被抽取到

/**
 * 奖品
 */
class Prize {
    # ID
    public $id = null;
    # 权重
    public $weight = null;
    # 奖品名
    public $name = null;

    # 权重范围区间起始值
    protected $start = 0;
    # 权重范围区间结束值
    protected $end = 0;

    public function __construct($id = null, $weight = null, $name = null) {
        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, 70))
        ->addItem(new Prize(4, 2));

    // $prizePoll->updateItem(new Prize(3, 80)); //更新奖池指定权重

    $turn = new SimpleTurn($prizePoll);
    // 抽中奖池
    $jackpot_data['prize'] = $turn->run($prizePoll);
    // 删除抽中奖池
    $prizePoll->removeItem($jackpot_data['prize']->id);
    // 剩余未抽到奖池
    $jackpot_data['Items'] = $prizePoll->getItems();

    // shuffle($jackpot_data['Items']); //打乱数组顺序
    echo "<pre>";
    print_r($jackpot_data);
} catch (Exception $e) {
    print_r($e);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值