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. lightgbm用于排序

    一. LTR(learning to rank)经常用于搜索排序中,开源工具中比较有名的是微软的ranklib,但是这个好像是单机版的,也有好长时间没有更新了.所以打算想利用lightgbm进行排序, ...

  2. ftp连接

    package enterprise.celerity.ac.util; import java.io.IOException;import java.io.InputStream;import ja ...

  3. CPU占用高系统反应慢之问题定位

    今天在看到公司群里有关于测试反应测试服务器比较卡,调用调用超时,响应很慢,成功率低的问题,然后想着去处理这个问题. 本着开发的精神,摒弃网管的水平,寻找问题的根源. 主要从如下几个方面入手: 1:查询 ...

  4. GO- 使用JSON

    1 json.Marshal  把对象转换为JSON的方法 原型如下 func Marshal(v interface{}) ([]byte, error)这个函数接收任意类型的数据 v,并转换为字节 ...

  5. springMVC_注解方式搭建基础环境

    ---恢复内容开始--- 一.jar包环境,web配置文件和Spring-MVC配置文件的,相关的modelAndview 1.配置DispatcherServlet <servlet>  ...

  6. 解决跨操作系统平台JSON中文乱码问题

    解决跨操作系统平台JSON中文乱码问题 LINUX统一使用utf-8编码,WINDOWS却不是. LINUX中间件,传输JSON给WINDOWS程序,会乱码. 解决办法: 对JSON字段是字符串类型的 ...

  7. libmidas.so.2

    libmidas.so.2 libmidas.so.2文件,使DATASNAP FOR LINUX中间件,支持OleVariant格式的序列,使TDataSetProvider+TClientData ...

  8. 每天一个linux命令:top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...

  9. zblog上传安装主题插件不成功的原因和解决办法

    最近有不少zblog用户反映在后台上传安装主题或者插件的时候出现了问题.本文就来尝试说明下这类问题的原因和解决办法. 首先来说说zblog主题或者插件的安装方法,一共有三种方式: 第一种是直接在网站后 ...

  10. 007-多线程-JUC集合-Queue-BlockingQueue接口以及ArrayBlockingQueue

    0.BlockingQueue接口介绍 BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利 ...