PHP using parabolic model to implement the program source code of WeChat red envelope generation algorithm

Source: Internet
Author: User

Today, we study the generation algorithm of red envelopes, seemingly feel simple, actually get up not so much, of course, if you just feel that with a rand to generate a few random numbers, it will not make any sense.

Send red envelopes should be a bit fastidious ah, let the distribution of all can get the non-0 red envelopes, at the same time to let most people get the average of the red envelopes.

No other said, I have written a lot of comments in the program, the code is as follows:

<?php/* *author:kermit *time:2015-8-26 *note: Red envelopes Generate random Algorithm */header ("Content-type:text/html;charset=utf-8");d Ate_              Default_timezone_set (' PRC '), #红包生成的算法程序class reward{public $rewardMoney, #红包金额, Unit $rewardNum public; #红包数量 public $scatter, #分散度值1 -10000 public $rewardArray, #红包结果集 #初始化红包类 public Function __construct () {$this Rewardarray=array (); } #执行红包生成算法 Public Function Splitreward ($rewardMoney, $rewardNum, $scatter =100) {#传入红包金额和数量 $this->rewardmoney=$ Rewardmoney, $this->rewardnum= $rewardNum, $this->scatter= $scatter; $this->realscatter= $this->scatter/ 100;/* * Preface: Today I suddenly think that, for example, to give 1 red envelopes to the n person, is actually equivalent to get n percent data * condition is the sum of the N percent of =100/100. The average of these N percentages is 1/n. * and this n percent data conforms to a normal distribution (most values are closer to the average) * viewpoint: A lot of 0.01 of red envelopes in the red envelopes, I think this is the process of artificial control, the purpose is to prevent the total red envelope number over the total, first divided into a few 0.01 red envelopes. * Otherwise it is very difficult to have a lot of 0.01 yuan red envelopes, either in random probability or normal distribution. */#我的思路: As said above, for example: a red envelope to 5 people, I want to draw 5 decimals, their and is 1, their average is 1/5# calculate the average probability of the red envelopes, accurate to decimal 4 bits. That is, the 1/n value above. $avgRand =round (1/$this->rewardnum,4); #红包的向平均数集中的分布正像数学上的抛物线. The larger the parabolic y=ax2,|a|, the smaller the opening of the parabolic line, the smaller the |a|.The greater the opening of the parabolic line, the a>0 when the opening, we are positive, a>0 to consider it. #程序里的 $scatter value is the top of a, this value divided by 100, as 100 as the benchmark, #通过开方 (parabolic model in mathematics, radical can narrow the value of the change) to obtain a small number (small number is more small red packets) of the random distribution, resulting in a random number $randarr=array (), while (count ($RANDARR) < $rewardNum) {$t =round (sqrt (Mt_rand (1,10000)/$this->realscatter); $randArr []= $t;} #计算当前生成的随机数的平均值, keep 4 decimal $randall=round (Array_sum ($RANDARR)/count ($RANDARR), 4); #为将生成的随机数的平均值变成我们要的1/N, Calculate the value that each random number generated will need to be divided by. We can deal with the last red envelope separately, so this can be about equal to processing. $mixrand =round ($randAll/$avgRand, 4); #对每一个随机数进行处理, and the amount of the red envelope left in the total amount. $REWARDARR =array (), foreach ($randArr as $key = = $randVal) {$randVal =round ($randVal/$mixrand, 4); $REWARDARR []=round ($this->rewardmoney* $randVal, 2);} #对比红包总数的差异, fix the last big red envelope sort ($REWARDARR), $rewardAll =array_sum ($REWARDARR); $REWARDARR [$this->rewardnum-1]= $this- >rewardmoney-($rewardAll-$REWARDARR [$this->rewardnum-1]); Rsort ($REWARDARR); #对红包进行排序一下以方便在前台图示展示foreach ($ Rewardarr as $k = $value) {$t = $k%2;if ($t) Array_push ($this->rewardarray, $value); else Array_unshift ($this Rewardarray, $value);} $REWARDARR =null;retuRN $this->rewardarray;    }} $money =1000; #总共要发的红包数; $people =50; #总共要发的人数 $scatter = 100; #分散度 $reward =new reward () $rewardArr = $reward->splitreward ($money, $people, $scatter); echo "The number of red envelopes issued: {$people}, The total amount of the red envelope {$money} yuan. Sum of all red envelopes below: ". Array_sum ($reward->rewardarray). ' Yuan. The following figure shows the distribution of red envelopes '; Echo ' 




In the uploaded files need to change 54 lines: $t =round (sqrt (Mt_rand (1,10000)/$this->realscatter), to control the value is not 0, I changed to 1, no test, may need to change the point, Because the value after the radical is reduced. The value of this line can also be directly ceil processing, there will be no red envelopes to 0 of the number.

I did not do much research on the value of scatter, but according to the mathematical model of the parabola, the meaning of this value can reduce the opening of the parabolic line, that is, the value of the red envelope can be scattered or concentrated

Related recommendations:

PHP Public number development of cash red envelopes

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.