PHP generates WeChat red envelopes

Source: Internet
Author: User
/** * @param $total [total amount of red packets you want to send] * @param int $num [send several] * @return array[generate red envelope amount] */function getredgift ($total, $num = ten) {
  $min = 0.01;    $wamp = Array ();    $returnData = Array ();    for ($i = 1; $i < $num, + + $i) {        $safe _total = ($total-($num-$i) * $min)/($num-$i);        _total < 0) break;        $money = @mt_rand ($min *, $safe _total * 100)/100;//randomly generates a red envelope amount        $total = $total-$money;//The total amount of the remaining red packets        $wamp [$i] = Round ($money, 2);//reserved two valid digits    }    $wamp [$i] = Round ($total, 2);    $returnData [' moneysum '] = $wamp;    $returnData [' newtotal '] = array_sum ($wamp);    return $returnData;} Test $data = Getredgift (1),;p Rint_r ($data),//result:/*array ([+] = 8.7 [2] = 10.09 [3] = = 6.23 [4] = = 6. [5] = = 0.47 [6] = 3.12 [7] = 7.52 [8] = = 12.21 [9] = 20.53 [ten] = 24.26) */
  • Related Article

    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.