I am trying to generate a random integer for each row I select between 1 and 60 as timer.
SELECT downloads.date, products.*, (FLOOR(1 + RAND() * 60)) AS timer
I have searched and keep coming up to this FLOOR function as how to select a random integer in a range. This is giving me a 1 for every row. What am I missing?
I am on mysql 5.0.75
Heres the rest of the query I belive it might be a nesting issue
SELECT * FROM ( SELECT downloads.date, products.*, FLOOR(1 + (RAND() * 60)) AS randomtimer, ( SELECT COUNT( * ) FROM distros WHERE distros.product_id = products.product_id ) AS distro_count, (SELECT COUNT(*) FROM downloads WHERE downloads.product_id = products.product_id) AS true_downloads FROM downloads INNER JOIN products ON downloads.product_id = downloads.product_id ) AS count_table WHERE count_table.distro_count > 0 AND count_table.active = 1 ORDER BY count_table.randomtimer , count_table.date DESC LIMIT 10
select FLOOR( RAND() * (maximumValue-minimumValue) + minimumValue) as anyVariableName; Let us check with some maximum and minimum value. The maximum value we are considering is 200 and minimum is 100. The random number will be between 100 and 200 including 100 and 200 itself.
RAND() Return a random floating-point value.
This is working for me. Your mysql version maybe?
SELECT id, (FLOOR( 1 + RAND( ) *60 )) AS timer FROM users LIMIT 0 , 30
The output of the RAND
function will always be a value between 0 and 1.
Try this:
SELECT downloads.date, products.*, (CAST(RAND() * 60 AS UNSIGNED) + 1) AS timer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With