#### Archived

This topic is now archived and is closed to further replies.

# Random 8 numbers string...

## Recommended Posts

Heyy Guys,
I have my little random number string, I can't seem to work-out
how to make it generate a random 8 number code. sometimes it
is 1 number or others it is 2 numbers :/.

[code=php:0]\$numbers = array('1','2','3','4','5','6','7','8','9');
for (\$i=8; \$i<10; \$i++)
{
\$Keyarray[\$i] = \$numbers[rand(0,count(\$numbers))];
}
shuffle(\$Keyarray);
\$Newkey = implode("",\$Keyarray);

echo "EUM\$Newkey";
exit;[/code]

##### Share on other sites
here is a function that you can use. It will return a random number that is eight digits long.

[code=php:0]
function random_number() {
\$salt = "0123456789";
srand((double)microtime()*1000000);
\$i = 0;
while (\$i <= 8) {
\$num = rand() % 33;
\$tmp = substr(\$salt, \$num, 1);
\$num = \$num . \$tmp;
\$i++;
}
return \$num;
} [/code]

Now you can use this function like this.

[code=php:0]
\$random_number = random_number();
echo "\$random_number";
[/code]

##### Share on other sites
that only outputs double figures :/ I need 8.

##### Share on other sites
\$lenght_of_key = 8;
\$key='EUM';
for (\$i=0; \$i<\$lenght_of_key; \$i++) \$key .=rand(1,9);
echo \$key;

##### Share on other sites
An easier way:
[code=php:0]\$num = sprintf("%08d", rand(0,99999999));[/code]

##### Share on other sites
What's wrong with:
[code]<?php
\$key = rand(10000000,99999999);
echo \$key;
?>[/code]

Ken
(poirot beat me to it by 9 seconds .. GMTA :) )

thanks guys :]