In PHP, how do I generate a big pseudo-random number?

前端 未结 14 1215
遇见更好的自我
遇见更好的自我 2020-12-03 14:12

I\'m looking for a way to generate a big random number with PHP, something like:

mt_rand($lower, $upper);

The closer I\'ve

14条回答
  •  死守一世寂寞
    2020-12-03 15:09

    Tested and works

    ";
    
    function bigRandomNumber($min,$max) {
        // take the max number length
        $number_length = strlen($max);
    
        // Set the counter
        $i = 1;
    
        // Find the base and the min and max ranges
        // Loop through the min to find the base number
        while ($i <= $number_length) {
            $sub_string = substr($min, 0, $i);
    
            // format pattern
            $format_pattern = '/'.$sub_string.'/';
            if (!preg_match($format_pattern, $max)) {
                $base = $sub_string;
    
                // Set the min and max ranges
                $minRange = substr($min, ($i - 1), $number_length);
                $maxRange = substr($max, ($i - 1), $number_length);
    
                // End while loop, we found the base
                $i = $number_length;
            }
            $i++;
        }
        // find a random number with the min and max range
        $rand = rand($minRange, $maxRange);
    
        // add the base number to the random number
        $randWithBase = $base.$rand;
    
        return $randWithBase;
    }
    
    ?>
    

提交回复
热议问题