当前位置:网站首页>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);
}边栏推荐
- 客户端如何请求数据库?
- TC8:UDP_USER_INTERFACE_01-08
- C [byte array] and [hexadecimal string] mutual conversion - codeplus series
- Computer USB, HDMI, DP various interfaces and speeds
- Drive away bad emotions and stop worrying
- Live broadcast management project
- 新数据库时代,不要只学 Oracle、MySQL
- tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
- Eat a rich woman's melon...
- Which securities company has a low, safe and reliable Commission for stock trading and account opening
猜你喜欢

The stock position building rate of global funds and asset management reached a new low in 15 years

Japanese professor sues Intel FPGA and SOC products for infringing a design patent

Introduction to expressions and operators in C language

Apple amplification! It's done so well

TC8:UDP_ USER_ INTERFACE_ 01-08

HMS Core音频编辑服务3D音频技术,助力打造沉浸式听觉盛宴

谁还在买“三只松鼠”们

Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022

uniapp微信小程序组件按需引入

好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!
随机推荐
硬件中台项目
The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
SQL statement modify field type "suggestions collection"
Network partition notes
Prefabricated dishes usher in the "golden age", who can lead the next trillion market
Introduction to expressions and operators in C language
哪个券商公司炒股开户佣金低又安全又可靠
C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列
C one line code calculates the MD5 value of the file - codeplus series
[unity rendering] customized screen post-processing
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul
Sd-wan notes
STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
Project procurement management
SQL SERVER2014删除数据库失败,报错偏移量0x0000...
7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
遇到女司机业余开滴滴,日入500!
我喜欢两个男人。。。