一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

PHP实现微信红包金额拆分试玩的算法示例

时间:2018-04-08 编辑:猪哥 来源:一聚教程网

本文介绍了PHP实现微信红包金额拆分试玩的算法示例,分享给大家,有兴趣的可以看一下:

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 . '
'; 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 . '
'; $i++; } $this->bonus[] = $bonus_money_temp;//分配剩余金额给最后一个红包 } //打印 public function output(){ $total = 0; foreach($this->bonus as $k => $v) { echo '红包' . ($k+1) . '=' . $v . '
'; $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

热门栏目