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

不言
Release: 2023-03-24 14:08:01
Original
1869 people have browsed it


Today I studied the red envelope generation algorithm. It seems simple, but in practice it is not that different. Of course, if you just want to use a rand to generate a few random numbers, then that's fine. It means nothing.

You should be a little particular about giving out red envelopes. You must allow everyone assigned to receive a non-zero red envelope, and at the same time, most people must receive an average red envelope.

Not much else to say, I have written a lot of comments in the program, the code is as follows:

rewardArray=array(); } #执行红包生成算法 public function splitReward($rewardMoney,$rewardNum,$scatter=100) { #传入红包金额和数量 $this->rewardMoney=$rewardMoney; $this->rewardNum=$rewardNum; $this->scatter=$scatter; $this->realscatter=$this->scatter/100; /* *前言:今天我突然这样一想,比如要把1个红包分给N个人,实际上就是相当于要得到N个百分比数据 * 条件是这N个百分比之和=100/100。这N个百分比的平均值是1/N。 * 并且这N个百分比数据符合一种正态分布(多数值比较靠近平均值) *观点:微信红包里很多0.01的红包,我觉得这是微信程序里的人为控制,目的是为了防止总红包数超过总额,先分了几个0.01的红包。 * 不然不管是以随机概率还是正态分布都很难会出现非常多的0.01元红包。 */ #我的思路:正如上面说的,比如:1个红包发给5个人,我要得出5个小数,它们的和是1,他们的平均值是1/5 #计算出发出红包的平均概率值、精确到小数4位。即上面的1/N值。 $avgRand=round(1/$this->rewardNum,4); #红包的向平均数集中的分布正像数学上的抛物线。抛物线y=ax2,|a|越大则抛物线的开口就越小,|a|越小则抛物线的开口就越大,a>0时开口向上,我们这都是正数,就以a>0来考虑吧。 #程序里的$scatter值即为上方的a,此值除以100,当做100为基准, #通过开方(数学里的抛物线模型,开方可缩小变化值)得出一个小数字较多(小数字多即小红包多)的随机分布,据此生成随机数 $randArr=array(); while(count($randArr)<$rewardNum) { $t=round(sqrt(mt_rand(1,10000)/$this->realscatter)); $randArr[]=$t; } #计算当前生成的随机数的平均值,保留4位小数 $randAll=round(array_sum($randArr)/count($randArr),4); #为将生成的随机数的平均值变成我们要的1/N,计算一下生成的每个随机数都需要除以的值。我们可以在最后一个红包进行单独处理,所以此处可约等于处理。 $mixrand=round($randAll/$avgRand,4); #对每一个随机数进行处理,并剩以总金额数来得出这个红包的金额。 $rewardArr=array(); foreach($randArr as $key=>$randVal) { $randVal=round($randVal/$mixrand,4); $rewardArr[]=round($this->rewardMoney*$randVal,2); } #对比红包总数的差异、修正最后一个大红包 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 "发放红包个数:{$people},红包总金额{$money}元。下方所有红包总额之和:".array_sum($reward->rewardArray).'元。下方用图展示红包的分布'; echo '
'; echo ""; foreach($rewardArr as $val) { #线条长度计算 $width=intval($people*$val*300/$money); echo ""; } echo "
红包金额图示
{$val}
"; ?>
Copy after login




You need to change line 54 in the uploaded file: $t=round(sqrt(mt_rand(1,10000)/$ this->realscatter));, to control the value from being 0, I changed it to 1. I haven’t tested it yet. You may need to change it a bit larger, because the value after the square root will shrink. You can also directly perform ceil processing on the value of this row, so that the number of red envelopes with a value of 0 will not appear.

I haven’t done much research on the value of scatter, but according to the mathematical model of the parabola, the meaning of this value can make the mouth of the parabola expand or shrink, that is, it can make the value of the red envelope dispersed or concentrated

Related recommendations:

php WeChat public account development cash red envelope



The above is the detailed content of PHP uses parabolic model to implement the program source code of WeChat red envelope generation algorithm. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!