• rand值出现负数的解决方案


    当rand($number)或者mt_rand($number)的时候,$number太大,超过pow(2,31) - 1;即整型最大值的时候,会出现负数。

    如果只是单纯转换成整数而已的话,可以采用sprintf('%u', $number)来处理,如果是概率统计,过滤一定的范围的话,可以看看如下解决方案:

     1 public function my_mt_rand($randMaxValue) {
     2         $intMaxValue = pow(2,31) - 1;    //rand函数最大值
     3         $maxValueLen = strlen($randMaxValue);    //最大值的长度
     4         $intMaxValueLen = strlen($intMaxValue);    //rand函数最大值的长度
     5         
     6         /*if($randMaxValue>$intMaxValue){
     7             $randValue = mt_rand(0, $intMaxValue);
     8         }else{
     9             $randValue = mt_rand(0, $randMaxValue);
    10         }
    11         return $randValue;*/
    12         
    13         if ($randMaxValue > $intMaxValue) {
    14             if ($maxValueLen == $intMaxValueLen) {
    15                 //$randValue = mt_rand(0, $intMaxValue);
    16                 $randValue = $this->getIntMaxBetween($randMaxValue, $intMaxValue);
    17             } else {
    18                 $temp_a = mt_rand(0, substr($randMaxValue, -$intMaxValueLen));    //低位
    19                 
    20                 if ($temp_a > $intMaxValue) {
    21                     $temp_a = $this->getIntMaxBetween($temp_a, $intMaxValue);
    22                 }
    23                 
    24                 $temp_b = mt_rand(0, substr($randMaxValue, 0, ($maxValueLen - $intMaxValueLen)));    //高位
    25                 $randValue = $temp_a + $temp_b * pow(10,$intMaxValueLen);
    26             }
    27         }else{
    28             $randValue = mt_rand(0, $randMaxValue);
    29         }
    30         
    31         return $randValue;
    32     }
    33     
    34     public function getIntMaxBetween($a, $b) {
    35         if ($a > $b) {
    36             $divideNumber = intval($a/$b);
    37             $modNumber = intval($a%$b);
    38             
    39             $returnVal = 0;
    40             
    41             //$returnVal = mt_rand(0,$divideNumber) * mt_rand(0,$b);
    42             if ($divideNumber > 10) {
    43                 $returnVal += mt_rand(0,$divideNumber) * mt_rand(0,$b);
    44             } else {
    45                 for($m = 0; $m < $divideNumber; $m++) {
    46                     $returnVal += mt_rand(0,$b);
    47                 }
    48             }
    49             
    50             if ($modNumber > 0) {
    51                 $returnVal += mt_rand(0,$modNumber);
    52             }
    53             
    54             return $returnVal;
    55         } else {
    56             $returnVal = mt_rand(0, $a);
    57         }
    58         
    59         return $returnVal;
    60     }

    调用:$randValue = $this->my_mt_rand($randMaxValue);//获取随机数

  • 相关阅读:
    easyui tree:根据属性格式化树节点名称
    Druid执行多条SQL异常:Cause: java.sql.SQLException: sql injection violation, multi-statement not allow
    springmvc接收jquery提交的数组数据
    jquery easyui:tab自动加载第一个tab内容
    thymeleaf-extras-shiro
    Shiro:授权控制
    thymeleaf : EL1050E The arguments (...) for the constructor call are missing
    (转载)ibatis:解决sql注入问题
    05 Oracle process
    04 memory structure
  • 原文地址:https://www.cnblogs.com/xingmeng/p/3461566.html
Copyright © 2020-2023  润新知