当前位置:网站首页>PHP 实现根据概率抽奖
PHP 实现根据概率抽奖
2022-07-06 21:17:00 【指针再爱我一次】
一、思路
大概思路根据可能性向数组中填写多少个值,比如说30%的几率,就向数组中填入30个这个奖品的id
二、实现代码
$data = [
[
'id' => 1,
'name' => '谢谢惠顾',
'probability' => 80 // 抽奖概率
],
[
'id' => 2,
'name' => '10元现金',
'probability' => 18 // 抽奖概率
],
[
'id' => 3,
'name' => '100元现金',
'probability' => 2 // 抽奖概率
],
];
$list = [];
foreach ($data as $key=>$v){
$list = array_merge($list, array_fill(0, $v['probability'], $v['id']));//生成奖品数组
}
shuffle($list);//打乱数组顺序
$key = array_rand($list,1);//获取数组随机下标
// 返回奖品id, 后面可以根据奖品id实现具体其他业务
return $list[$key];
有问题欢迎联系我哦!
边栏推荐
- 二叉搜索树的实现
- First understand the principle of network
- codeforces每日5题(均1700)-第七天
- Sorting operation partition, argpartition, sort, argsort in numpy
- 使用 BR 恢复 GCS 上的备份数据
- Kalman filter-1
- 未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
- 1.19.11.SQL客户端、启动SQL客户端、执行SQL查询、环境配置文件、重启策略、自定义函数(User-defined Functions)、构造函数参数
- 海思万能平台搭建:颜色空间转换YUV2RGB
- About Estimation Statistics
猜你喜欢
Do you choose pandas or SQL for the top 1 of data analysis in your mind?
我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么
QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)
【DPDK】dpdk样例源码解析之三:dpdk-l3fwd_001
ubuntu20安裝redisjson記錄
运算放大器应用汇总1
25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)
Code quality management
U.S. Air Force Research Laboratory, "exploring the vulnerability and robustness of deep learning systems", the latest 85 page technical report in 2022
【mysql】mysql中行排序
随机推荐
It's too convenient. You can complete the code release and approval by nailing it!
Hisilicon 3559 universal platform construction: RTSP real-time playback support
海思3559万能平台搭建:RTSP实时播放的支持
【开发软件】 tilipa开发者软件
Enter the rough outline of the URL question (continuously updated)
Baidu map JS development, open a blank, bmapgl is not defined, err_ FILE_ NOT_ FOUND
Force buckle ----- path sum III
预处理——插值
Top 50 hit industry in the first half of 2022
大白话高并发(二)
QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)
How to manage the expiration of enterprise distribution certificates- How to manage Enterprise Distribution certificate expiration?
PIP download only, not install
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
ubuntu20安裝redisjson記錄
Codeworks 5 questions per day (1700 average) - day 7
OSCP工具之一: dirsearch用法大全
使用 Dumpling 备份 TiDB 集群数据到 GCS
Restcloud ETL Community Edition June featured Q & A
Native MySQL