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


Posted in PHP onSeptember 18, 2019

 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。

<?php
namespace Werben\Tools;
use Exception;
class NumberSlicing {
 /**
  * 精确小数点,舍弃最后一位之后的数据(非四舍五入)
  * floor with precision
  * @param $number 要精确的数
  * @param $precision 精度,比如保留到0.01,则该值为2
  * @return float|int
  */
 public static function floorWithPrecision($number, $precision) {
  $power = pow(10, $precision);
  $ret = floor($number * $power) * 1.0 / $power ;
  return $ret;
 }
 /**
  * 精确小数点,按四舍五入保留最后一位
  * round with precision
  * @param $number 要精确的数
  * @param $precision 精度,比如保留到0.01,则该值为2
  * @return float|int
  */
 public static function roundWithPrecision($number, $precision) {
  $power = pow(10, $precision);
  $ret = round($number * $power) * 1.0 / $power ;
  return $ret;
 }
 /**
  * 将数把权重放大,比如1,要按精度0.0001分配,则先将1乘以10000然后再来分配
  * random the sum weights 加上权重之后,整个要切割的数的权重总值
  * @param $weight_items 用来保留,随机分配的权重值
  * @param $count 要切割的份数
  * @param int $each_weight 加上权重之后,每一份平均的权重值
  * @param int $min_weight 加上权重之后,最小额度的值
  * @return float|int
  */
 public static function weightSlicing(&$weight_items, $count, $each_weight = 10, $min_weight = 3)
 {
  $already_count = count($weight_items);
  $cur_random_full_total = ($already_count + 1) * $each_weight;
  $already_random_real_total = 0;
  foreach ($weight_items as $value) {
   $already_random_real_total += $value;
  }
  $cur_random_rest = $cur_random_full_total - $already_random_real_total;
  if ($already_count == $count - 1) {
   $cur_random_rate = $cur_random_rest;
  } else {
   $cur_random_rate_max = $cur_random_rest + $each_weight - $min_weight * 2;
   $cur_random_rate = $min_weight + mt_rand(0, $cur_random_rate_max);
  }
  $weight_items[] = $cur_random_rate;
  return $cur_random_rate;
 }
 /**
  * slicing the number
  * @param int $number
  * @param int $size
  * @param float $precision
  * @param float $min
  * @return array
  * @throws Exception
  */
 public static function numberSlicing($number, $size, $precision = 0.01, $min = 0.01) {
  if ($number * 1.0 / $size <= $min) {
   throw new Exception('min number is bigger than the average value!');
  }
  if ($precision > 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源代码
Aug 21 PHP
php抓取https的内容的代码
Apr 06 PHP
基于curl数据采集之单页面采集函数get_html的使用
Apr 28 PHP
使用PHP实现Mysql读写分离
Jun 28 PHP
php stream_get_meta_data返回值
Sep 29 PHP
php实现xml与json之间的相互转换功能实例
Jul 07 PHP
Docker配置PHP开发环境教程
Dec 21 PHP
php实现的中文分词类完整实例
Feb 06 PHP
php使用PDO获取结果集的方法
Feb 16 PHP
[原创]php正则删除img标签的方法示例
May 27 PHP
PHP基于ORM方式操作MySQL数据库实例
Jun 21 PHP
浅谈PHP进程管理
Mar 08 PHP
php实现多站点共用session实现单点登录的方法详解
Sep 18 #PHP
PHP实现批量修改文件名的方法示例
Sep 18 #PHP
php DES加密算法实例分析
Sep 18 #PHP
php实现QQ小程序发送模板消息功能
Sep 18 #PHP
php文件后缀不强制为.php的实操方法
Sep 18 #PHP
php校验公钥是否可用的实例方法
Sep 17 #PHP
php写入mysql中文乱码的实例解决方法
Sep 17 #PHP
You might like
老生常谈PHP面向对象之解释器模式
2017/05/17 PHP
php 提交表单 关闭layer弹窗iframe的实例讲解
2018/08/20 PHP
jQuery中ajax的post()方法用法实例
2014/12/26 Javascript
使用js画图之圆、弧、扇形
2015/01/12 Javascript
javascript实现瀑布流自适应遇到的问题及解决方案
2015/01/28 Javascript
JavaScript中5种调用函数的方法
2015/03/12 Javascript
js判断图片加载完成后获取图片实际宽高的方法
2016/02/25 Javascript
如何使用AngularJs打造权限管理系统【简易型】
2016/05/09 Javascript
解析预加载显示图片艺术
2016/12/05 Javascript
AngularJS使用angular.bootstrap完成模块手动加载的方法分析
2017/01/19 Javascript
jQuery实现点击关注和取消功能
2017/07/03 jQuery
angularjs实现柱状图动态加载的示例
2017/12/11 Javascript
关于React动态加载路由处理的相关问题
2019/01/07 Javascript
详解微信UnionID作用
2019/05/15 Javascript
vue移动端弹起蒙层滑动禁止底部滑动操作
2020/07/22 Javascript
使用js获取身份证年龄的示例代码
2020/12/11 Javascript
[00:39]DOTA2上海特级锦标赛 Liquid战队宣传片
2016/03/04 DOTA
[00:35]2016完美“圣”典风云人物:冷冷宣传片
2016/12/08 DOTA
python正则表达式之作业计算器
2016/03/18 Python
Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】
2018/07/12 Python
对numpy中数组转置的求解以及向量内积计算方法
2018/10/31 Python
django 外键model的互相读取方法
2018/12/15 Python
python自动脚本的pyautogui入门学习
2020/04/01 Python
Python小白垃圾回收机制入门
2020/06/09 Python
Keras中的两种模型:Sequential和Model用法
2020/06/27 Python
CSS3中的clip-path使用攻略
2015/08/03 HTML / CSS
捷科时代的软件测试笔试题
2015/11/09 面试题
国际贸易专业个人求职信格式
2014/02/02 职场文书
党支部书记岗位责任制
2014/02/11 职场文书
《中华少年》教学反思
2014/02/15 职场文书
学生个人自我鉴定范文
2014/03/28 职场文书
物资采购方案
2014/06/12 职场文书
标准版离职证明书
2014/09/12 职场文书
幼儿园工作总结2015
2015/04/01 职场文书
动画「半妖的夜叉姬」新BD特典图公开
2022/03/22 日漫
Win10 heic文件怎么打开 ? Win10 heic文件打开教程
2022/04/06 数码科技