Php implements the program algorithm of WeChat red packets

Source: Internet
Author: User
Next, let's take a look at a php program algorithm that we often encounter in our daily lives. I hope this program will be helpful to all of you. Recently, I have been experiencing the red packet function in the group, there are red packet types. • ordinary red envelopes • fighting for two common red envelopes... next, let's take a look at a php program algorithm that we often encounter in our daily lives. I hope this program will be helpful to all of you. Recently, I have been experiencing the red packet function in the group, there are red packet types.

• Common red envelopes • Two types of lucky red envelopes

Ordinary red envelopes do not need to be parsed much. The principle of big pot meals is equal to that of big pot meals. it is about luck. some people can grab a lot, while others can get a lot of money, of course, there must be more than a first grab. In the end, it is random.

I thought about whether I could implement similar functions (not to mention algorithms) by writing and writing on my own ).

// $ Bonus_total total amount of red packets/$ bonus_count number of red packets/$ bonus_type red packet type 1 = empty red packets 0 = normal red packet function randBonus ($ bonus_total = 0, $ bonus_count = 3, $ bonus_type = 1) {$ bonus_items = array (); // results to be divided $ bonus_balance = $ bonus_total; // balance after each score $ bonus_avg = number_format ($ bonus_total/$ bonus_count, 2); // average money for each red packet $ I = 0; while ($ I <$ bonus_count) {if ($ I <$ bonus_count-1) {$ rand = $ bonus_type? (Rand (1, $ bonus_balance * 100-1)/100): $ bonus_avg; // calculate the current red envelope amount based on the red envelope type $ bonus_items [] = $ rand; $ bonus_balance-= $ rand;} else {$ bonus_items [] = $ bonus_balance; // contract all the final amounts for the last red packet, ensure that the total amount sent is correct} $ I ++;} return $ bonus_items ;}

Now let's try it out:

// Send 3 lucky red packets, totaling $100 $ bonus_items = randBonus (100, 3, 1); // view the generated red packet var_dump ($ bonus_items ); // check whether the total amount is correct. check if there is any trouble with var_dump (array_sum ($ bonus_items). The other version uses the array implementation. The principle is similar: function sendRandBonus ($ total = 0, $ count = 3, $ type = 1) {if ($ type = 1) {$ input = range (0.01, $ total, 0.01); if ($ count> 1) {$ rand_keys = (array) array_rand ($ input, $ count-1); $ last = 0; foreach ($ rand_keys as $ I => $ key) {$ current = $ input [$ Key]-$ last; $ items [] = $ current; $ last = $ input [$ key];} $ items [] = $ total-array_sum ($ items);} else {$ avg = number_format ($ total/$ count, 2); $ I = 0; while ($ I <$ count) {$ items [] = $ I <$ count-1? $ Avg :( $ total-array_sum ($ items); $ I ++;} return $ items ;}


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.