PHP切割整数工具,类似微信红包金额分配
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切割整数工具,类似微信红包金额分配的更多相关文章
- 如何用 js 实现一个类似微信红包的随机算法
如何用 js 实现一个类似微信红包的随机算法 js, 微信红包, 随机算法 "use strict"; /** * * @author xgqfrms * @license MIT ...
- 一个小玩意 PHP实现微信红包金额拆分试玩
<meta charset="utf-8"> <?php // 新年红包金额拆分试玩 class CBonus { public $bonus;//红包 publ ...
- PHP实现微信随机红包算法和微信红包的架构设计简介
微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...
- PHP实现微信红包算法和微信红包的架构设计简介
微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...
- 微信红包中使用的技术:AA收款+随机算法
除夕夜你领到红包了吗?有的说“我领了好几K!”“我领了几W!” 土豪何其多,苦逼也不少!有的说“我出来工作了,没压岁钱了,还要发红包”.那您有去抢微信红包吗?微信群中抢“新年红包”春节爆红.618微信 ...
- 第三方app抽奖发送微信红包
1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...
- 第三方app抽奖发送微信红包实现
1.控制器方法: private string SendRedPackge(string OpenId, int Amount, string LuckyCode) { Models.PayWeiXi ...
- java 微信红包算法代码实现及架构设计
转载至:https://www.zybuluo.com/yulin718/note/93148 微信红包的架构设计简介 架构 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈 ...
- 基于微信红包插件的原理实现android任何APP自动发送评论(已开源)
背景 地址:https://github.com/huijimuhe/postman 核心就是android的AccessibilityService,回复功能api需要23以上版本才行. 其实很像在 ...
随机推荐
- for循环实战性能优化
完成同样的功能,用不同的代码来实现,性能上可能会有比较大的差别,所以对于一些性能敏感的模块来说,对代码进行一定的优化还是很有必要的.今天就来说一下java代码优化的事情,今天主要聊一下对于for(wh ...
- 小程序 之修改radio默认样式
一.效果图 二.代码 /* 选中后的 背景样式 (红色背景 无边框 可根据UI需求自己修改) */ radio .wx-radio-input.wx-radio-input-checked { bor ...
- 配置mysql远程访问
参考: https://www.cnblogs.com/sanduzxcvbnm/p/9789236.html
- PyTorch Tutorials 5 数据并行(选读)
%matplotlib inline 数据并行(选读) Authors: Sung Kim and Jenny Kang 在这个教程里,我们将学习如何使用 DataParallel 来使用多GPU. ...
- 打印变量地址-0x%08x
地址是8个16进制数. 1.8个16进制数:相当于32个二进制数.4G内存刚好可以用32位的二进制表示出来.2.因为变量或函数等等在运行时都是存储在内存中的,所以你用取地址符当然是取出计算机内存中的地 ...
- 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_12-用户退出-服务端
实现退出 用户退出要以下动作: 1.删除redis中的token 2.删除cookie中的token controller内定义 spring securety config内放行 对这个url放行 ...
- Day5作业,商城+ATM机+后台管理
晚来了....东西太多,需要写的blog内容太多,re讲的渣渣,不明白为什么oldboy经常换老师,吐槽下吧,真心不爱了.... github地址在这:https://github.com/ccorz ...
- MySQL性能优化最佳实践20条
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...
- Python 解析式、生成器
标准库datetime datetime模块 对日期.时间.时间戳的处理 datetime类 类方法 today() 返回本地时区当前时间的datetime对象 now(tz=None) 返回当前时间 ...
- [CareerCup] Single Valid Tree
https://www.careercup.com/question?id=5103530547347456 Given a list of nodes, each with a left child ...