


I wrote my own random distribution of fake WeChat red envelopes. Why is the average difference so big?
I have some free time these days, so I studied the algorithm of WeChat red envelopes. I checked it with Du Niang and saw an official answer: "Randomly, the amount is between 0.01 and (remaining average 2)." In other words, when everyone receives a red envelope, the amount generated is calculated instantly , this can reduce memory usage, and the amount of each red envelope is between 0.01 and (remaining average 2). That is to say, if there are 5 red envelopes for 100 yuan, the value of the first red envelope will be between 0.01 and 40. However, anyone who has played red envelope grabbing knows that this is wrong. It is common for the first one to grab and get more than the remaining average * 2.
Before I saw this answer, my idea was to distribute the amount after giving out the red envelope, and then wait for others to open the red envelope.
<code>public function index($money,$count){ if($count==1){ echo $money;exit; } $max=$money*100; if($max<$count){ echo '钱太少,人太多,不够分';exit; } $data=array(); $arr=array(); if($count==2){ $arr[]=mt_rand(1,$max-1); }else{ $a=range(1,$max-1); shuffle($a); $arr= array_rand($a,$count-1); } for($i=0;$i<=$count-1;$i++){ if($i==0){ $data[$i]=$arr[$i]; }elseif($i==$count-1){ $data[$i]=$max-$arr[$i-1]; }else{ $data[$i]=$arr[$i]-$arr[$i-1]; } $data[$i]=$data[$i]/100; //echo $data[$i].'<br/>'; } return $data; }</code>
My idea is that the total amount of red envelopes is equal to the length of a straight line, and then n-1 points are randomly placed on the straight line, and the distance between points is equal to the amount of each red envelope. At first, I thought that this would be relatively average. Then I tested sending 10 red envelopes for 100 yuan, and tested it 10,000 times, and found that
And this is the result after I messed up the sorting of points and point lengths,
If this is the case without disturbing the previous results, the difference will be even greater.
It can be clearly seen that it is far from the average. Is my idea wrong to begin with?
Reply content:
I have some free time these days, so I studied the algorithm of WeChat red envelopes. I checked it with Du Niang and saw an official answer: "Randomly, the amount is between 0.01 and (remaining average 2)." In other words, when everyone receives a red envelope, the amount generated is calculated instantly , this can reduce memory usage, and the amount of each red envelope is between 0.01 and (remaining average 2). That is to say, if there are 5 red envelopes for 100 yuan, the value of the first red envelope will be between 0.01 and 40. However, anyone who has played red envelope grabbing knows that this is wrong. It is common for the first one to grab and get more than the remaining average * 2.
Before I saw this answer, my idea was to distribute the amount after giving out the red envelope, and then wait for others to open the red envelope.
<code>public function index($money,$count){ if($count==1){ echo $money;exit; } $max=$money*100; if($max<$count){ echo '钱太少,人太多,不够分';exit; } $data=array(); $arr=array(); if($count==2){ $arr[]=mt_rand(1,$max-1); }else{ $a=range(1,$max-1); shuffle($a); $arr= array_rand($a,$count-1); } for($i=0;$i<=$count-1;$i++){ if($i==0){ $data[$i]=$arr[$i]; }elseif($i==$count-1){ $data[$i]=$max-$arr[$i-1]; }else{ $data[$i]=$arr[$i]-$arr[$i-1]; } $data[$i]=$data[$i]/100; //echo $data[$i].'<br/>'; } return $data; }</code>
My idea is that the total amount of red envelopes is equal to the length of a straight line, and then n-1 points are randomly placed on the straight line, and the distance between points is equal to the amount of each red envelope. At first, I thought that this would be relatively average. Then I tested sending 10 red envelopes for 100 yuan, and tested it 10,000 times, and found that
And this is the result after I messed up the sorting of points and point lengths,
If this is the case without disturbing the previous results, the difference will be even greater.
It can be clearly seen that it is far from the average. Is my idea wrong to begin with?
See if the red envelope algorithm here meets your requirements
https://github.com/qieangel2013/yaf
The WeChat amount is calculated in real time when splitting. It uses pure memory calculation and does not require budget space storage. Consideration for real-time calculation of amounts: the budget requires storage, and real-time efficiency is very high.
This answer is well analyzed. You can check it out http://coderroc.com/article/%E6%95%B0%E5%AD%A6%E5%92%8C%E7%AE%97%E6%B3%95 /%E5%BE%AE%E4%BF%A1%E7%BA%A2%E5%8C%85%E9%9A%8F%E6%9C%BA%E7%AE%97%E6%B3%95% E5%88%9D%E6%8E%A2.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
