Jump to content

Generating 9 random digits exactly


graham23s

Recommended Posts

Hi Guys,

 

I'm trying to generate 9 random digits, i have done rand(0, 999999999) but i was wanting the script to generate 9 random digits exactly not 8 or 7 but consistantly 9 i cant think of a way to dfo ity lol

 

any help would be great

 

thanks guys

 

Graham

Link to comment
Share on other sites

Hi Guys,

 

I'm trying to generate 9 random digits, i have done rand(0, 999999999) but i was wanting the script to generate 9 random digits exactly not 8 or 7 but consistantly 9 i cant think of a way to dfo ity lol

 

any help would be great

 

thanks guys

 

Graham

 

Why not generate each one seperately then add them to a string and convert that string if you need to do math functions on it?

Link to comment
Share on other sites

And another way!

 

<?php

echo randdigits( 9 ) . "\n";
echo randdigits( 9 ) . "\n";
echo randdigits( 9 ) . "\n";
echo randdigits( 9 ) . "\n";
echo randdigits( 9 ) . "\n";
echo randdigits( 9 ) . "\n";
echo randdigits( 9 ) . "\n";
echo randdigits( 9 ) . "\n";


/**
* Generates $n random digits
* 
* @param int $n
*/
function randdigits( $n ) {
    if( ! is_int( $n ) || $n < 1 ) return false;
    
    $v = 0;
    for( $i = 0; $i < $n; $i += 1 ) {
        $v = ($v * 10) + rand(0,9);
    }
    return $v;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.