Home > Backend Development > PHP Tutorial > I wrote a red envelope generation algorithm myself. It can be generated correctly, but it cannot be recursively generated?

I wrote a red envelope generation algorithm myself. It can be generated correctly, but it cannot be recursively generated?

WBOY
Release: 2016-07-06 13:52:36
Original
868 people have browsed it

Because you want to generate random red envelopes, you need to set the amount, minimum value, maximum value, and quantity of the red envelopes to be distributed.

I tried using a for loop, but it got stuck here in do while, so I wrote one using recursion. The correct red envelope can be generated in the memory, but it cannot be recursed. Please help me to find out where the problem lies?

<code><?php
//调试函数
function p($arr)
{
    echo "<pre class="brush:php;toolbar:false">" . print_r($arr, true) . "
"; } //发红包函数 function hongbao($money, $min, $max, $num, $arr = array(), $first = 'yes') { if ($first == 'yes') { //由于红包是以分为单位所以先转换单位,1元=100分,但只需转换一次 $money = $money * 100; $min = $min * 100; $max = $max * 100; //红包发放最大最小值合法性检测,防止发送死循环 if ($money - $min * $num < 0) { return "你发放红包的金额太小不足以 发给这么多人"; } else { if ($money - $max * $num > 0) { return "你发放红包的金额太大 这些人领不完"; } } } //计算已生成的红包总额 $safe_total = array_sum($arr); //如果红包总额 大于或等于 要发放的总额, 就说明红包已经生成完毕 if ($safe_total >= $money) { //防止出现 最后出现的数据都相同,所以让这里随机排序一下 shuffle($arr); /*这里有异常, 可以打印出来正确的值 却无法return到最外面吧,应该是递归上的问题*/ p($arr); return $arr; } else { //随机生成红包金额 $rand = mt_rand($min, $max); //用当前的钱 减去已发的钱 -减去本次要发的钱 $zx = $money - $safe_total - $rand - ($num - 1) * $min; $zd = $money - $safe_total - $rand - ($num - 1) * $max; if ($zx >= 0 && $zd <= 0) { //说明条件可以执行 $num--; $arr[] = $rand; $xrr = $arr; hongbao($money, $min, $max, $num, $arr, 'no'); } else { //说明发完本次红包就没法继续下次了 所以不做修改。 hongbao($money, $min, $max, $num, $arr, 'no'); } } } //发80块钱的红包, 最小1.3元 最大2, 数量为50个 $myarr = hongbao(80, 1.3, 2, 50); //打印下 myarr 看下赋值是否正确 , 函数里已经打印过一次, 所以要出现打印两次 数组才是正常 p($myarr);
Copy after login
Copy after login

Reply content:

Because you want to generate random red envelopes, you need to set the amount, minimum value, maximum value, and quantity of the red envelopes to be distributed.

I tried using a for loop, but it got stuck here in do while, so I wrote one using recursion. The correct red envelope can be generated in the memory, but it cannot be recursed. Please help me to find out where the problem lies?

<code><?php
//调试函数
function p($arr)
{
    echo "<pre class="brush:php;toolbar:false">" . print_r($arr, true) . "
"; } //发红包函数 function hongbao($money, $min, $max, $num, $arr = array(), $first = 'yes') { if ($first == 'yes') { //由于红包是以分为单位所以先转换单位,1元=100分,但只需转换一次 $money = $money * 100; $min = $min * 100; $max = $max * 100; //红包发放最大最小值合法性检测,防止发送死循环 if ($money - $min * $num < 0) { return "你发放红包的金额太小不足以 发给这么多人"; } else { if ($money - $max * $num > 0) { return "你发放红包的金额太大 这些人领不完"; } } } //计算已生成的红包总额 $safe_total = array_sum($arr); //如果红包总额 大于或等于 要发放的总额, 就说明红包已经生成完毕 if ($safe_total >= $money) { //防止出现 最后出现的数据都相同,所以让这里随机排序一下 shuffle($arr); /*这里有异常, 可以打印出来正确的值 却无法return到最外面吧,应该是递归上的问题*/ p($arr); return $arr; } else { //随机生成红包金额 $rand = mt_rand($min, $max); //用当前的钱 减去已发的钱 -减去本次要发的钱 $zx = $money - $safe_total - $rand - ($num - 1) * $min; $zd = $money - $safe_total - $rand - ($num - 1) * $max; if ($zx >= 0 && $zd <= 0) { //说明条件可以执行 $num--; $arr[] = $rand; $xrr = $arr; hongbao($money, $min, $max, $num, $arr, 'no'); } else { //说明发完本次红包就没法继续下次了 所以不做修改。 hongbao($money, $min, $max, $num, $arr, 'no'); } } } //发80块钱的红包, 最小1.3元 最大2, 数量为50个 $myarr = hongbao(80, 1.3, 2, 50); //打印下 myarr 看下赋值是否正确 , 函数里已经打印过一次, 所以要出现打印两次 数组才是正常 p($myarr);
Copy after login
Copy after login

I changed it according to what you gave:

<code class="php"><?php
//调试函数
function p($arr)
{
    echo "<pre class="brush:php;toolbar:false">" . print_r($arr, true) . "
"; } //发红包函数 function luckymoney($money, $min, $max, $num, $arr = array(), $first = 'yes') { if ($first == 'yes') { //由于红包是以分为单位所以先转换单位,1元=100分,但只需转换一次 $money = $money * 100; $min = $min * 100; $max = $max * 100; //红包发放最大最小值合法性检测,防止发送死循环 if ($money - $min * $num < 0) { return "你发放红包的金额太小不足以 发给这么多人"; } else { if ($money - $max * $num > 0) { return "你发放红包的金额太大 这些人领不完"; } } } //计算已生成的红包总额 $safe_total = array_sum($arr); //如果红包总额 大于或等于 要发放的总额, 就说明红包已经生成完毕 if ($num == 1) { // if ($safe_total >= $money) { //防止出现 最后出现的数据都相同,所以让这里随机排序一下 $arr[] = $money - $safe_total; shuffle($arr); /*这里有异常, 可以打印出来正确的值 却无法return到最外面吧,应该是递归上的问题*/ // p($arr); return $arr; } else { //随机生成红包金额 $rand = mt_rand($min, $max); //用当前的钱 减去已发的钱 -减去本次要发的钱 $zx = $money - $safe_total - $rand - ($num - 1) * $min; $zd = $money - $safe_total - $rand - ($num - 1) * $max; if ($zx >= 0 && $zd <= 0) { //说明条件可以执行 $num--; $arr[] = $rand; // $xrr = $arr; return luckymoney($money, $min, $max, $num, $arr, 'no'); } else { //说明发完本次红包就没法继续下次了 所以不做修改。 // luckymoney($money, $min, $max, $num, $arr, 'no'); return luckymoney($money, $min, $max, $num, $arr, 'no'); // p($arr); // $arr[] = $rand; // shuffle($arr); // return $arr; } } } //发80块钱的红包, 最小1.3元 最大2, 数量为50个 //$myarr = luckymoney(5, 1.3, 2, 3); $myarr = luckymoney(80, 1.3, 2, 50); //打印下 myarr 看下赋值是否正确 , 函数里已经打印过一次, 所以要出现打印两次 数组才是正常 p($myarr); $array_sum = array_sum($myarr); p($array_sum);
Copy after login

Note that since you are using recursion, and it is random based on luck, it is easy for stack overflow,

I tested your data luckymoney(80, 1.3, 2, 50) and it succeeded about 2 times out of 5 times. I suggest you change the algorithm...

return hongbao($money, $min, $max, $num, $arr, 'no');

Thank you everyone, I reused the for loop to improve the algorithm.
Calculated based on sending 200 yuan, minimum 1.3, maximum 3, 200 red envelopes are sent

Using the for loop algorithm, the function is executed 100 times, and the average execution time is 0.004 seconds each time
Using the recursive algorithm, the function is executed 100 times, and the average execution time is 0.211 seconds

The efficiency difference is 52.75 times. Above code. Thank you @aristotll for your suggestion

The following is the for loop code. If you have a better idea, please guide me, thank you

<code><?php
//调试函数
function p($arr)
{
    echo "<pre class="brush:php;toolbar:false">" . print_r($arr, true) . "
"; } //发红包函数 function hongbao($money, $min, $max, $num) { $arr = array(); //由于红包是以分为单位所以先转换单位,1元=100分,但只需转换一次 $money = $money * 100; $min = $min * 100; $max = $max * 100; //红包发放最大最小值合法性检测,防止发送死循环 if ($money - $min * $num < 0) { return "你发放红包的金额太小不足以 发给这么多人"; } else { if ($money - $max * $num > 0) { return "你发放红包的金额太大 这些人领不完"; } } $tempnum = $num; for ($i = 0; $i < $tempnum; $i++) { $flag = 'no'; do { //随机生成一个红包 $rand = mt_rand($min, $max); $anum = count($arr); $zx = $money - array_sum($arr) - $rand - ($num - 1) * $min; $zd = $money - array_sum($arr) - $rand - ($num - 1) * $max; $all = array_sum($arr); if ($zx >= 0 && $zd <= 0) { $arr[] = $rand; $flag = 'yes'; $num--; } } while ($flag == 'no'); } shuffle($arr); return $arr; } $myarr = hongbao(200, 1, 3, 100); p($myarr); echo array_sum($myarr)/100;
Copy after login
Related labels:
php
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