Jump to content

[SOLVED] Problem using while() to get unique number


shamuraq

Recommended Posts

hi all,

 

I've been trying to source for script to produce unique for digit number as in getting php to randomise 4 digits with no digit having same value.

 

So i tried this code i got from

http://forums.digitalpoint.com/showthread.php?t=9048

and modified it a bit into:

<?
error_reporting(E_ALL);
ini_set('display_errors', 1);

$arr = array();
$i=0;
$result = array();

while ( count($arr) < 6 ) {
    $x = mt_rand(1,10);
    if ( !in_array($x,$arr) ) {
	$result[$i] = $x;
	$i+=1;
}
}
print_r($x);
?>

This is the output that i got:

Fatal error: Maximum execution time of 30 seconds exceeded in D:\Apache Group\Apache2\htdocs\..... on line 11

Any suggestions?

maybe its just sleepless nights trying to get this thing working...

i made the change "$result[$i] to $arr[$i]"

and only 1 number came out instead of 6 numbers...

<?
error_reporting(E_ALL);
ini_set('display_errors', 1);

$arr = array();
$i=0;
$result = array();

while ( count($arr) < 6 ) {
    $x = mt_rand(1,10);
    if ( !in_array($x,$arr) ) {
	$arr[$i] = $x;
	$i+=1;
}
}
print_r($x);
?>

:confused:::)

Archived

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

×
×
  • 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.