PHP: do I need to srand() this random number?

michaelsanford

Translator, Web Developer
On the main page of my new web site, it will display a random passage of text (lyrics) pulled from a MySQL database.

PHP:
$result_count = mysql_query("SELECT COUNT(lyric_id) FROM lyrics", $link);
$resultRow = mysql_fetch_array($result_count);
$rand_lyric_id = rand(1, $resultRow['0']);

Now, do I really need to go through the extra code of srand();ing that random number, or not? It's not cryptography...

Also, how is rand() seeded if srand() isn't called?

I currently have two records in the database, and I echoed $rand_lyric_id and got '1 2 2 2 1 1 2 1 2 1 1' so it looks random enough...
 
Hi
well you can skip the rand line and use this instead
$result_count_=_mysql_query("SELECT COUNT(lyric_id) FROM lyrics ORDER BY RAND()", $link);
or
$result_count_=_mysql_query("SELECT COUNT(lyric_id) FROM lyrics ORDER BY RAND() limit 0,1", $link);

That should do it
 
Back
Top