Fixed Length & Large Random Numbers with PHP

PHP’s random number functions generate integers with variable number of digits. Also, the maximum size of the random number generated that way is PHP_INT_MAX. You can remove these two limitations using the function below.

//Returns string of specified length containing decimal characters
 
function big_rand( $len = 9 ) {
    $rand   = '';
    while( !( isset( $rand[$len-1] ) ) ) {
        $rand   .= mt_rand( );
    }
    return substr( $rand , 0 , $len );
}

How to use

What is different in this function is that you have to specify a length, rather than a range, to generate the random number. Here is how to use it:

$rand = big_rand( 60 );
 
echo strlen( $rand );
//Output: 60