📚
note
Blog
  • Initial page
  • JS-notes
    • 使用浏览器书签执行js代码
    • JSON.stringify的小技巧
    • Fisher–Yates shuffle洗牌算法
    • 打印页面
  • Web-notes
    • 在网页中引入在线字体
  • Uniapp-notes
    • swiper-item高度自适应
    • 微信小程序的图片预览兼容性处理
  • VUE-notes
    • vue-note
    • vue3-note
  • VPS-notes
    • CentOs7笔记
    • ssh小记
    • Ubuntu笔记
    • vps安全相关
    • [Google Drive笔记](VPS-notes/Google Drive笔记.md)
  • TypeScript-notes
    • ts热编译项目
    • TypeScript笔记
    • js项目转ts
  • Python-notes
    • Python爬虫笔记
    • Python笔记
  • PHP-notes
    • php笔记
    • php+redis
    • php-codeIgniter
    • php抽奖算法
    • Laravel笔记
  • Mobile-notes
    • 移动端常用样式及兼容相关
  • Linux-notes
    • linux常用指令
  • Game-notes
    • Minecraft-server
  • TelegramBot-notes
    • tg-bot小记
  • Windows-notes
    • window-note
    • node-note
    • WSL-note
  • RaspberryPi-notes
    • RaspberryPi-note
    • 其他玩法
    • Openwrt
    • Ubuntu安装指南
  • Phone-notes
    • ZenFone6-note
  • Cocos-notes
    • Cocos-note
  • Network-notes
    • 单线复用
  • Other-notes
    • 国际化地域标识码
Powered by GitBook
On this page
  • 奖品设置
  • 根据概率随机抽奖项

Was this helpful?

  1. PHP-notes

php抽奖算法

奖品设置

$award = [{id: "1", award: "幸运值+5", probability: "48", quantity: "0"},
        {id: "2", award: "幸运值+1", probability: "50", quantity: "0"},
        {id: "3", award: "iPad pro", probability: "0", quantity: "0"},
        {id: "4", award: "联想笔记本电脑", probability: "0", quantity: "0"}]

根据概率随机抽奖项

/**
 * 抽奖逻辑
 */
private function randPrize($award)
{
    $result = array();
    foreach ($award as $key => $val) {
        // 1.每个抽奖项的获取概率
        $arr[$key] = $val['probability'];
    }
    // 2.计算总概率
    $proSum = array_sum($arr);
    // 3.重排概率
    asort($arr);
    // 4.概率数组循环
    foreach ($arr as $k => $v) {
        $randNum = mt_rand(1, $proSum);
        if ($randNum <= $v) {
            $result = $award[$k];
            break;
        } else {
            $proSum -= $v;
        }
    }
    return $result['id'];
}

Last updated 2 years ago

Was this helpful?