Jump to content

random 6 digit number


pouncer

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.