基于PHP实现假装商品限时抢购繁忙的效果,商品限时抢购
基于PHP实现假装商品限时抢购繁忙的效果,商品限时抢购
最近要做一个项目,有关商品显示抢购的功能。比如我们的网站很带流量,那么成千上万的用户在几秒内同时点你的商品,确实会出现“抢购人数过多,会提示,系统繁忙。
但是呢,大部分网站然而并没有这么牛叉。为了让用户感受到商品很抢手,动不动就提示”系统繁忙“的效果,我们需要做一个程序来”假装很繁忙“。 (除了淘宝,大家不要以为其他网站真的很繁忙哦,只不过人家是故意让你觉得不抢就买不到,求懂)
本文来设定一个规则,大家可以根据我的思路扩展即可。
1、商品购买链接,每个人都可以点。
2、我们要让用户有70%的可能性出现“排队中,商品繁忙”
本文用 php代码实现。其他语言一样,改改。
首先我们用小学学到的知识想一下:
1、 如果有10个球,其中3个红球,7个篮球。放在袋子里。随便胡乱的混合一下,让你用手伸进去摸,那么摸到篮球的几率是多少?显然,是70%
之前我把这个需求给了一个小伙伴看。他给出的答案如下:
$arr=array(“red”,”red”,”red”,”blue”,”blue”,”blue”,”blue”,”blue”,”blue”,”blue”);
然后 echo $arr[rand(0,9)];
然后告诉我,他两句话就搞定了。
这个做法其实已经蛮聪明了。但是这位小伙伴忽略了很重要的一点
2、如果第二个人来摸呢? 这里有个注意点,如果第二个人来摸,那么必定要把这10个球补满(依然是3个红球,7个篮球)
然后最重要的,还要继续“胡乱的、随便的”混合一下。这样,第二个人来摸到篮球的几率才会依然是70%.
上面的程序明显忽略了:继续“胡乱的、随便的”混合一下。 如果每个人都按这个 前三个红后七个蓝 来摸球。那么php的rand函数不能保证篮球是70%。
说到这,很多大神要拿出各种高级算法,譬如啥贝叶斯、矩阵之类的字眼出来。如果这么一个电商功能要用这么复杂的运算,我相信你的老板不会同意你花这么多时间来完成这个功能吧。
接下来,我放出一种简单,但也不失精准性的算法。我们的目标是:使用php的简单函数,尽可能的让摸到篮球的几率接近于70%。
第一步: $arr=array(“red”,”red”,”red”,”blue”,”blue”,”blue”,”blue”,”blue”,”blue”,”blue”); 这个东西要有,这就是初始化的三个红球,7个篮球
第二步:随意的、胡乱的混合。
上面一个数组有10个元素,我们可以采取随机两个球交换,交换多少次可以自己定
先写个交换函数 (如果这个函数看不懂,就要恶补基础知识啦)
function swap($i,$j,$arr) { $tmp=$arr[$i]; $arr[$i]=$arr[$j]; $arr[$j]=$tmp; return $arr; }
这个函数实现,我输入两个随便什么序号,实现对这个数组中符合该序号的求交换一下。
第三步:优化交换算法。
因为上面的交换函数,输入的随机参数导致,红球和红球交换,或者篮球和篮球交换。那么然而并没有实现“真正的”混合
所以我们要写个补充函数,确保每次交换,都必须是红球和篮球进行随意交换
function getRange($arr,$v) { $ret=array(); for($i=0;$i<count($arr);$i++) { if($arr[$i]==$v) { $ret[]=$i; } } return $ret[rand(0,count($ret)-1)]; }
这个函数的作用是:在10个球中找到 红球或篮球,然后分别取出他们目前所在的序号,然后利用rand函数随机取一个篮球或者红球的序号。
诺看一下这里:
$i=getRange($arr,”red”); //这样可以取出随机一个红球的序号
$j=getRange($arr,”blue”); //这样可以取出随机一个篮球的序号
第四步:比较重要。
开始随意的、胡乱的混合
for($num=0;$num<10;$num++) { $i=getRange($arr,”red”); $j=getRange($arr,”blue”); $arr=swap($i,$j,$arr); // echo implode(“,”, $arr).”|”.$i.”|”.$j.”<br/>”; //这个语句可以看一下输出,混合过后的排列,是否每次都不一样 }
这里的注意点是,$num
第四步执行完成后出来的$arr 就是搅拌好的 红球和篮球的混合体。
第五步:再次调用 rand函数
echo $arr[rand(0,9)];
如果出来的是内容是blue ,则直接exit(“老子很忙,别烦”)
如果是red,那么让程序继续执行购买程序吧。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
