最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP切割整數(shù)工具類似微信紅包金額分配的思路詳解

 更新時間:2019年09月18日 11:32:08   作者:werben  
這篇文章主要介紹了 PHP切割整數(shù)工具類似微信紅包金額分配的思路詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下

 Composer地址:https://packagist.org/packages/werbenhu/php-number-slicing

GitHub地址:https://github.com/werbenhu/php-number-slicing

主要代碼:NumberSlicing.php

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

<?php
namespace Werben\Tools;
use Exception;
class NumberSlicing {
 /**
  * 精確小數(shù)點,舍棄最后一位之后的數(shù)據(jù)(非四舍五入)
  * floor with precision
  * @param $number 要精確的數(shù)
  * @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;
 }
 /**
  * 精確小數(shù)點,按四舍五入保留最后一位
  * round with precision
  * @param $number 要精確的數(shù)
  * @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;
 }
 /**
  * 將數(shù)把權(quán)重放大,比如1,要按精度0.0001分配,則先將1乘以10000然后再來分配
  * random the sum weights 加上權(quán)重之后,整個要切割的數(shù)的權(quán)重總值
  * @param $weight_items 用來保留,隨機(jī)分配的權(quán)重值
  * @param $count 要切割的份數(shù)
  * @param int $each_weight 加上權(quán)重之后,每一份平均的權(quán)重值
  * @param int $min_weight 加上權(quán)重之后,最小額度的值
  * @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 = [];
  //不加權(quán)重情況下,每一份的平均值
  $each_weight = intval($number / $size);
  if ($precision < 1) {
   //如果精度是小數(shù)
   if ($each_weight > 1) {
    //如果平均值大于1,則最小額度則直接用min就可以了
    //每一份的平均值乘以權(quán)重的值,比如精度為0.01,則每一份的平均值要乘以權(quán)重(100)
    $each_weight = intval((1 / $precision) * $number / $size);
    //最小數(shù)值也要乘以權(quán)重
    $min_weight = intval(1 / $precision) * $min;
   } else {
    //如果平均值小于1,需要將平均值也乘以權(quán)重
    $each_weight = intval(1 / $precision);
    $min_weight = $each_weight * $size * $min / $number;
   }
   $precision_num = log10(1 / $precision);
  } else {
   //如果精度是整數(shù)(1)
   $min_weight = $min;
   $precision_num = 0;
  }
  $sum_item_number = 0.0;
  $sum_weight = 0.0;
  //先將整個數(shù),隨機(jī)按最小額度分配
  for ($i = 0; $i < $size; $i++) {
   $cur_weight = self::weightSlicing($weight_items, $size, $each_weight, $min_weight);
   //將權(quán)重去除,換算回原先的比例
   $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;
  }
  //由于誤差,隨機(jī)分配后,還會遺留一些數(shù)沒有完全分配完,則將剩下的數(shù)隨機(jī)分配
  if ($precision_num != 0) {
   //如果是切割成小數(shù)
   $rest = $number - $sum_item_number;
   while ($rest - 0.00 > PHP_FLOAT_MIN) {
    if ($rest / $min >= 1.0) {
     //剩余的數(shù)大于min最小額度,則將每份最小額度隨機(jī)分配
     $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 {
     //剩余的數(shù)小于min最小額度,則將這最后的未分配的數(shù)隨機(jī)分配
     $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 {
   //如果是切割成整數(shù)
   $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;   //切割的份數(shù),the size of the number to slicing
 $min = 3;  //最小額度,最小額度必須大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008
 $number = 100;  //要切割的數(shù)字,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;   //切割的份數(shù),the size of the number to slicing
 $min = 18666;  //最小額度,最小額度必須大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008
 $number = 800000;  //要切割的數(shù)字,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;   //切割的份數(shù),the size of the number to slicing
 $min = 0.05;  //最小額度,最小額度必須大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008
 $number = 100;  //要切割的數(shù)字,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;   //切割的份數(shù),the size of the number to slicing
 $min = 0.00005;  //最小額度,最小額度必須大于最小精度,min amount eg: 3, 0.23, 0.05, 0.008
 $number = 5;  //要切割的數(shù)字,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";
}

總結(jié)

以上所述是小編給大家介紹的PHP切割整數(shù)工具類似微信紅包金額分配的思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • PHP設(shè)計模式(觀察者模式)

    PHP設(shè)計模式(觀察者模式)

    這篇文章主要介紹了PHP設(shè)計模式(觀察者模式),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • PHP實現(xiàn)數(shù)據(jù)分頁顯示的簡單實例

    PHP實現(xiàn)數(shù)據(jù)分頁顯示的簡單實例

    下面小編就為大家?guī)硪黄狿HP實現(xiàn)數(shù)據(jù)分頁顯示的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給的大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • ThinkPHP模板自定義標(biāo)簽使用方法

    ThinkPHP模板自定義標(biāo)簽使用方法

    這篇文章主要介紹了ThinkPHP模板自定義標(biāo)簽使用方法,需要的朋友可以參考下
    2014-06-06
  • Laravel?Swagger?使用超詳細(xì)教程

    Laravel?Swagger?使用超詳細(xì)教程

    Swagger?是一個基于?Open?Api?規(guī)范的?API?管理工具,通過項目注解的形式自動構(gòu)建?API?文檔,擁有在線調(diào)試的功能,這篇文章主要介紹了Laravel?Swagger?使用完整教程,需要的朋友可以參考下
    2023-09-09
  • PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解決辦法

    PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解決辦法

    這篇文章主要介紹了PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解決辦法,需要的朋友可以參考下
    2014-05-05
  • 使用ThinkPHP+Uploadify實現(xiàn)圖片上傳功能

    使用ThinkPHP+Uploadify實現(xiàn)圖片上傳功能

    這篇文章主要介紹了使用ThinkPHP+Uploadify實現(xiàn)圖片上傳功能,需要的朋友可以參考下
    2014-06-06
  • PHP命令空間namespace及use的用法小結(jié)

    PHP命令空間namespace及use的用法小結(jié)

    命名空間一個最明確的目的就是解決重名問題,PHP中不允許兩個函數(shù)或者類出現(xiàn)相同的名字,否則會產(chǎn)生一個致命的錯誤。這篇文章主要介紹了PHP命令空間namespace及use的用法實踐總結(jié),需要的朋友可以參考下
    2017-11-11
  • Php中使用Select 查詢語句的實例

    Php中使用Select 查詢語句的實例

    php中要查詢mysql數(shù)據(jù)庫中的內(nèi)容我們必須先連接mysql數(shù)據(jù)庫,然后再利用sql語句進(jìn)行查詢,下面我們來看一些例子吧
    2014-02-02
  • Laravel學(xué)習(xí)教程之本地化模塊

    Laravel學(xué)習(xí)教程之本地化模塊

    這篇文章主要給大家介紹了關(guān)于Laravel學(xué)習(xí)教程之本地化模塊的相關(guān)資料,文中通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • Laravel 5.5 異常處理 & 錯誤日志的解決

    Laravel 5.5 異常處理 & 錯誤日志的解決

    今天小編就為大家分享一篇Laravel 5.5 異常處理 & 錯誤日志的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評論

太保市| 邹平县| 竹溪县| 扎鲁特旗| 县级市| 富裕县| 万年县| 宝山区| 密山市| 济南市| 韶关市| 阿坝县| 万载县| 抚宁县| 勃利县| 万州区| 南丰县| 静乐县| 新巴尔虎左旗| 崇信县| 翁牛特旗| 额济纳旗| 松江区| 祁门县| 东丽区| 秦安县| 岳阳市| 裕民县| 望谟县| 铜梁县| 太仆寺旗| 水富县| 习水县| 南丰县| 大田县| 海阳市| 多伦县| 绿春县| 南涧| 兴仁县| 长岛县|