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

World Oceans Day丨Today, let’s get to know this friend in the deep sea again

Expert of this article: Liu Yadan, former assista...

Replacing global fonts in a rough way in Android

[[204795]] sequence Using custom fonts on Android...

Dyson electric car details revealed: the first may be a performance sports car

When it comes to Dyson, the first thing that come...

Online consumer finance product customer acquisition and operation methodology

After the beginning of spring, the weather is gra...

Zhao Yu's "User Experience Design Practical Notes"

When talking about a product, people often use use...

Mother’s Day Marketing Promotion Case Analysis!

A Boston company called Rehtom once posted a very...

What is aggregate thinking? What types of aggregation pages are there?

After completing the basic steps of on-site optim...

Tencent Interactive Entertainment Community Product User Growth Formula!

The development of the Internet has entered the s...

How to develop an online event planning plan?

Even those who are just starting out in operation...

Can “sad” music make people happy? I’m confused…

Scientists are intrigued by the fact that we can ...