日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php数组10000分割1000_PHP切割整数工具类似微信红包金额分配的思路详解

發布時間:2025/3/15 php 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php数组10000分割1000_PHP切割整数工具类似微信红包金额分配的思路详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要代碼:NumberSlicing.php

思路:將數字按精度放大倍數,比如切割數字1,切割的份數是10,精度是0.01,則將1放大100 X 10倍,然后再來對加了1000倍權重后的值進行切割。切割完成之后,再將權重去除,保證總值是1。

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切割整數工具類似微信紅包金額分配的思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

總結

以上是生活随笔為你收集整理的php数组10000分割1000_PHP切割整数工具类似微信红包金额分配的思路详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。