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. 支持utf8的str_split函数

    <?php header("Content-type: text/html; charset=utf-8"); /** * 按字节数对字符串进行分片 * @param $st ...

  2. 面试题集锦---BY算法导论小组

    3.7题 3.21题 1.给定能随机生成整数 1 到 5 的函数,写出能随机生成整数 1 到 7 的函数. 提示:两个random就可以有25种可能,每种可能都是等概率的 2.判断一个自然数是否是某个 ...

  3. NSCTF 2017-pwn2

    目录 程序基本信息 程序漏洞 整体思路 exp脚本 发现的问题 内容参考 程序基本信息 32位动态链接程序,开启了数据段不可执行以及栈溢出保护 程序漏洞 在函数中sub_80487fa中有一个格式化字 ...

  4. Git git rm和git rm --cached

    git rm 和 git rm --cached 的区别 git rm file git commit -m "xxx" git push origin master 删除本地及仓 ...

  5. Mininet系列实验(六):Mininet动态改变转发规则实验

    一. 实验目的 熟悉Mininet自定义拓扑脚本的编写:熟悉编写POX脚本动态改变转发规则 二.实验原理 在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为.在本实验中,基于Mi ...

  6. LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)

    题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums ...

  7. Linux中工作目录切换命令

    1.pwd命令用于显示当前的工作目录 2.cd命令用于切换工作路径,格式为:cd [目录名称] 参数 作用 - 切换到上一次的 目录,如:cd - ~ 切换到”家目录“,如:cd ~ ~usernam ...

  8. python 了解一点属性的延迟计算

    写在前面 本以为百度搜索这类知识的文章应该有很多, 然后我看了前面几篇后,基本上都是类似的内容,我想找些与众不同的博客看下,来拖宽这方面的广度,我就随机点到了第10页,结果第10页的内容基本跟属性的延 ...

  9. YApi内部部署文档

    旨在为开发.产品.测试人员提供更优雅的接口管理服务.可以帮助开发者轻松创建.发布.维护 API 1.安装Node.js环境(7.6+) 1.官网下载适合的nodejs版本放置在/usr/package ...

  10. NULL和nullptr

    NULL就是0 nullptr是空指针[c++11]