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切割整数工具,类似微信红包金额分配的更多相关文章

  1. 如何用 js 实现一个类似微信红包的随机算法

    如何用 js 实现一个类似微信红包的随机算法 js, 微信红包, 随机算法 "use strict"; /** * * @author xgqfrms * @license MIT ...

  2. 一个小玩意 PHP实现微信红包金额拆分试玩

    <meta charset="utf-8"> <?php // 新年红包金额拆分试玩 class CBonus { public $bonus;//红包 publ ...

  3. PHP实现微信随机红包算法和微信红包的架构设计简介

    微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...

  4. PHP实现微信红包算法和微信红包的架构设计简介

    微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...

  5. 微信红包中使用的技术:AA收款+随机算法

    除夕夜你领到红包了吗?有的说“我领了好几K!”“我领了几W!” 土豪何其多,苦逼也不少!有的说“我出来工作了,没压岁钱了,还要发红包”.那您有去抢微信红包吗?微信群中抢“新年红包”春节爆红.618微信 ...

  6. 第三方app抽奖发送微信红包

    1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...

  7. 第三方app抽奖发送微信红包实现

    1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...

  8. java 微信红包算法代码实现及架构设计

    转载至:https://www.zybuluo.com/yulin718/note/93148 微信红包的架构设计简介 架构 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈 ...

  9. 基于微信红包插件的原理实现android任何APP自动发送评论(已开源)

    背景 地址:https://github.com/huijimuhe/postman 核心就是android的AccessibilityService,回复功能api需要23以上版本才行. 其实很像在 ...

随机推荐

  1. base/7/x86_64/filelists_db FAILED

      解决办法: [root@localhost ~]# cd /var/lib/rpm [root@localhost rpm]# rm -rf __db.* # 清除原 rpmdb 文件 [root ...

  2. 全国行政区域代码(免费来拿) xls格式 可直接导入

    全部区域代码 地址:https://pan.baidu.com/s/1Elz-zW_nLS8YR8GZAn3WRw 提取码:ze3s

  3. Java学习日记——基础篇(二)基本语法

    变量 变量和常量是程序处理的两种基本数据对象,变量是程序的基本组成单位 变量的目的就是确定目标并提供存放空间 public class Hello { public static void main( ...

  4. MovingWallVelocity边界条件【翻译】

    翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-solving/59646-movingwallvelocity-bound ...

  5. ICEM-缺角正方体(2D转3D)

    原视频下载地址:https://yunpan.cn/cqKYEeSsQhJHt  访问密码 1cce

  6. Java 面向对象(五)

    包装类 什么是包装类 在面向对象当中"一切皆对象".基本数据类型变量不是对象,比如 int a = 10; 当中就没有对象,所以就很矛盾.此时我们就可以对基本数据类型进行包装,把基 ...

  7. python何时用list,dict,set

    从读取的角度来讲: 看是用来随机读取(查询)还是连续读取. list数组集中存放,连续读取效率高(具体还没测试,理论上应该如此). dict散列表,使用hash计算存放的位置,随机读取效率高. 随机读 ...

  8. java Calendar 小时值得到24进制格式

    Calendar cal = Calendar.getInstance(); cal.get(Calendar.HOUR_OF_DAY)

  9. PowerDesigner 使用小结

    这里总结一篇关于数据建模工具 PowerDesigner 的使用小技巧,下面列出的两个应用场景要在网上现找解决方案的话还真不一定好找,所以选择将这两个棘手的问题先记下来. 1. PDM 中表间关系出现 ...

  10. SpringBoot视图层技术

    一.SpringBoot整合jsp 在maven的dependencies的依赖中除了springBoot启动器还要添加对jstl和jsp的依赖. <dependencies> <d ...