12 messages in com.mysql.lists.mysqlTrouble using RAND function
FromSent OnAttachments
Darla Baker02 Oct 1999 17:21 
Jani Tolonen03 Oct 1999 05:10 
Darla Baker03 Oct 1999 08:56.txt
Stan P. van de Burgt03 Oct 1999 09:11 
Jani Tolonen03 Oct 1999 09:22 
Darla Baker03 Oct 1999 10:15 
Martin Ramsch03 Oct 1999 12:43 
Darla Baker03 Oct 1999 14:38 
Jani Tolonen03 Oct 1999 17:55 
Darla Baker04 Oct 1999 03:15 
Matthias Urlichs04 Oct 1999 04:07 
Michael Widenius06 Oct 1999 06:35 
Subject:Trouble using RAND function
From:Jani Tolonen (ja@mysql.com)
Date:10/03/1999 05:10:43 AM
List:com.mysql.lists.mysql

Darla Baker writes:

I am trying to write a perl script that I will link to a graphic on my web page so that when the graphic is clicked it will take the user to a random favorite from my collection of favorite urls. I've been testing the SELECT statement from the mysql command line using the following statement:

SELECT _rowid,url from favorites WHERE _rowid = ROUND( (RAND()*24) + 1 );

Right now there are 25 entries in the table and _rowid is an auto_increment field. When I execute the query I get an empty set frequently and then occasionally it will return a row. What am I missing? I am running mysql 3.22.22

Hi Darla,

The above should work. Can you please include in the mail the output from the following:

shell> mysqldump db_name favorites

Please show us also some output from the client when you execute queries:

mysql> SELECT ROUND((RAND() * 24) + 1);

and

mysql> SELECT RAND();

couple of times.

Here is an alternative query you can try, for the meanwhile we try to find out why the above doesn't work:

SELECT _rowid,url FROM favorites WHERE _rowid = (((RAND() * 24) % 24) + 1);

Regards,

- Jani