I wrote a red envelope generation algorithm. can I generate a correct algorithm without recursion?

Source: Internet
Author: User
To generate a random red envelope, you must set the amount, minimum value, maximum value, and quantity of the red envelope to be issued. I tried to use the for loop, but it would be stuck in the dowhile, so I wrote one recursively. the correct red envelope can be generated in the memory, but it cannot be recursive. Please help me. because you want to generate random red packets, and you need to set the amount, minimum value, maximum value, and quantity of red packets to be issued.

I tried to use the for loop, but it would be stuck in do while, so I wrote a recursive code. the correct red envelope can be generated in the memory, but it cannot be recursive. Can you help me find out what the problem is?


  ". Print_r ($ arr, true )."
";}// Function of sending a red packet: hongbao ($ money, $ min, $ max, $ num, $ arr = array (), $ first = 'yes ') {if ($ first = 'yes') {// because the red packet is divided into units, the unit is converted first. 1 RMB = 100 points, however, you only need to convert $ money = $ money * 100; $ min = $ min * 100; $ max = $ max * 100; // check the validity of the maximum and minimum values of red packets, prevent sending endless loops if ($ money-$ min * $ num <0) {return "the amount of your red packet is too small to be sent to so many people ";} else {if ($ money-$ max * $ num> 0) {return "the amount of money you pay for a red envelope is too large ";}}} // calculate the total amount of generated red packets $ safe_total = array _ Sum ($ arr); // if the total amount of red packets is greater than or equal to the total amount to be issued, it indicates that the red packet has been generated if ($ safe_total> = $ money) {// prevent the last occurrence of the same data, so let's randomly sort shuffle ($ arr) here;/* there is an exception, you can print the correct value but cannot return to the outermost side. it should be a recursion problem */p ($ arr); return $ arr ;} else {// randomly generated red packet amount $ rand = mt_rand ($ min, $ max ); // subtract the money sent from the current money-minus the money to be sent this time $ zx = $ money-$ safe_total-$ rand-($ num-1) * $ min; $ zd = $ money-$ safe_total-$ rand-($ num-1) * $ max; if ($ zx> = 0 & $ zd <= 0) {// You can execute $ num --; $ arr [] = $ rand; $ xrr = $ arr; hongbao ($ money, $ min, $ max, $ num, $ arr, 'no');} else {// indicates that the next time the red packet is sent, it cannot be modified. Hongbao ($ money, $ min, $ max, $ num, $ arr, 'no') ;}}// you can send a red packet of 80 RMB. the minimum value is 1.3 RMB and the maximum value is 2, the number is 50 $ myarr = hongbao (80, 1.3, 2, 50); // Print myarr and check whether the assignment is correct. the function has already printed it once, so it is normal to print the array twice ($ myarr );

Reply content:

To generate a random red envelope, you must set the amount, minimum value, maximum value, and quantity of the red envelope to be issued.

I tried to use the for loop, but it would be stuck in do while, so I wrote a recursive code. the correct red envelope can be generated in the memory, but it cannot be recursive. Can you help me find out what the problem is?


  ". Print_r ($ arr, true )."
";}// Function of sending a red packet: hongbao ($ money, $ min, $ max, $ num, $ arr = array (), $ first = 'yes ') {if ($ first = 'yes') {// because the red packet is divided into units, the unit is converted first. 1 RMB = 100 points, however, you only need to convert $ money = $ money * 100; $ min = $ min * 100; $ max = $ max * 100; // check the validity of the maximum and minimum values of red packets, prevent sending endless loops if ($ money-$ min * $ num <0) {return "the amount of your red packet is too small to be sent to so many people ";} else {if ($ money-$ max * $ num> 0) {return "the amount of money you pay for a red envelope is too large ";}}} // calculate the total amount of generated red packets $ safe_total = array _ Sum ($ arr); // if the total amount of red packets is greater than or equal to the total amount to be issued, it indicates that the red packet has been generated if ($ safe_total> = $ money) {// prevent the last occurrence of the same data, so let's randomly sort shuffle ($ arr) here;/* there is an exception, you can print the correct value but cannot return to the outermost side. it should be a recursion problem */p ($ arr); return $ arr ;} else {// randomly generated red packet amount $ rand = mt_rand ($ min, $ max ); // subtract the money sent from the current money-minus the money to be sent this time $ zx = $ money-$ safe_total-$ rand-($ num-1) * $ min; $ zd = $ money-$ safe_total-$ rand-($ num-1) * $ max; if ($ zx> = 0 & $ zd <= 0) {// You can execute $ num --; $ arr [] = $ rand; $ xrr = $ arr; hongbao ($ money, $ min, $ max, $ num, $ arr, 'no');} else {// indicates that the next time the red packet is sent, it cannot be modified. Hongbao ($ money, $ min, $ max, $ num, $ arr, 'no') ;}}// you can send a red packet of 80 RMB. the minimum value is 1.3 RMB and the maximum value is 2, the number is 50 $ myarr = hongbao (80, 1.3, 2, 50); // Print myarr and check whether the assignment is correct. the function has already printed it once, so it is normal to print the array twice ($ myarr );

I changed it as follows:


  ". Print_r ($ arr, true )."
";}// Function of sending a red packet: luckymoney ($ money, $ min, $ max, $ num, $ arr = array (), $ first = 'yes ') {if ($ first = 'yes') {// because the red packet is divided into units, the unit is converted first. 1 RMB = 100 points, however, you only need to convert $ money = $ money * 100; $ min = $ min * 100; $ max = $ max * 100; // check the validity of the maximum and minimum values of red packets, prevent sending endless loops if ($ money-$ min * $ num <0) {return "the amount of your red packet is too small to be sent to so many people ";} else {if ($ money-$ max * $ num> 0) {return "the amount of money you pay for a red envelope is too large ";}}} // calculate the total amount of generated red packets $ safe_total = ar Ray_sum ($ arr); // if the total amount of red packets is greater than or equal to the total amount to be issued, the red packet has been generated if ($ num = 1) {// if ($ safe_total> = $ money) {// prevents the last data from being the same, so let's sort $ arr [] = $ money-$ safe_total; shuffle ($ arr);/* there is an exception, you can print the correct value but cannot return to the outermost side. it should be a recursion problem * // p ($ arr); return $ arr ;} else {// randomly generated red packet amount $ rand = mt_rand ($ min, $ max ); // subtract the money sent from the current money-minus the money to be sent this time $ zx = $ money-$ safe_total-$ rand-($ num-1) * $ min; $ zd = $ money-$ safe_total -$ Rand-($ num-1) * $ max; if ($ zx> = 0 & $ zd <= 0) {// you can execute $ num -- to specify the condition --; $ arr [] = $ rand; // $ xrr = $ arr; return luckymoney ($ money, $ min, $ max, $ num, $ arr, 'no ');} else {// indicates that the next time the red packet is sent, it cannot be continued. // Luckymoney ($ money, $ min, $ max, $ num, $ arr, 'no'); return luckymoney ($ money, $ min, $ max, $ num, $ arr, 'no'); // p ($ arr); // $ arr [] = $ rand; // shuffle ($ arr ); // return $ arr ;}}// send a red packet of 80 RMB. the minimum value is 1.3 RMB, the maximum value is 2, and the number is 50. // $ myarr = luckymoney (5, 1.3, 2, 3); $ myarr = luckymoney (80, 1.3, 2, 50); // Print myarr and check whether the assignment is correct. the function has already printed it once, therefore, it is normal to print the array twice ($ myarr); $ array_sum = array_sum ($ myarr); p ($ array_sum );

Note that because you use recursion and random luck, it is easy to stack overflow,

I tested your data luckymoney (80, 1.3, 2, 50) five times and probably succeeded twice. we suggest you change the algorithm .....

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

Thank you! I have used the for loop again to improve the algorithm.
It is calculated based on a minimum of 200 RMB, a minimum of 1.3 RMB, and a maximum of 3 million red packets.

With the for loop algorithm, the average execution time of the function is 100 seconds.
With recursive algorithms, the function is executed 100 times, with an average execution time of 0.211 seconds.

The efficiency is 52.75 times different. thanks for the suggestions from @ aristotll.

Below is the for loop code. if you have better ideas, please give me some guidance. thank you.


  ". Print_r ($ arr, true )."
";}// Function of sending a red packet: hongbao ($ money, $ min, $ max, $ num) {$ arr = array (); // because the red packet is divided into units, the unit is first converted, 1 yuan = 100 points, but only one conversion $ money = $ money * 100; $ min = $ min * 100; $ max = $ max * 100; // check the validity of the maximum and minimum values of red packet distribution to prevent endless sending if ($ money-$ min * $ num <0) {return "the amount of your red packet is too small to be sent to so many people";} else {if ($ money-$ max * $ num> 0) {return "the amount of money you pay for a red packet is too large for these people to receive"; }}$ tempnum = $ num; for ($ I = 0; $ I <$ tempnum; $ I ++) {$ flag = 'no'; do {// Generate a random red envelope $ 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.