MySQL RAND() strange behaviour
===

I am trying to obtain a random row from the result of an SQL query.

My query is as follows

    SET @rank = 0;
    SELECT * FROM(
         SELECT (@rank:=@rank+1) AS num, ...
         FROM ...
         WHERE ....) as raw
    WHERE raw.num = FLOOR(1 + (RAND() * @rank)) LIMIT 1

The general idea being that each row of the table from the inner query result is given a unique number (num). I have manually checked that this is indeed the case and that every row is numbered.

The last line is causing me distress. As it stands, `WHERE num = FLOOR(1 + (RAND() * @rank)) LIMIT 1` is returning what I want - only half the time. It appears to be returning random rows within the correct range (which for the example I am testing the query on is 0-1299). However, one in every three queries returns absolutely nothing.

Ok, so I thought maybe it was a double precision issue, so I tried using `>=` as follows: `WHERE num >= FLOOR(1 + (RAND() * @rank)) LIMIT 1`. The result in this case is confusing me. With this code I always get a result, but the number of the row returned is always < 100.

So if we call FLOOR(1 + (RAND() * @rank)) `x`. When I use `=` rather than `>=` it confirms that `x` must (in some cases) be equal to numbers greater than 1000. However, when using `>=`, the fact that the condition is satisfied means that x must always be less than 100? 

What's going on? or how else can I solve my problem

