Jump to content


Photo

Random 8 numbers string...


  • Please log in to reply
6 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 10 August 2006 - 12:18 PM

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 :/.

$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;


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 10 August 2006 - 12:27 PM

here is a function that you can use. It will return a random number that is eight digits long.

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; 
}

Now you can use this function like this.

$random_number = random_number();
echo "$random_number";

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 10 August 2006 - 12:56 PM

that only outputs double figures :/ I need 8.

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 10 August 2006 - 01:09 PM

$lenght_of_key = 8;
$key='EUM';
for ($i=0; $i<$lenght_of_key; $i++) $key .=rand(1,9);
echo $key;

#5 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 10 August 2006 - 01:21 PM

An easier way:
$num = sprintf("%08d", rand(0,99999999));

~ D Kuang

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 August 2006 - 01:21 PM

What's wrong with:
<?php
$key = rand(10000000,99999999);
echo $key;
?>

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

#7 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 10 August 2006 - 01:34 PM

thanks guys :]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users