search
Homephp教程php手册基于PHP实现简单的随机抽奖小程序,php抽奖小程序

基于PHP实现简单的随机抽奖小程序,php抽奖小程序

一个抽奖小程序,概论可控,也可某个奖品在前端显示,而程序中根本不可能获得!把所有的概率x10后相加起来,新数组中每项的值等于它前几个的和加上它本身。

然后随即生成一个0到最大数之间的一个数,通过循环查看它属于那个区间,返回该区间的key。

代码如下

$prize = array(
1 => 3.5, //3.5%机率
2 => 2.5, 
3 => 6, 
4 => 2, 
5 => 42, //42%机率
6 => 36, 
7 => 2, 
8 => 4, 
9 => 2
);
//最后确认相加等于100
$prizeList = array(
1 => array('小仙子'), 
2 => array('5Q币'), 
3 => array('38888游戏币'), 
4 => array('88888游戏币'), 
5 => array('谢谢参与!'), 
6 => array('欢迎下次再来!'), 
7 => array('10Q币'), 
8 => array('888银元宝(绑定)'), 
9 => array('随机高级坐骑(绑定)')
);
//unset($prize[7]); 直接把10Q这个奖品去掉
$times = 10;
$max = 0;
foreach ($prize as $k => $v)
{
$max = $v * $times + $max;
$row['v'] = $max;
$row['k'] = $k;
$prizeZone[] = $row;
}
$max--; //临界值
$rand = mt_rand(0, $max);
$zone = 1;
foreach ($prizeZone as $k => $v)
{
if ($rand >= $v['v'])
{
if ($rand >= $prizeZone[$k + 1]['v'])
{
continue;
}
else
{
$zone = $prizeZone[$k + 1]['k'];
break;
}
}
$zone = $v['k'];
break;
}
print_r($prizeList[$zone][0]);

基于php实现简单的抽奖小程序代码就给大家写这么多,不过都是核心代码,希望对大家学习工作有所帮助。

您可能感兴趣的文章:

  • php抽奖小程序的实现代码
  • php实现可以设置中奖概率的抽奖程序代码分享
  • 适用于抽奖程序、随机广告的PHP概率算法实例
  • php解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题
  • PHP转盘抽奖接口实例
  • php编写的抽奖程序中奖概率算法
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor