Jump to content

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]
Link to comment
https://forums.phpfreaks.com/topic/17124-random-8-numbers-string/
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]
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.