


I wrote a red envelope generation algorithm myself. It can be generated correctly, but it cannot be recursively generated?
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);
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);
I changed it according to what you gave:
<?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);
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;

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
