Home  >  Article  >  Backend Development  >  How to implement red envelope function in php code

How to implement red envelope function in php code

藏色散人
藏色散人Original
2021-11-02 09:39:072607browse

How to implement the red envelope function in PHP code: 1. Use the distribute_red_bages method to achieve lucky red envelopes; 2. Use the average_red_bages method to achieve even distribution of red envelopes; 3. Use the rob_red_bages method to achieve the specified number of red envelopes.

How to implement red envelope function in php code

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How does the php code implement the red envelope function?

PHP red envelope function code

I was asked this question some time ago, so I wrote it down when I had time recently. It’s quite interesting

First of all Let’s classify the methods of grabbing red envelopes:

For people who send red envelopes, there are roughly 3 categories (I haven’t thought of the others yet, please contact me if you think of them~)

1. The number of lucky red envelopes is variable (it all depends on luck)

2. The specified number of lucky red envelopes (the number is certain, at least one point)

3. The red envelopes are divided evenly (the number is one) (determined, everyone will share it equally) This is easier to write, right? Hehe

Code above:

<?php
var_dump(rob_red_bages(5 , 10));
var_dump(distribute_red_bages(5));
/*
方法主要功能:拼手气红包(个数不定)
一个参数
    参数一: 红包总金额(按分计算)
*/
function distribute_red_bages($sum){
    $sum = $sum*100;
    $i=0;
    while($sum>0){
        $temp = rand(1 , $sum);//红包值
        $sum -= $temp;
        $arr[$i++] = $temp/100;
    }
    //check($arr);
    return $arr;
}
/*
方法主要功能:均分红包
两个参数:
    参数一: 红包总金额
    参数二: 均分个数
*/
function average_red_bages($sum , $num){
    $res = $sum/$num;
    for($i=0 ; $i<$num ; $i++){
        $arr[$i] = $res;
    }
    //check($arr);
    return $arr;
}
/*
方法主要功能:规定个数的手气红包
两个参数: 
    参数一:红包总金额
    参数二:红包个数
    
    计算流程,随机生成num个数
    求和得到m
    用sum/m 得到每个数需要k
    用随机数*k
*/
function rob_red_bages($sum , $num){
    $sum = $sum*100;
    for($i=0 ; $i<$num ; $i++){
        $temp = rand(1 , $sum);
        $arr[$i] = $temp;
        $sumall += $temp;
    }
    $k = $sum/$sumall;
    for($i=0 ; $i<sizeof($arr); $i++){
        $arr2[$i] = $arr[$i]*$k/100;        
    }
    return $arr2;
}
/*红包总额检测*/
function check($arr){
    foreach($arr as $a){
        $sum += $a;
        echo $a."+";
    }
    
    echo " 0 =".$sum."\n";
}
?>

Let’s analyze the lucky red envelopes (I won’t talk about the equal share, everyone knows it):

The number of lucky red envelopes is uncertain: (Total amount: sum)

---->Randomly the first number less than sum: n

---->Subtract this number from the total amount: temp = sum -n

---->Randomly a number less than temp

---->Recurse the above method until the total amount is 0

---- >Return an array that stores random results

This method is relatively conventional, and it can also be used to achieve a specified number of red envelopes! However~

The idea of ​​​​specifying the number of lucky red envelopes (total amount sum, number of red envelopes num)

---->Randomly num numbers less than sum, generate an array

---->Sum these numbers to get allsum

---->Divide sum by allsum to get the common multiple of all generated numbers

----> ;Multiply each element of the array by a common multiple to get the value of each red envelope

This method has shortcomings (the final result may be slightly larger than sum, or slightly smaller than sum because division may result in irrational numbers)

That’s probably it! Welcome to communicate and make corrections! ~~~

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement red envelope function in php code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn