Based on PHP to implement fake goods timed to buy busy effect _php tips

Source: Internet
Author: User
Tags php code rand touch

Recently to do a project that shows snapping features about the merchandise. For example, our site is very with traffic, then tens of thousands of users in a few seconds at the same time point your merchandise, there will be "too many people, will be prompted, the system busy."

However, most websites do not have such a fork. In order to allow users to feel the product is very popular, the prompt "system busy" effect, we need to do a program to "pretend to be busy." (in addition to Taobao, we do not think that other sites are really busy oh, but people are deliberately let you think not to buy, to understand)

This article to set a rule, we can expand according to my ideas.

1, the purchase of merchandise links, everyone can point.

2, we want to let users have the possibility of 70% "in line, the goods busy"

This article is implemented with PHP code. Other languages, change.

First, we use the knowledge learned in elementary school to think about:

1, if there are 10 balls, of which 3 red ball, 7 basketball. Put it in the bag. Casually mix, let you put your hand in touch, then touch the odds of basketball? Apparently, it's 70%.

Before I put this demand to a small partner to see. The answer he gave was as follows:

$arr =array ("Red", "Red", "Red", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue");

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

Then tell me that he's done with two words.

This approach is actually quite clever. But this little partner misses a very important point.

2. What if the second person touches it? Here is a note, if the second person to touch, then must fill the 10 balls (still 3 red balls, 7 basketball)

And the most important thing is to continue the "random, Casual" mix. In this way, the second person to touch the chances of the basketball will still be 70%.

The above program obviously ignores: continue to "random, casual" mix. If everyone touches the ball with the top three red seven blue. Then the RAND function of PHP does not guarantee that basketball is 70%.

Speaking of this, many great gods want to come up with various advanced algorithms, such as what Bayesian, matrix and other words come out. I'm sure your boss won't allow you to spend so much time on this function if it's such a complicated operation.

Next, I released a simple, but also accurate algorithm. Our goal: To use PHP's simple function, as much as possible to touch the probability of basketball close to 70%.

The first step: $arr =array ("Red", "Red", "Red", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue"); This thing to have, this is the initialization of three red balls, 7 basketball

The second step: Random, Random mix.

The above array has 10 elements, we can take a random two ball exchange, exchange how many times we can set ourselves

First write an Exchange function (if this function can not understand, it is necessary to fill the basic knowledge)

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

This function implementation, I entered two random number, to achieve the array in accordance with the ordinal of the exchange.

The third step: Optimize the exchange algorithm.

Because of the switching function above, the input of random parameters leads to the exchange of red ball and red ball, or basketball and basketball. Then, however, there is no "real" mix.

So we're going to write a supplemental function to make sure that every exchange must be a red ball and a basketball for free exchange.

 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 is to find the red ball or basketball in 10 balls, and then take out the serial number that they are in, and then use the RAND function to randomly pick a basketball or red ball serial number.

Norbit, look at this:

$i =getrange ($arr, "Red"); This will take out the serial number of a random red ball.
$j =getrange ($arr, "Blue"); This will take out the serial number of a random basketball.

Fourth step: More important.

Start a random, indiscriminate mix.

 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/> "; This statement can look at the output, the mix after the arrangement, whether each time is different
     }

The point to note here is that $num <10. On behalf of me mixed 10 times. It's like using your big hand to stir in the bag 10 times. The more theoretically stirred, the more random the randomness. Here in fact 10 times enough.

The fourth step after the completion of the $arr is the mixing of good red ball and basketball mix.

Step Fifth: Call the RAND function again

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

If the content is blue, direct exit ("Lao Tze is busy, don't bother")
If it is red, then let the program continue to execute the purchase program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.