How is the random algorithm of WeChat red envelopes implemented?

How is the random algorithm of WeChat red envelopes implemented?

I saw a question on Zhihu: How is the random algorithm for WeChat red envelopes implemented?

[[218949]]

Some people say that Tencent achieved this roughly like this:

  1. public static double getRandomMoney(LeftMoneyPackage _leftMoneyPackage) {
  2. // remainSize The remaining number of red packets
  3. // remainMoney remaining money
  4. if (_leftMoneyPackage.remainSize == 1) {
  5. _leftMoneyPackage.remainSize--;
  6. return (double) Math.round(_leftMoneyPackage.remainMoney * 100) / 100;
  7. }
  8. Random r = new Random();
  9. double min = 0.01; //
  10. double max = _leftMoneyPackage.remainMoney / _leftMoneyPackage.remainSize * 2;
  11. double money = r.nextDouble() * max;
  12. money = money <= min ? 0.01: money;
  13. money = Math.floor(money * 100) / 100;
  14. _leftMoneyPackage.remainSize--;
  15. _leftMoneyPackage.remainMoney -= money;
  16. return money;
  17. }

Some people have also done normal distribution, variance analysis, regression analysis, statistical simulation, etc., but I won’t post it because the picture is too long.

However

  • All answers are "random when taken", that is, the concept of designing a "red envelope pool" and then randomly taking numbers when drawing.
  • All answers are "random of money", that is, a random amount of money, and then returned.

Let’s change our thinking. Now we change all the money into 1-cent coins, imagine the red envelope as a jar, and then scatter the coins.

  1. /**
  2. * @param count number of red packets
  3. * @param money total amount
  4. * @return  
  5. */
  6. public   static   Integer []ranRedPac( Integer   count , Integer money) {
  7. Integer [] result = new Integer [ count ];
  8. for ( int i = 1; i <= money; i++) {
  9. int n = new Random().nextInt( count );
  10. result[n] = result[n] == null ? 1 : result[n] + 1;
  11. }
  12. return result;
  13. }
  14.  
  15. //test
  16. public   static void main(String[] args) {
  17. Arrays.asList(ranRedPac(10, 5000000)).forEach(i -> System. out .println(i));
  18. System. out .println( "sum: " + Arrays.asList(ranRedPac(10, 50)).stream().mapToInt(i -> i). sum ());
  19. }

Red envelopes are randomly selected for every penny.

As for regression analysis and statistical simulation, they are of no use at all.

In this example, we abandon traditional concepts such as "drawing" and "random amount", so that money has a sense of choice and performs "random" behavior. Naturally, the red envelope has the attribute of random amount.

Change your thinking and don't complicate simple problems.

When we design code, we usually consider the logic in real life and abstract objects into classes and behaviors into methods. However, we also need to consider the reversal of thinking occasionally.

Of course, my code has certain drawbacks.

Thinking is the most important thing.

<<:  Xiaomi Mi A1 releases kernel source code: turns into the little prince of flashing

>>:  Taobao's PlayerUnknown's Battlegrounds: Is the launch of "Mini Programs" a counterattack against WeChat or JD.com?

Recommend

"Twenty-five, grind tofu", eating this way is healthier!

Review: Experts from the National Health Science ...

National Malaria Day丨From 30 million cases to 0 cases, what did it take?

April 26th is the 15th National Malaria Day. Spea...

5 practical typography tips to improve the high-end feel of UI interface

In the UI interface, typography design is equally...

Love life and prevent suicide, what can we do?

Follow "Body Code Decoding Bureau" (pub...

Why can e-commerce mini programs break the bottleneck of merchant development?

With the rise of mobile Internet, the development...

New brand Xiaohongshu content marketing? 28 suggestions!

Is there any shortcut to marketing on Xiaohongshu...