设为首页 加入收藏

TOP

PHP切割整数工具,类似微信红包金额分配(一)
2019-09-18 11:10:53 】 浏览:110
Tags: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_
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[Linux] 编写Dockerfile文件自动.. 下一篇[PHP] 持续交付Jenkins安装

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目