2

I want to generate a number with 75 characters using PHP.

I have searched everywhere and got nothing, except for this: http://dailycoding.com/tools/RandomNumber.aspx

I do not think it is effective to grab it from that page.

What I have tried is this:

rand(1,9999999999999999999999999999999999999);
CommandZ
  • 3,333
  • 1
  • 23
  • 28
Aby
  • 57
  • 2
  • 9
  • 1
    Are you doing math with the number, or just displaying a large number-like string (no maths)? – DACrosby Feb 14 '14 at 20:41
  • 2
    http://stackoverflow.com/questions/1479823/in-php-how-do-i-generate-a-big-pseudo-random-number – quickshiftin Feb 14 '14 at 20:41
  • @DACrosby's question is crucial. `rand()` returns an integer which has a max value of `2147483647`. If you want to do any math, you'll need to leave it as an integer. Otherwise, you can make a function that picks random numbers `0-9` and appends them to a string to make a large X-digit number. – Sam Feb 14 '14 at 20:42
  • I'm sorry, I just need the string(no maths) – Aby Feb 14 '14 at 20:45

5 Answers5

8

try this

function bigNumber() {
    # prevent the first number from being 0
    $output = rand(1,9);

    for($i=0; $i<74; $i++) {
        $output .= rand(0,9);
    }

    return $output;
}
visevo
  • 791
  • 7
  • 23
2
function genRandomNumber($length = 15, $formatted = true) {
    $nums = '0123456789';

   // First number shouldn't be zero
    $out = $nums[mt_rand( 1, strlen($nums)-1 )];  

   // Add random numbers to your string
    for ($p = 0; $p < $length-1; $p++)
        $out .= $nums[mt_rand( 0, strlen($nums)-1 )];

  // Format the output with commas if needed, otherwise plain output
    if ($formatted)
        return number_format($out);
    return $out;
}

echo genRandomNumber();         // 7,825,104,236
echo genRandomNumber(14,false); // 11648596961188
echo genRandomNumber(25);       // 7,154,062,783,835,742,231,986,176

See here: http://codepad.org/yDvyo6MY

Community
  • 1
  • 1
DACrosby
  • 11,116
  • 3
  • 39
  • 51
0

PHP's rand() function may have as little as 15 bits on some platforms, so it's only good for 4 digits at a time. But since to don't actually need the numeric value of the total, call rand(0,9999) to get a 4-digit value, convert it to a string (you'll have concatenate "000" in front of it and then trim to 4 characters with substr() to get leading zeros), and add 4 characters at a time to a big string.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
0

It's not possible because, the biggest integer can be generated with a PHP 64 bits is 9223372036854775807. The biggest with a PHP 32 bits is 2147483647.

You can see most information on the PHP documentation.

If you want an equivalent, you can generate a string but you can't manipulate your integer.

Baptiste Donaux
  • 1,300
  • 13
  • 34
-3

Here you go ...

function genRandomString($length = 75) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}
cmorrissey
  • 8,493
  • 2
  • 23
  • 27