Home  >  Article  >  Backend Development  >  PHP randomly returns busy effect

PHP randomly returns busy effect

*文
*文Original
2017-12-27 15:40:221275browse

This article mainly introduces the effect of pretending to be busy with limited-time sales of goods based on PHP. I hope to be helpful.

Recently I am working on a project, which is about the function of displaying rush to buy products. For example, if our website has a lot of traffic, if 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."

But, it is a big problem However, some websites are not so awesome. In order to make users feel that the products are very popular, we need to make a program to "pretend to be busy" (except Taobao, don't think that other websites are really busy). It's very busy, but they deliberately make you feel that you can't buy it without grabbing it, please understand)

This article will set 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 "queuing, product is busy"

This article Use php code to implement it. The same as in other languages.

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

1. If there are 10 balls, 3 of them are red. A basketball. Put it in a bag. Mix it randomly and let you put your hand in it. What is the probability of touching the basketball? Obviously, I gave this request to someone before. Look, my friend. The answer he gave is as follows:

     $arr=array(“red”,”red”,”red”,”blue”,”blue”,”blue”,”blue”,”blue”,”blue”,”blue”);
     echo $arr[rand(0,9)];

Then he told me that he did it in two sentences. But this friend ignored it. A very important point

2. What if the second person comes to touch? There is a point to note here. If the second person comes to touch, then the 10 balls must be filled up (still 3 red balls). Balls, 7 basketballs)

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

The above program obviously ignores: continue to mix "randomly and casually". If everyone touches the ball according to the first three red and the last seven blue, then the rand function of PHP cannot guarantee that the basketball is 70. %.

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

Next, I will release a simple, yet accurate algorithm. Our goal is: simplicity using PHP. Function, try to make the probability of touching the 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 there, these are the three red balls and 7 basketballs that are initialized

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 supplement the basic knowledge)


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


To implement this function, I enter two Any serial number can be used to exchange the sequence numbers in this array.


Step 3: Optimize the exchange algorithm.

Because of the above exchange function and the input random parameters, the red ball is exchanged with the red ball, or the basketball is exchanged with the basketball. Then, however, "real" mixing is not achieved

So we have 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)];
  }


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

Please take a look here:

    $i=getRange($arr,”red”);  //这样可以取出随机一个红球的序号
   $j=getRange($arr,”blue”); //这样可以取出随机一个篮球的序号

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

## 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 the mixture of red ball and basketball.


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.

related suggestion:


php implements event candidate lottery function code

php implements simple permission management

php Lottery Mini Program

The above is the detailed content of PHP randomly returns busy effect. For more information, please follow other related articles on the PHP Chinese website!

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