Current Location: Home> Latest Articles> PHP Implementation of WeChat Red Packet Amount Splitting Algorithm | Random Red Packet Distribution Method

PHP Implementation of WeChat Red Packet Amount Splitting Algorithm | Random Red Packet Distribution Method

gitbox 2025-07-27

Algorithm Example

The splitting of red packet amounts is a common problem. This article demonstrates how to implement a red packet amount splitting algorithm using PHP to help developers better understand the random distribution of red packet amounts.

Creating a Function

First, we need to create a function to split the red packet amount. The function takes two parameters: the red packet amount and the number of red packets.


function divideAmount($amount, $num) {
    // TODO: Implement the red packet amount splitting algorithm
}

Algorithm Logic

Next, let's analyze the algorithm logic for splitting the red packet amount.

Assume the red packet amount is A yuan, and the number of red packets is N. The maximum amount for each red packet is A/N yuan, and the minimum amount is 0.01 yuan.

We can first generate N-1 random numbers between 0 and A as the initial amounts for the red packets. Then, we distribute the remaining amount to the last red packet.

Algorithm Implementation

Now, let's implement the algorithm for splitting the red packet amount.


function divideAmount($amount, $num) {
    $remainAmount = $amount; // Remaining amount
    $remainNum = $num; // Remaining red packet count
    $result = array(); // Splitting result
    for ($i = 0; $i < $num - 1; $i++) {
        $maxAmount = $remainAmount / $remainNum * 2; // Maximum amount
        $randAmount = mt_rand(1, $maxAmount * 100) / 100; // Randomly generate amount, keeping two decimal places
        $remainAmount -= $randAmount; // Update remaining amount
        $remainNum--; // Update remaining red packet count
        $result[] = $randAmount; // Add the amount to the result array
    }
    $result[] = $remainAmount; // Add the remaining amount to the result array
    return $result;
}

Algorithm Explanation

First, we define two variables: $remainAmount for the remaining amount and $remainNum for the remaining number of red packets.

In the loop, we first calculate the maximum amount, $maxAmount, then use the mt_rand function to generate a random amount, $randAmount, between 1 and $maxAmount. After that, we update the remaining amount and the number of red packets, and add $randAmount to the result array.

Finally, we add the remaining amount to the result array and return the result array.

Example Run

Let's test the algorithm by running an example.


$amount = 10; // Red packet amount
$num = 5; // Number of red packets
$result = divideAmount($amount, $num);
foreach ($result as $amount) {
    echo $amount . "yuan\n";
}

Running the above code, the output will be as follows:


1.07 yuan
2.48 yuan
2.28 yuan
2.42 yuan
1.75 yuan

As you can see, the red packet amount is successfully split into five different amounts.

Conclusion

This article has demonstrated a PHP implementation of the WeChat red packet amount splitting algorithm.

By analyzing the algorithm's logic and implementation details, we can clearly understand the process of splitting red packet amounts.

If you are developing a WeChat red packet feature, you may find this algorithm example useful.