How to get random integer mysql range?

I am trying to create a random integer for each line. I choose from 1 to 60 as a timer.

SELECT downloads.date, products.*, (FLOOR(1 + RAND() * 60)) AS timer 

I searched and continue to approach this FLOOR function to select a random integer in the range. This gives me 1 for each row. What am I missing?

I'm on mysql 5.0.75

Here is the rest of the request, I suppose it could be a nesting problem

 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 
+44
mysql random
Jun 11 '09 at 23:57
source share
4 answers

This works for me. Perhaps your version of mysql?

 SELECT id, (FLOOR( 1 + RAND( ) *60 )) AS timer FROM users LIMIT 0 , 30 
+78
Jun 12 '09 at 0:08
source share

I fulfill your request and this gives me a random number for each line .... maybe it has something to do with the random name (timer)?

+2
Jun 12 '09 at 0:07
source share

The output of the RAND function will always have a value between 0 and 1 .

Try the following:

 SELECT downloads.date, products.*, (CAST(RAND() * 60 AS UNSIGNED) + 1) AS timer 
0
Jun 12 '09 at 0:10
source share

You can increase the number times the number of entries in the table.

 SELECT id, (FLOOR( (SELECT MIN(id) FROM your_table ) + RAND( ) * 1000000 ) ) AS timer FROM your_table LIMIT 0 , 30 
0
Feb 04 '15 at 15:25
source share



All Articles