当前位置:网站首页>php 实现抽奖功能
php 实现抽奖功能
2022-07-01 10:07:00 【陈卿诺语】
记录借载
核心算法
<?php
namespace app\Server;
class LuckDraw
{
public static function get_rand($arr){
$result = '';
$proSum = array_sum($arr);
foreach ($arr as $key => $proCur){ //1 5 10 12 22 50
$randNum = mt_rand(1,$proSum);
if ($randNum <= $proCur){
$result = $key;
break;
}else{
$proSum -= $proCur;
}
}
unset($arr);
return $result;
}
}控制器调用
public function luckDraw(){
$prize = array(
array('id' => 1,'text' => '2022 ipad Pro','v' => 1),
array('id' => 2,'text' => 'iphone 12','v' => 5),
array('id' => 3,'text' => '华为p50','v' => 10),
array('id' => 4,'text' => '一加手机','v' => 12),
array('id' => 5,'text' => '小米14','v' => 22),
array('id' => 6,'text' => '很遗憾未中奖','v' => 50),
);
$prize = array_combine(array_column($prize,'id'),$prize);
foreach ($prize as $key => $val){
$arr[$val['id']] = $val['v'];
}
$rid = LuckDraw::get_rand($arr);
$res['yes'] = $prize[$rid]['text'];
unset($prize[$rid]);
shuffle($prize);
for ($i = 0; $i < count($prize);$i++){
$pr[] = $prize[$i]['text'];
}
$res['no'] = $pr;
dd($res);
}边栏推荐
- 12.Gateway新一代网关
- 年薪100万,在北上广深买的起房子吗?
- C one line code calculates the MD5 value of the file - codeplus series
- “中移链”国密引擎在BSN正式上线
- 4hutool practice: dateutil format time [easy to understand]
- Packetdrill script analysis guide
- Huawei accounts work together at multiple ends to create a better internet life
- Who has the vision to cross the cycle?
- 睡了二哥。。。
- Postgraduate entrance examination vocabulary 2023 sharing (1)
猜你喜欢

The programmer was beaten.

这样理解mmap,挺有意思!

年薪100万,在北上广深买的起房子吗?

Introduction to expressions and operators in C language

历史上的今天:九十年代末的半导体大战;冯·诺依曼发表第一份草案;CBS 收购 CNET...

Live broadcast management project

The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious

云原生到底是什么?它会是未来发展的趋势吗?

嵌入式开发用到的一些工具

Flinkv1.13实现金融反诈骗案例
随机推荐
这样理解mmap,挺有意思!
电脑USB、HDMI、DP各种接口及速度
【Laravel 】faker数据填充详解
Drive away bad emotions and stop worrying
Network partition notes
This is the best flash popular science article I have ever seen!
PHP code audit and File Inclusion Vulnerability
C [byte array] and [hexadecimal string] mutual conversion - codeplus series
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
Write your own who commands
Ssh server rejects password, try again; Permitrootlogin yes invalid problem
Postgraduate entrance examination vocabulary 2023 sharing (1)
架构实战营 毕业总结
sql语句修改字段类型「建议收藏」
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
我喜欢两个男人。。。
Prefabricated dishes usher in the "golden age", who can lead the next trillion market
High precision factorial
4hutool实战:DateUtil-格式化时间[通俗易懂]
C# 一行代码计算文件的MD5值 - CodePlus系列