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

Galaxy S3 is so sad!

Today, a detailed progress report on the upgrade o...

How does Pinduoduo achieve user growth?

Recently, Pinduoduo has been caught up in a count...

How much does it cost for Dehong to join a lottery app?

What is the price for joining the Dehong Lottery ...

New vulnerability turns Android phones into trees?

Trend Micro reported a vulnerability that was mar...

Why iOS may become the next generation of enterprise operating system?

[[150553]] Throughout my life, Windows and Mac op...

The Verge's Best Tablets of 2015

We have seen a variety of new tablets this year, ...

The No. 1 game in PC history is born! A monster-level game that beats GTA

If someone asks you, what is the best PC game in ...

The world's first shot!

On May 1, the clinical study of the inactivated C...

Healthy eating tips for families: Eat to stay healthy and improve immunity

Do you have a low immune system and the flu virus...