zoukankan      html  css  js  c++  java
  • 一个小玩意 PHP实现微信红包金额拆分试玩

    <meta charset="utf-8">
    <?php
    // 新年红包金额拆分试玩
    
    class CBonus
    {
        public $bonus;//红包
        public $bonus_num;//红包个数
        public $bonus_money;//红包总金额
        public $money_single_max;//单个红包限额
        
        public function __construct(){
            $this->bonus_num = 10;
            $this->bonus_money = 200;
            $this->money_single_max = 60;
        }
    
        private function randomFloat($min = 0, $max = 1) {
            $mt_rand = mt_rand();
            $mt_getrandmax = mt_getrandmax();
            echo 'mt_rand=' . $mt_rand . ', mt_getrandmax=' . $mt_getrandmax . '<hr/>';
            return $min + $mt_rand / $mt_getrandmax * ($max - $min);
        }
        //计算
        public function compute()
        {
            $this->bonus = array();
            $bonus_money_temp = $this->bonus_money;
            $money_single_max = $this->money_single_max;
            $i = 1;
            while($i < $this->bonus_num)
            {
                if ($money_single_max > $bonus_money_temp)
                {
                    $money_single_max = floatval(sprintf("%01.2f", $bonus_money_temp / 2));//剩余金额不够分时,把剩余金额的一半作为备用金
                }
                $bonus_money_rad = $this->randomFloat(0.01, $money_single_max);//一个红包随机金额 最小的1分钱
                $bonus_money_rad = floatval(sprintf("%01.2f", $bonus_money_rad));
                $bonus_money_temp = $bonus_money_temp - $bonus_money_rad ;//待分配的总剩余金额
                $bonus_money_temp = floatval(sprintf("%01.2f", $bonus_money_temp));
                $this->bonus[] = $bonus_money_rad;
                //echo $bonus_money_rad . ',' . $bonus_money_temp . '<hr/>';
                $i++;
            }
            $this->bonus[] = $bonus_money_temp;//分配剩余金额给最后一个红包
        }
        //打印
        public function output(){
            $total = 0;
            foreach($this->bonus as $k => $v)
            {
                echo '红包' . ($k+1) . '=' . $v . '<br/>';
                $total += $v;
            }
            echo '红包总金额:'.$total;
        }
    }
    
    $CBonus = new CBonus();
    $CBonus->compute();
    $CBonus->output();
    ?>

    演示结果:

    红包1=12.36
    红包2=24.37
    红包3=42.71
    红包4=36.92
    红包5=25.84
    红包6=23.17
    红包7=15.92
    红包8=1.35
    红包9=7.75
    红包10=9.61
    红包总金额:200

    红包1=24.59
    红包2=17.66
    红包3=29.67
    红包4=32.34
    红包5=12.67
    红包6=37.15
    红包7=17.41
    红包8=15.23
    红包9=6.13
    红包10=7.15
    红包总金额:200

  • 相关阅读:
    UVALive 7141 BombX
    CodeForces 722D Generating Sets
    CodeForces 722C Destroying Array
    CodeForces 721D Maxim and Array
    CodeForces 721C Journey
    CodeForces 415D Mashmokh and ACM
    CodeForces 718C Sasha and Array
    CodeForces 635C XOR Equation
    CodeForces 631D Messenger
    田忌赛马问题
  • 原文地址:https://www.cnblogs.com/imbin/p/3536276.html
Copyright © 2011-2022 走看看