views:

21

answers:

1

I'm using the following code to return a random row from a table. Using the field 'rand'.

SELECT * 
FROM  imgs
WHERE rand > RAND( ) 
ORDER BY rand ASC 
LIMIT 1

The field 'rand' is generated by mysql at creation using something similar to:

INSERT INTO imgs SET rand = RAND ()

For some reason the results although changing each run are only rows with a very low 'rand' field. There is definitely a complete range with over 7,000 rows. Seams to work fine if i replace the 'WHERE rand > RAND( )' with 'WHERE rand > [a number between 0-1]'

A: 

RAND() generates a floating point value x where 0 <= x < 1.0... to generate a larger number (whole number) use FLOOR(y + (RAND() * z)) which will generate a value x where y <= x < (y+z)

md5sum
I know. The problem is for some reason in the context of 'WHERE' it does not seam to be correctly generating a floating point number between 0-1. RAND () is used to generate the column at the insert stage
Olly Hicks