当前位置:网站首页>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];
有问题欢迎联系我哦!
边栏推荐
- Redis configuration and optimization of NoSQL
- PHP lightweight Movie Video Search Player source code
- ggplot 分面的细节调整汇总
- Antd Comment 递归循环评论
- 使用 TiDB Lightning 恢复 GCS 上的备份数据
- Flutter3.0, the applet is not only run across mobile applications
- 本机mysql
- About Estimation Statistics
- Enumeration general interface & enumeration usage specification
- What is Ba? How about Ba? What is the relationship between Ba and Bi?
猜你喜欢

ubuntu20安裝redisjson記錄

Kotlin Android environment construction

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

一些常用软件相关

VHDL implementation of arbitrary size matrix multiplication

Confirm the future development route! Digital economy, digital transformation, data This meeting is very important

数据的存储

代码质量管理
Docker部署Mysql8的实现步骤

qt-线程等01概念
随机推荐
Machine learning notes - bird species classification using machine learning
Clock in during winter vacation
浅谈网络安全之文件上传
One of oscp tools: dirsearch usage Encyclopedia
Native MySQL
Restcloud ETL Community Edition June featured Q & A
PHP lightweight Movie Video Search Player source code
19. (ArcGIS API for JS) ArcGIS API for JS line acquisition (sketchviewmodel)
[leetcode] 700 and 701 (search and insert of binary search tree)
Codeworks 5 questions per day (1700 average) - day 7
运算放大器应用汇总1
Open3d mesh filtering
[hcie TAC] question 3
NoSQL之Redis配置与优化
Que savez - vous de la sérialisation et de l'anti - séquence?
23. (ArcGIS API for JS) ArcGIS API for JS ellipse collection (sketchviewmodel)
【mysql】mysql中行排序
Open3D 网格滤波
What is Ba? How about Ba? What is the relationship between Ba and Bi?
[safe office and productivity application] Shanghai daoning provides you with onlyoffice download, trial and tutorial