当前位置:网站首页>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);
}边栏推荐
- Upload labs for file upload - white box audit
- What if the win11 account is locked and unable to log in? Win11 account is locked and unable to log in
- Change password of MySQL version 5.7 and 8.0
- 零基础入门测试该学什么?最全整理,照着学就对了
- 年薪100万,在北上广深买的起房子吗?
- Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- BSN long story 10: how to ensure the safety of NFT
- Superscalar processor design yaoyongbin Chapter 4 branch prediction -- Excerpt from subsection 4.1
- Is it safe to do fund fixed investment on CICC securities?
- Ubuntu系统安装与配置MySQL
猜你喜欢

TC8:UDP_USER_INTERFACE_01-08

Precautions for lvgl v8.2 string display on keil MDK (take little bear pie as an example)

Live broadcast management project

日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利

I like two men...

leetcode:111. Minimum depth of binary tree

Hololens2 development -6-eyetracking and speech recognition

Apple amplification! It's done so well

HMS core audio editing service 3D audio technology helps create an immersive auditory feast

This is the best flash popular science article I have ever seen!
随机推荐
4hutool实战:DateUtil-格式化时间[通俗易懂]
炒币,亏了1000万。
Thread Basics
MySQL interception_ MySQL method for intercepting strings [easy to understand]
Introduction to expressions and operators in C language
Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture
Cortex M4 systick details
SQL SERVER2014删除数据库失败,报错偏移量0x0000...
Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"
项目必用的全局异常处理器,你学会了吗
PHP code audit and File Inclusion Vulnerability
电脑USB、HDMI、DP各种接口及速度
[unity shader] substitution of bool type in the property definition
Matrix and coordinates
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;
CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
Some tools used in embedded development
Finally, someone made it clear what DRAM and NAND flash are
4hutool practice: dateutil format time [easy to understand]
华为帐号多端协同,打造美好互联生活