侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP切割整数工具类似微信红包金额分配的思路详解

微信(小程序)  /  管理员 发布于 7年前   185

 Composer地址:https://packagist.org/packages/werbenhu/php-number-slicing

GitHub地址:https://github.com/werbenhu/php-number-slicing

主要代码:NumberSlicing.php

思路:将数字按精度放大倍数,比如切割数字1,切割的份数是10,精度是0.01,则将1放大100 X 10倍,然后再来对加了1000倍权重后的值进行切割。切割完成之后,再将权重去除,保证总值是1。

 1) {   throw new Exception('precision can\'t bigger than 1!');  }  if ($min < $precision) {   throw new Exception('precision can\'t bigger than min!');  }  $weight_items = [];  $items = [];  //不加权重情况下,每一份的平均值  $each_weight = intval($number / $size);  if ($precision < 1) {   //如果精度是小数   if ($each_weight > 1) {    //如果平均值大于1,则最小额度则直接用min就可以了    //每一份的平均值乘以权重的值,比如精度为0.01,则每一份的平均值要乘以权重(100)    $each_weight = intval((1 / $precision) * $number / $size);    //最小数值也要乘以权重    $min_weight = intval(1 / $precision) * $min;   } else {    //如果平均值小于1,需要将平均值也乘以权重    $each_weight = intval(1 / $precision);    $min_weight = $each_weight * $size * $min / $number;   }   $precision_num = log10(1 / $precision);  } else {   //如果精度是整数(1)   $min_weight = $min;   $precision_num = 0;  }  $sum_item_number = 0.0;  $sum_weight = 0.0;  //先将整个数,随机按最小额度分配  for ($i = 0; $i < $size; $i++) {   $cur_weight = self::weightSlicing($weight_items, $size, $each_weight, $min_weight);   //将权重去除,换算回原先的比例   $rate = ($number * $cur_weight * 1.00) / ($size * $each_weight);   $rate = self::floorWithPrecision($rate, $precision_num);   $sum_item_number += $rate;   $sum_weight += $cur_weight;   $items[] = $rate;  }  //由于误差,随机分配后,还会遗留一些数没有完全分配完,则将剩下的数随机分配  if ($precision_num != 0) {   //如果是切割成小数   $rest = $number - $sum_item_number;   while ($rest - 0.00 > PHP_FLOAT_MIN) {    if ($rest / $min >= 1.0) {     //剩余的数大于min最小额度,则将每份最小额度随机分配     $random_index = mt_rand(0, $size - 1);     $items[$random_index] = self::roundWithPrecision($items[$random_index] + $min, $precision_num);     $sum_item_number = self::roundWithPrecision($sum_item_number + $min, $precision_num);     $rest = self::roundWithPrecision($number - $sum_item_number, $precision_num);    } else {     //剩余的数小于min最小额度,则将这最后的未分配的数随机分配     $random_index = mt_rand(0, $size - 1);     $items[$random_index] = self::roundWithPrecision($items[$random_index] + $number - $sum_item_number, $precision_num);     $sum_item_number = $number;     $rest = $number - $sum_item_number;    }   }  } else {   //如果是切割成整数   $rest = $number - $sum_item_number;   while ($rest > 0) {    if ($rest / $min >= 1) {     $random_index = mt_rand(0, $size - 1);     $items[$random_index] += $min;     $sum_item_number += $min;     $rest = $number - $sum_item_number;    } else {     $random_index = mt_rand(0, $size - 1);     $items[$random_index] += $rest;     $sum_item_number += $rest;     $rest = $number - $sum_item_number;    }   }  }  return $items; }}

  测试代码:

use Werben\Tools\NumberSlicing; function testIntSlicing2IntOne() { $precision = 1; //精确度 eg: 1, 0.1, 0.01, 0.01 $size = 10;   //切割的份数,the size of the number to slicing $min = 3;  //最小额度,最小额度必须大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008 $number = 100;  //要切割的数字,the number $items = NumberSlicing::numberSlicing($number, $size, $precision, $min); $sum = 0.0; $ret_min = $number; foreach ($items as $value) {  $sum += $value;  if ($ret_min > $value) {   $ret_min = $value;  } } $count = count($items); echo "count: $count, sum: $sum, ret_min: $ret_min\n"; echo "items : ". json_encode($items) ."\n";}function testIntSlicing2IntTwo() { $precision = 1; //精确度 eg: 1, 0.1, 0.01, 0.01 $size = 30;   //切割的份数,the size of the number to slicing $min = 18666;  //最小额度,最小额度必须大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008 $number = 800000;  //要切割的数字,the number $items = NumberSlicing::numberSlicing($number, $size, $precision, $min); $sum = 0.0; $ret_min = $number; foreach ($items as $value) {  $sum += $value;  if ($ret_min > $value) {   $ret_min = $value;  } } $count = count($items); echo "count: $count, sum: $sum, ret_min: $ret_min\n"; echo "items : ". json_encode($items) ."\n";}function testIntSlicing2FloatOne() { $precision = 0.01; //精确度 eg: 1, 0.1, 0.01, 0.01 $size = 1000;   //切割的份数,the size of the number to slicing $min = 0.05;  //最小额度,最小额度必须大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008 $number = 100;  //要切割的数字,the number $items = NumberSlicing::numberSlicing($number, $size, $precision, $min); $sum = 0.0; $ret_min = $number; foreach ($items as $key => $value) {  $sum += $value;  if ($ret_min > $value) {   $ret_min = $value;  } } $count = count($items); echo "count: $count, sum: $sum, ret_min: $ret_min\n"; echo "items: ". json_encode($items) ."\n";}function testIntSlicing2FloatTwo() { $precision = 0.00001; //精确度 eg: 1, 0.1, 0.01, 0.01 $size = 1000;   //切割的份数,the size of the number to slicing $min = 0.00005;  //最小额度,最小额度必须大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008 $number = 5;  //要切割的数字,the number $items = NumberSlicing::numberSlicing($number, $size, $precision, $min); $sum = 0.0; $ret_min = $number; foreach ($items as $key => $value) {  $sum += $value;  if ($ret_min > $value) {   $ret_min = $value;  } } $count = count($items); echo "count: $count, sum: $sum, ret_min: $ret_min\n"; echo "items: ". json_encode($items) ."\n";}

总结

以上所述是小编给大家介绍的PHP切割整数工具类似微信红包金额分配的思路详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

您可能感兴趣的文章:

  • php仿微信红包分配算法的实现方法
  • PHP 中使用explode()函数切割字符串为数组的示例
  • PHP搭建大文件切割分块上传功能示例
  • php切割页面div内容的实现代码分享


  • 上一条:
    通过微信公众平台获取公众号文章的方法示例
    下一条:
    php实现QQ小程序发送模板消息功能
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客