I am idle these days and have studied the algorithm of red packets myself. After checking this information, the Ms official replied: "random, the quota is between 0.01 and (remaining average 2 ." That is to say, when each person receives a red packet, the generated amount is calculated in real time, which can reduce the memory usage...
I am idle these days and have studied the algorithm of red packets myself. After checking the quota, the Ms officially replied: "random, the quota is at 0.01 and (the remaining average)2 ." That is to say, when each person receives a red packet, the generated amount is calculated in real time, which can reduce the memory usage, and the amount of each red packet is at 0.01 and (the remaining average value)2) between, that is to say, if there are 100 yuan and 5 red envelopes, the money for the first red envelope will be between 0.01 and 40, but anyone who has ever snatched the red envelope knows that this is wrong, it is common for the first attacker to take more than the remaining average value * 2.
Before I saw this answer, I imagined that I had allocated the money after sending the red packet and waited for someone to open the red packet.
Public function index ($ money, $ count) {if ($ count = 1) {echo $ money; exit;} $ max = $ money * 100; if ($ max <$ count) {echo 'too little money, too many people, not enough minutes '; 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 a red envelope is equal to the length of a straight line. then, random n-1 points are placed on the straight line. The distance between points is equal to the amount of each red envelope. At first, I thought it would be relatively average. then I tested 100 million yuan, issued 10 red packets, and tested 10 thousand times, and found that
In addition, this is the result obtained after sorting the point and the point length,
If the previous results are not disrupted, the difference is even greater.
Obviously, it is far from the average. is my idea wrong?
Reply content:
I am idle these days and have studied the algorithm of red packets myself. After checking the quota, the Ms officially replied: "random, the quota is at 0.01 and (the remaining average)2 ." That is to say, when each person receives a red packet, the generated amount is calculated in real time, which can reduce the memory usage, and the amount of each red packet is at 0.01 and (the remaining average value)2) between, that is to say, if there are 100 yuan and 5 red envelopes, the money for the first red envelope will be between 0.01 and 40, but anyone who has ever snatched the red envelope knows that this is wrong, it is common for the first attacker to take more than the remaining average value * 2.
Before I saw this answer, I imagined that I had allocated the money after sending the red packet and waited for someone to open the red packet.
Public function index ($ money, $ count) {if ($ count = 1) {echo $ money; exit;} $ max = $ money * 100; if ($ max <$ count) {echo 'too little money, too many people, not enough minutes '; 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 a red envelope is equal to the length of a straight line. then, random n-1 points are placed on the straight line. The distance between points is equal to the amount of each red envelope. At first, I thought it would be relatively average. then I tested 100 million yuan, issued 10 red packets, and tested 10 thousand times, and found that
In addition, this is the result obtained after sorting the point and the point length,
If the previous results are not disrupted, the difference is even greater.
Obviously, it is far from the average. is my idea wrong?
Check whether the red envelope algorithm meets your requirements.
Https://github.com/qieangel2013/yaf
The amount is calculated in real time during split. it is calculated in pure memory and does not need to be stored in budget space. Taking into account the amount calculated in real time: The budget needs to occupy storage, and the real-time efficiency is very high.
Good analysis of this answer you can look at the 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