random x digit number generation
Posted: Wed Nov 08, 2006 4:11 am
looking to generate a random 8 digit number from 00000000-99999999 inclusive but cant work out how to do it easily.
e.g. 03840441
Cheers
e.g. 03840441
Cheers
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
[feyd@home]>php -r "var_dump(getrandmax(), mt_getrandmax());"
int(32767)
int(2147483647)Code: Select all
for($i=0;$i<$yourStringLength;++$i)
{
$random_digit .= rand(0,9);
}Good point. Unfortunately, 'perfect' means 100% and neither of our methods are 100%. You're right that mine could generate duplicate ids. Yours could too (technically) if two people from behind the same NAT gateway access the page in the same second. ~sh33p1985: I believe if you incorporate uniqid() into the mix, you should be ok.nickman013 wrote:Random numbers arent always reliable... Even though the range of numbers is so great, beleive it or not I had the same number twice. If you are trying to generate unique numbers. Use the persons IP and time stamp, it works perfect.