PHP random number WeChat randomly generated red envelope amount algorithm PHP version

Source: Internet
Author: User
Recently in the study of the function of hair red envelopes, so wrote a red envelope generation algorithm.

Demand of red envelope generation algorithm
Generate all the red envelopes in advance or a request randomly generate a red envelope
Simply put, a large integer m decomposition (directly divided into units, such as 1 or 100) decomposition into n small integers, the range of small integers is [min, Max].
The simplest idea, the first guarantee, each small red envelope guaranteed to have min, and then each request randomly generated a 0 to (max-min) range of integers, plus min is the amount of red envelopes.
This algorithm is simple, but there is a disadvantage: the final generation of red envelopes may be the min money. That means maybe the last red envelopes are 0.01 yuan.
Another way is to pre-generate all the red envelopes, so it's easier to control. I chose to generate all the red envelopes in advance.

An ideal algorithm for generating red envelopes
The ideal red envelope generation result is the average number of red envelopes near the mean, large red envelopes and small red envelopes less.
As you can imagine, the distribution of the number of red envelopes is somewhat like a normal distribution.

So how to achieve this average near the value of more than the requirements of the line?
is to find an algorithm that can increase the probability near the mean. Then use a "swell" and then "shrink" the way to achieve this effect.
First square, then generate the square in the range of random numbers, then the root, then the probability is no longer the average.
Specific algorithm: (set total amount of money, total number, maximum value, minimum value to reasonable)
PHP code

/** * The square of a number * @param $n */function Sqr ($n) {return $n * $n;} /** * Produces a random number between Min and Max, but the probability is not average and the probability of direction from min to max increases gradually. * First square, then produce a square value within the range of random numbers, and then the root, so that a "swelling" and then "contraction" effect.   */function Xrandom ($bonus _min, $bonus _max) {$sqr = Intval (Sqr ($bonus _max-$bonus _min));   $rand _num = rand (0, ($SQR-1)); Return Intval (sqrt ($rand _num)); }/** * * @param $bonus _total red Envelope total * @param $bonus _count Red envelope number * @param $bonus _max The maximum amount of each small red envelope * @param $bonus _min per Small red Envelopes The minimum * @return a one-dimensional array that stores the values of each small red envelope generated ($bonus _total, $bonus _count, $bonus _max, $bonus _min) {$res Getbonus     Ult = Array ();     $average = $bonus _total/$bonus _count;    $a = $average-$bonus _min;     $b = $bonus _max-$bonus _min;    The probability of such a random number actually changes, and the probability of generating a large number is less than that of generating a decimal. This achieves the value of most red envelopes near the average.    Big red Envelopes and small red envelopes are relatively few.    $range 1 = sqr ($average-$bonus _min);     $range 2 = sqr ($bonus _max-$average);      for ($i = 0; $i < $bonus _count; $i + +) {//Because the number of small red envelopes is usually more than the number of big red envelopes, because the probabilities here are to be replaced. When the random number > average, a small red envelope is generated//when the random number
 <平均值,则产生大红包 if (rand($bonus_min, $bonus_max)>
  
 $average) {//minus money on average $temp = $bonus _min + xrandom ($bonus _min, $average);        $result [$i] = $temp;      $bonus _total-= $temp;        } else {//Add money on the average $temp = $bonus _max-xrandom ($average, $bonus _max);        $result [$i] = $temp;      $bonus _total-= $temp;    }}//If you still have money, try adding it to a small red envelope, and if not, try the next one. while ($bonus _total > 0) {for ($i = 0; $i < $bonus _count; $i + +) {if ($bonus _total > 0 && $r          esult[$i] < $bonus _max) {$result [$i]++;        $bonus _total--;  }}}//If the money is negative, you have to extract it from the generated small red envelopes ($bonus _total < 0) {for ($i = 0; $i < $bonus _count; $i + +)          {if ($bonus _total < 0 && $result [$i] > $bonus _min) {$result [$i]--;        $bonus _total++;  }}} return $result; } $bonus _total = 200; $bonus _count = 100; $bonus _max = 10;//This algorithm requires that the maximum value set is greater than the average $bonus _min = 1; $result _bonus = Getbonus ($bonus _total, $bonus _count, $bonus _Max, $bonus _min); $total _money = 0; $arr = Array ();   foreach ($result _bonus as $key = + $value) {$total _money + = $value;   if (Isset ($arr [$value])) {$arr [$value] + = 1;   }else{$arr [$value] = 1; }}//Output total money to see if it is the same as the total number of settings echo $total _money; Output all random red envelope values Var_dump ($result _bonus); Statistics of the number of red envelopes per money, check whether close to normal distribution Ksort ($arr); Var_dump ($arr);
 

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support this site.

The above introduces the PHP random number randomly generated red envelope amount algorithm PHP version, including the PHP random number of content, I hope the PHP tutorial interested in a friend helpful.

  • 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.