pouncer Posted April 14, 2007 Share Posted April 14, 2007 how do i get a random 6 digit number in php guys? Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 14, 2007 Share Posted April 14, 2007 $digit_loop = 1; $digit = ''; while ( $digit_loop <= 6){ $digit = $digit.rand(0, 9); } echo $digit you can check i in a loop througha database or whatever if it needs to be unique Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 14, 2007 Share Posted April 14, 2007 $random = rand(100000,999999); Quote Link to comment Share on other sites More sharing options...
pouncer Posted April 14, 2007 Author Share Posted April 14, 2007 ah thanks. so php has the rand function too could i just not use rand(100000,999999); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 14, 2007 Share Posted April 14, 2007 Use the rand() function: <?php $num = rand(100000,999999); echo $num; ?> or <?php $num = sprintf("%06d",rand(1,999999)); // zero filled 6 digits echo $num; ?> Ken Quote Link to comment Share on other sites More sharing options...
448191 Posted April 14, 2007 Share Posted April 14, 2007 $digit_loop = 1; $digit = ''; while ( $digit_loop <= 6){ $digit = $digit.rand(0, 9); } echo $digit you can check i in a loop througha database or whatever if it needs to be unique Uhmm... Apart from the fact that it serves no pupose, there are 3 things wrong with that code: 1. It results in an infinite loop. 2. It allows leading zero's, producing 'numbers' with less than 6 digits. 3. It uses a string to store an integer. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted April 14, 2007 Share Posted April 14, 2007 well i didnt say it was perfect, but its a start, and easily fixed. ive done it before, but im sure the smart people at this forum can fix it for their own sites, its not like were doing all the coding for them.......... its a start and a pointer, surely thats gotta help Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 14, 2007 Share Posted April 14, 2007 3. It uses a string to store an integer. Since PHP is a typeless language, this is not a bad thing as can be seen by this example: <?php <?php $a = sprintf("%06d",rand(1,999999)); $b = rand(100000,999999); echo '<pre>'; var_dump($a); var_dump($b); echo '</pre>'; $c = $a + $b; echo $c; ?> Numbers in strings are treated as numbers. Ken Quote Link to comment Share on other sites More sharing options...
obsidian Posted April 14, 2007 Share Posted April 14, 2007 well i didnt say it was perfect, but its a start, and easily fixed. Indeed, and it's not a bad suggestion. Here's one using that idea that does work like you are intending: <?php $num = ''; for ($i = 0; $i < 6; $i++) { $num .= rand(0,9); } ?> While this isn't practical for smaller numbers that you could simply use the rand() method mentioned initially by AndyB, this could get very handy when working with random numbers that exceed the boundaries of the unsigned integer in PHP. Imagine you needed a 30-40 digit random number (or even bigger). You can't use the built-in data types, so you'd have to use something like this to do it. Quote Link to comment Share on other sites More sharing options...
448191 Posted April 15, 2007 Share Posted April 15, 2007 Ok, so maybe I was a bit harsh, and too hasty to dismiss string assembly as a means to generate a random number. However there are still some things to consider. <?php $num = ''; for ($i = 0; $i < 6; $i++) { $num .= rand(0,9); } ?> That still possibly produces a string with a leading zero. With a minor ajustment one should be fine though: <?php $num = rand(1,9); for ($i = 0; $i < 5; $i++) { $num .= rand(0,9); } ?> this could get very handy when working with random numbers that exceed the boundaries of the unsigned integer in PHP. You could also use a float instead: <?php public function getFloat($digits){ if(is_int($digits)){ if($digits > 308) return false; $this->maxValue = pow(10, $digits) - 1; $this->minValue = $digits == 1? 0 : pow(10, $digits) / 10; do { $this->value = $this->minValue + $this->maxValue * mt_rand(0, pow(2,15)-1)/pow(2,15)-1; } while($this->value < $this->minValue || $this->value > $this->maxValue); return $this->value; } throw new Exception('RandomNumber::getFloat(): $digits must be (int)'); } ?> But float also has a max length, ~1.0E+308 - 308 digits. So if you want more than 308 digits, yes, you'll need to use strings. The BCMath extension can do it too, but (aside from the fact that it also uses strings) that would be overkill imo. You can use the BCMath functions to calculate on your randomly assembled numeric string. Also, (s)printf doesn't play along when you ask for something impossible: <?php printf('%d', $outOfBoundaryInt); ?> Returns 2147483647 (max int). While an appropiate result, you might expect it to display as a float because of PHP's type juggling. printf() doesn't roll that way though. You should be carefull when using numbers as strings: <?php //Very long string to represent float, close to INF $str = '99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'; echo strlen($str)." digits\n"; //308 'digits' $float = (float) $str; //Works fine, 1.0E+308 printf("%f",$float)."\n";//Messes up because not possible to represent as decimal float printf("%e",$float)."\n";//Close enough, 1.000000e+30813 echo strlen($float)." digits\n"; //Exponential notation, 6 'digits' var_dump($float.'13'); //string(7) "1E+30813" var_dump((float) $str); //float(1.0E+308) var_dump((float) $float.'5'); //Stays string: string(7) "1E+3081" var_dump($float.'5' + 1); //PHP: Ne comprend pas!: float(INF) var_dump(bcadd($float.'5', 1));//Ignores invalid E: string(402) "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" var_dump('1 .5' + 1); // .5 is discarded because of space: int(2) ?> Anyway, to make a long story short (darn, too late ;P), be careful when treating numbers as strings. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.