当前位置:网站首页>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];
有问题欢迎联系我哦!
边栏推荐
- QT 使用QToolTip 鼠标放上去显示文字时会把按钮的图片也显示了、修改提示文字样式
- 【开发软件】 tilipa开发者软件
- 20. (ArcGIS API for JS) ArcGIS API for JS surface collection (sketchviewmodel)
- 海思万能平台搭建:颜色空间转换YUV2RGB
- 2022年上半年HIT行业TOP50
- 【DPDK】dpdk样例源码解析之三:dpdk-l3fwd_001
- 使用 TiDB Lightning 恢复 GCS 上的备份数据
- 未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
- Introduction to opensea platform developed by NFT trading platform (I)
- Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
猜你喜欢

太方便了,钉钉上就可完成代码发布审批啦!

Clock in during winter vacation

Hisilicon 3559 universal platform construction: RTSP real-time playback support

【安全攻防】序列化与反序列,你了解多少?
接口数据安全保证的10种方式
![[MySQL] row sorting in MySQL](/img/97/8a451fa62796838e11242c86eecd8d.png)
[MySQL] row sorting in MySQL

Ubuntu20 installation redisjson record

QT item table new column name setting requirement exercise (find the number and maximum value of the array disappear)

2022年上半年HIT行业TOP50

A 股指数成分数据 API 数据接口
随机推荐
Can the applet run in its own app and realize live broadcast and connection?
qt-线程等01概念
Code quality management
史上最全学习率调整策略lr_scheduler
The true face of function pointer in single chip microcomputer and the operation of callback function
PIP download only, not install
本机mysql
Calculation of time and space complexity (notes of runners)
QT 项目 表格新建列名称设置 需求练习(找数组消失的数字、最大值)
About Tolerance Intervals
未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
三重半圆环进度条,直接拿去就能用
二进制、八进制、十六进制
Adaptive non European advertising retrieval system amcad
List interview common questions
使用 BR 备份 TiDB 集群到 GCS
自适应非欧表征广告检索系统AMCAD
哈夫曼树基本概念
ubuntu20安裝redisjson記錄
The most complete learning rate adjustment strategy in history LR_ scheduler