Home  >  Article  >  Backend Development  >  I wrote my own random distribution of fake WeChat red envelopes. Why is the average difference so big?

I wrote my own random distribution of fake WeChat red envelopes. Why is the average difference so big?

WBOY
WBOYOriginal
2016-08-04 09:20:451156browse

I have some free time these days, so I studied the algorithm of WeChat red envelopes. I checked it with Du Niang and saw an official answer: "Randomly, the amount is between 0.01 and (remaining average 2)." In other words, when everyone receives a red envelope, the amount generated is calculated instantly , this can reduce memory usage, and the amount of each red envelope is between 0.01 and (remaining average 2). That is to say, if there are 5 red envelopes for 100 yuan, the value of the first red envelope will be between 0.01 and 40. However, anyone who has played red envelope grabbing knows that this is wrong. It is common for the first one to grab and get more than the remaining average * 2.
Before I saw this answer, my idea was to distribute the amount after giving out the red envelope, and then wait for others to open the red envelope.

<code>public function index($money,$count){
        if($count==1){
            echo $money;exit;
        }
        $max=$money*100;
        
        if($max<$count){
            echo '钱太少,人太多,不够分';exit;
        }
        
        $data=array();
        $arr=array();  
        if($count==2){
            $arr[]=mt_rand(1,$max-1);
        }else{
            $a=range(1,$max-1);
            shuffle($a);
            $arr= array_rand($a,$count-1);
        }
        for($i=0;$i<=$count-1;$i++){
            if($i==0){
                $data[$i]=$arr[$i];
            }elseif($i==$count-1){
                $data[$i]=$max-$arr[$i-1];
            }else{
                $data[$i]=$arr[$i]-$arr[$i-1];
            }
            $data[$i]=$data[$i]/100;
            //echo $data[$i].'<br/>';
        }
        return $data;
}</code>

My idea is that the total amount of red envelopes is equal to the length of a straight line, and then n-1 points are randomly placed on the straight line, and the distance between points is equal to the amount of each red envelope. At first, I thought that this would be relatively average. Then I tested sending 10 red envelopes for 100 yuan, and tested it 10,000 times, and found that

I wrote my own random distribution of fake WeChat red envelopes. Why is the average difference so big?
And this is the result after I messed up the sorting of points and point lengths,

I wrote my own random distribution of fake WeChat red envelopes. Why is the average difference so big?
If this is the case without disturbing the previous results, the difference will be even greater.
It can be clearly seen that it is far from the average. Is my idea wrong to begin with?

Reply content:

I have some free time these days, so I studied the algorithm of WeChat red envelopes. I checked it with Du Niang and saw an official answer: "Randomly, the amount is between 0.01 and (remaining average 2)." In other words, when everyone receives a red envelope, the amount generated is calculated instantly , this can reduce memory usage, and the amount of each red envelope is between 0.01 and (remaining average 2). That is to say, if there are 5 red envelopes for 100 yuan, the value of the first red envelope will be between 0.01 and 40. However, anyone who has played red envelope grabbing knows that this is wrong. It is common for the first one to grab and get more than the remaining average * 2.
Before I saw this answer, my idea was to distribute the amount after giving out the red envelope, and then wait for others to open the red envelope.

<code>public function index($money,$count){
        if($count==1){
            echo $money;exit;
        }
        $max=$money*100;
        
        if($max<$count){
            echo '钱太少,人太多,不够分';exit;
        }
        
        $data=array();
        $arr=array();  
        if($count==2){
            $arr[]=mt_rand(1,$max-1);
        }else{
            $a=range(1,$max-1);
            shuffle($a);
            $arr= array_rand($a,$count-1);
        }
        for($i=0;$i<=$count-1;$i++){
            if($i==0){
                $data[$i]=$arr[$i];
            }elseif($i==$count-1){
                $data[$i]=$max-$arr[$i-1];
            }else{
                $data[$i]=$arr[$i]-$arr[$i-1];
            }
            $data[$i]=$data[$i]/100;
            //echo $data[$i].'<br/>';
        }
        return $data;
}</code>

My idea is that the total amount of red envelopes is equal to the length of a straight line, and then n-1 points are randomly placed on the straight line, and the distance between points is equal to the amount of each red envelope. At first, I thought that this would be relatively average. Then I tested sending 10 red envelopes for 100 yuan, and tested it 10,000 times, and found that

I wrote my own random distribution of fake WeChat red envelopes. Why is the average difference so big?
And this is the result after I messed up the sorting of points and point lengths,

I wrote my own random distribution of fake WeChat red envelopes. Why is the average difference so big?
If this is the case without disturbing the previous results, the difference will be even greater.
It can be clearly seen that it is far from the average. Is my idea wrong to begin with?

See if the red envelope algorithm here meets your requirements
https://github.com/qieangel2013/yaf

The WeChat amount is calculated in real time when splitting. It uses pure memory calculation and does not require budget space storage. Consideration for real-time calculation of amounts: the budget requires storage, and real-time efficiency is very high.

This answer is well analyzed. You can check it out http://coderroc.com/article/%E6%95%B0%E5%AD%A6%E5%92%8C%E7%AE%97%E6%B3%95 /%E5%BE%AE%E4%BF%A1%E7%BA%A2%E5%8C%85%E9%9A%8F%E6%9C%BA%E7%AE%97%E6%B3%95% E5%88%9D%E6%8E%A2.html

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