These days more busy, their own study of the red Envelopes algorithm. With degrees Niang looked at, saw an official answer: "Random, the quota is 0.01 and (the remaining mean 2)." In other words, when everyone takes a red envelope, the amount generated is calculated instantly, which reduces memory consumption, and each red envelope is between 0.01 and (the remaining mean 2), that is, if 100 pieces of 5 red envelopes, The first red envelope money will be between 0.01 to 40, but the people who have played the red envelopes know that it is wrong, the first grab, take more than the remaining average * * is a very common thing.
and I did not see this answer before, my idea is to send a red envelope after the amount has been allocated, and then wait for someone to open the red envelope.
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].'
'; } return $data;}
My idea is that the total amount of red envelopes is equal to the length of a straight line, and then randomly placed on a line n-1 points, the distance between points is equal to the amount of each red envelope. At first, I thought, this would be relatively average, then I tested 100 pieces of 10 red envelopes, tested 10,000 times, the results found
And this is the result of my messing up the point and the length of the order,
The difference is greater if the previous results are not disturbed.
Obviously, from the average, poor good far, is my thinking is wrong?
Reply content:
These days more busy, their own research on the algorithm of red envelopes. Use degree Niang looked up, saw an official answer: "Random, the quota is between 0.01 and (the remaining average 2)." In other words, when everyone takes a red envelope, the amount generated is calculated instantly, which reduces the memory footprint, and each red envelope is between 0.01 and (the remaining average of 2), that is, if 100 pieces of 5 red envelopes, The first red envelope money will be between 0.01 to 40, but the people who have played the red envelopes know that it is wrong, the first grab, take more than the remaining average * * is a very common thing.
Before I saw this answer myself, my idea was to distribute the money after the red envelope, and then wait for someone to open the red envelope.
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].'
'; } return $data;}
My idea is that the total amount of red envelopes is equal to the length of a straight line, and then randomly placed on a line n-1 points, the distance between points is equal to the amount of each red envelope. At first, I thought, this would be relatively average, then I tested 100 pieces of 10 red envelopes, tested 10,000 times, the results found
And this is the result of my messing up the point and the length of the order,
The difference is greater if the previous results are not disturbed.
Obviously, from the average, poor good far, is my thinking is wrong?
Look at the red envelope algorithm here.
Https://github.com/qieangel2013/yaf
The amount is calculated in real time when split, using pure memory calculation, no need for budget space storage. Take the real-time calculation of the amount of consideration: the budget needs to occupy storage, real-time efficiency is high.
This is a good analysis, you can look at 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