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

How to play in Douyin store group? A complete guide to Doudian operation

With the popularity of Douyin live streaming and ...

Gesture password

Source code introduction: An iOS gesture password...

The latest gamification operation methodology in 2020!

Preface How can you make users addicted to your p...

What were once ten unicorns can now only produce an autopsy report

The world of unicorns is never short of stories. ...

Understand information flow video ads and their delivery strategies in 3 minutes!

On the one hand, the professionalism of “If you h...

What are the methods to preheat the market before the APP goes online?

In the film and television industry, we often hea...

Private Domain Traffic Practical Training Camp

Private Domain Traffic Practical Training Camp In...