Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate BIG random number php [duplicate]

Tags:

php

random

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);
like image 366
Aby Avatar asked Aug 01 '26 18:08

Aby


2 Answers

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;
}
like image 133
visevo Avatar answered Aug 03 '26 09:08

visevo


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

like image 39
DACrosby Avatar answered Aug 03 '26 09:08

DACrosby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!