Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rand()

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi. I am wondering if rand() is a proper function in PHP. I have a script (below), but for some reason no random number is being generated. Any ideas please? I'm a Perl programmer by nature, so i may have done something stupid that would cause it not to work :-/

Code:
<?php  
/* 

*/  

$min = 1; $max = 50;
$get_number = $guess;
$rand_number = rand($mins, $max); 

if ($rand_number == $get_number) { good($get_number); } else { bad($get_number); }

function good($number)

{

echo &quot;Your number was right ($number)&quot;;

echo &quot;The answer was&quot; . $rand_number;

} # end the good sub.

function bad($number)

{

echo &quot;Your number was wrong ($number)&quot;;

echo &quot;The answer was&quot; . $rand_number;

} # end the bad sub.

?>

Thanks

Andy
 
Hi Andy,
rand() is a function , before you can use it, you must
seed it with srand().

example :
srand((double) microtime() * 1000000);
$ran_num = rand(); // random number
// or
$unique_id = md5 (uniqid (rand())); devnull22

--
Apparently if you play the Windows NT CD backwards you hear satanic messages. If you think that's bad, play it forwards and it installs Windows NT !
 
Ok, thanks for that reply. It did help, kinda :p

Now I am still having a problem. I dont quite understand the srand() thing. How would i make that number only x digets long? I.e. between 5 and 10?

The code I am now using is;

Code:
<?php  
/* 

*/  

$min = 1; $max = 50;
$get_number = $guess;

srand((double) microtime() * 100);
   $rand_number = rand(); // random number


if ($rand_number == $get_number) { good($get_number, $rand_number); } else { bad($get_number, $rand_number); }

function good($number, $rand_no)

{

echo &quot;Your number was right ($number)&quot;;

echo &quot;The answer was&quot; . $rand_no;

} # end the good sub.

function bad($number, $rand_no)

{

echo &quot;Your number was wrong ($number)&quot;;

echo &quot;The answer was&quot; . $rand_no;

} # end the bad sub.

?>

Thanks :)

Andy
 
Hi,

To get a random number between 5 and 10 use rand(5,10) as you did before. All srand does is &quot;seed&quot; the number generator (i.e. give it an &quot;anchor&quot; for the random number algorithm)

Remember there is no such thing as a truly random number generated by a computer. What really happens is some pretty complex mathematics to generate a number that appears random. In fact with most programming languages, you can use a fixed seed value and always generate the same sequence of &quot;random&quot; numbers - probably true of PHP as well - which is why srand() uses time most often as part of the seed as this is not random but &quot;different each time you call srand().

Hope This helps.

Cheers
Richard
 
Ah, great :) Thanks.

Do you think it was the space in
Code:
rand($mins, $max);
that was causig the problem?

Thanks

Andy
 
Hi Andy

No the space is irrelevant. In the first code you posted you had (missing the srand requirement):

$rand_number = rand($mins, $max);

In the second post you had (now you have srand but are missing the $min,$max parameters in rand()):

srand((double) microtime() * 100);
$rand_number = rand(); // random number

Cheers
Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top