Analysis of WeChat red packet algorithm implemented by php (unofficial)

Source: Internet
Author: User
This article mainly introduces the red envelope algorithm implemented by php, and analyzes the random algorithm techniques related to the popular red envelopes in the form of examples, which has some reference value, if you need it, refer to the example in this article to describe the red packet algorithm implemented by php. Share it with you for your reference. The details are as follows:

You have been using the red envelope feature in the group recently. There are two types of red envelopes:

1. regular red packets

2. lucky red packets

Ordinary red envelopes do not need to be parsed. The principle of big pot meals is equally divided.

A lucky red packet is about luck. some people can grab a lot of money, and some others are less pitiful. of course, they don't have to grab a lot first. In the end, they are 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 ;}

Let's try it now.

// 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 ));

Another version that uses arrays has the following principles:

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;}

I hope this article will help you with php programming.

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.