PHP implements red envelope function code

小云云
Release: 2023-03-21 09:16:01
Original
2623 people have browsed it

This article mainly shares with you the red envelope function implemented in php, mainly in the form of code. Since I am the first to write red envelopes and don’t know much about red envelopes, I hope it can help everyone.

for ($i=1; $i < $p; ++$i) { 
	$s2 = ($sum - ($p - $i) * 0.01)/($p - $i);  
	//echo $s2;die;
    $money = rand(1,$s2*100);
    $money = number_format($money/100,2,&#39;.&#39;,&#39;,&#39;); 
	$sum = $sum - $money;
	$wamp[$i] = $money;
	
}
   //format_number(要转换的浮点数,要分隔的符号)
   echo "<hr/>";
   echo "<pre class="brush:php;toolbar:false">";
    print_r($wamp);
    echo "<pre class="brush:php;toolbar:false">";


}
//echo 2;
Copy after login

The red envelope function can be basically realized and the main thoughts are discussed

There is a scenario where there are 50 students in a classmate group who want to give out red envelopes to celebrate the party

First of all, everyone must receive it Receive a red envelope and the probability of everyone receiving a red envelope is basically the same

Then the probability of everyone receiving a red envelope = (The current amount of the red envelope - (The number of people who have not received the red envelope currently) * The minimum amount of red envelopes received by each person) / The current number of people who have not received a red envelope

$safe_total The probability of each person receiving a red envelope $total The current amount of red envelopes $min The minimum amount of red envelopes for each person

Then the formula can be $safe_total = ($total - ($num - $i) * $min) / ($num - $i);

The code is not unique, the important thing is the thought

The following is the reference code

function getRedGift($total, $num = 10)
{
$min = 0.01;
$wamp = array();
$returnData = array();
for ($i = 1; $i < $num; ++$i) {
$safe_total = ($total - ($num - $i) * $min) / ($num - $i); //红包金额的最大值    为了是大部分个人获得的红包金额一定
if ($safe_total < 0) break;
$money = @mt_rand($min * 100, $safe_total * 100) / 100;//随机产生一个红包金额
$total = $total - $money;//剩余红包总额
$wamp[$i] = round($money, 2);//保留两位有效数字
}
$wamp[$i] = round($total, 2);
$returnData[&#39;MoneySum&#39;] = $wamp;
$returnData[&#39;newTotal&#39;] = array_sum($wamp);
return $returnData;
}
//测试
$data = getRedGift(100, 10);
echo "<pre class="brush:php;toolbar:false">";


print_r($data);


echo "<pre class="brush:php;toolbar:false">";
Copy after login

Related recommendations:

PHP development of WeChat cash red envelope function code sharing

PHP random red envelope function implementation

php implements lucky red envelope function

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template