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

Google moves AndroidX to AOSP to make development more transparent

According to foreign media reports, Google is try...

Super sweet! This is the most touching love story I have ever heard

◎ Comprehensive report by Zhang Shuang of Science...

Android M Developer Preview 2 released

[[139789]] Google updated the Android M developer...

Is the Internet of Things really about to be realized?

[[125949]] Recently, Samsung President and CEO BK...

Douban’s methods and strategies for obtaining traffic!

Not long ago, Pinduoduo's market value surpas...

What! Soba noodles are unhealthy? The truth is...

Author: Xue Qingxin, registered dietitian, one of...

In addition to space junk, they also threaten spacecraft!

Not long ago, NASA, ESA and the Canadian Space Ag...