Home > Backend Development > PHP Tutorial > Realize the effect of pretending to be busy with limited-time sales of goods based on PHP, limited-time sales of goods_PHP tutorial

Realize the effect of pretending to be busy with limited-time sales of goods based on PHP, limited-time sales of goods_PHP tutorial

WBOY
Release: 2016-07-12 09:07:13
Original
988 people have browsed it

Based on PHP, we can realize the effect of pretending that products are busy during limited-time sales. Products are on limited-time sales.

I recently have to do a project about the function of displaying products on sale. For example, if our website has a lot of traffic, and thousands of users click on your product at the same time within a few seconds, it will indeed appear that "there are too many people rushing to buy, and the system is busy."

However, most websites are not so awesome. In order to make users feel that the product is very popular, and the effect of "system busy" is always displayed, we need to make a program to "pretend to be busy". (Except for Taobao, please don’t think that other websites are really busy. It’s just that they deliberately make you feel that you can’t buy it without grabbing it. Please understand)

This article sets a rule, and you can expand it according to my ideas.

1. Everyone can click on the product purchase link.

2. We want to give users a 70% chance of experiencing “queuing, busy products”

This article is implemented using php code. Same as for other languages, change it.

First of all, let’s think about it using the knowledge we learned in primary school:

1. If there are 10 balls, 3 are red balls and 7 are basketballs. Put it in the bag. Mix it randomly and let you put your hand in and touch it. What is the probability of touching a basketball? Apparently, it’s 70%

I showed this requirement to a friend before. The answer he gave is as follows:

$arr=array("red","red","red","blue","blue","blue","blue","blue","blue","blue");

Then echo $arr[rand(0,9)];

Then he told me that he was done in just two sentences.

This approach is actually quite smart. But this little friend ignored a very important point

2. What if the second person touches it? One thing to note here is that if the second person comes to touch, these 10 balls must be filled up (still 3 red balls and 7 basketballs)

And the most important thing is to continue to mix it "randomly and casually". In this way, the probability of the second person touching the basketball will still be 70%.

The above procedure obviously ignores: continue to mix "randomly and casually". If everyone presses the first three red and the last seven blue to touch the ball. Then PHP's rand function cannot guarantee that the basketball is 70%.

Speaking of this, many masters want to come up with various advanced algorithms, such as Bayesian, matrix and other words. If such an e-commerce function requires such complex calculations, I believe your boss will not agree with you spending so much time to complete this function.

Next, I release a simple but accurate algorithm. Our goal is to use simple functions in PHP to make the probability of touching a basketball as close to 70% as possible.

Step 1: $arr=array("red","red","red","blue","blue","blue","blue","blue", "blue","blue"); This thing must be present, these are the initialized three red balls and 7 basketballs

Step 2: Mix randomly and randomly.

The above array has 10 elements. We can exchange two random balls. You can decide how many times to exchange them.

First write a swap function (if you don’t understand this function, you need to learn the basics)

function swap($i,$j,$arr)
  {
    $tmp=$arr[$i];
    $arr[$i]=$arr[$j];
    $arr[$j]=$tmp;
    return $arr;
  }
Copy after login

This function is implemented by inputting two random numbers and exchanging the sequence numbers in the array.

Step 3: Optimize the exchange algorithm.

Because of the above exchange function and the input random parameters, red balls are exchanged with red balls, or basketballs are exchanged with basketballs. However, "real" mixing is not achieved

So we need to write a supplementary function to ensure that every exchange must be a random exchange of red balls and basketballs

 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)];
  }
Copy after login

The function of this function is to find the red ball or basketball among the 10 balls, then take out their current serial numbers respectively, and then use the rand function to randomly pick the serial number of a basketball or red ball.

Please take a look here:

$i=getRange($arr,”red”); //This way you can get the serial number of a random red ball
$j=getRange($arr,”blue”); //This can take out the serial number of a random basketball

Step 4: More important.

Start mixing randomly and randomly

 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/>”; //这个语句可以看一下输出,混合过后的排列,是否每次都不一样
     }
Copy after login

The important thing to note here is that $num<10. Mix 10 times on my behalf. It's equivalent to using your big hands to stir the bag 10 times. Theoretically, the more you stir, the stronger the randomness. Here 10 times is actually enough.

The $arr that comes out after the fourth step is completed is a mixture of red balls and basketballs.

Step 5: Call the rand function again

echo $arr[rand(0,9)];

If the content is blue, exit directly ("I'm very busy, don't bother")
If it is red, then let the program continue to execute the purchase process.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1062029.htmlTechArticleBased on PHP, the effect of pretending to be busy with limited-time sales of products has been implemented. I have recently done a project for limited-time sales of products, and the relevant products are displayed as rush sales. function. For example, our website has a lot of traffic, so...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template