RAND() function in MySql

July 20, 2003 by  
Filed under Code Snippets

I was trying to use this query to get a random row from the database:

$myRandom = "SELECT * FROM MYTABLE ORDER BY RAND() LIMIT 1";

and I couldn’t figure out why it would continually get the previous number instead of some random number.

Ahh, but look what’s at the bottom of the page. ;)

“As of MySQL 3.23.52, MySQL changed the way RAND() functions, such that you MUST supply a SEED to get an actual random number. If you do not, each new connection will return close to the same number as the previous new connection. An example to test this would be to run the follow command multiple times in a row. mysql -u username -p -e’select rand()’ A basic way to seed this would be to run RAND(NOW()).”

So now I know, I must provide a random seed. ;)

Source: http://www.mysql.com

Related Products:

Elegoo UNO Project Super Starter Kit with Tutorial for ArduinoElegoo UNO Project Super Starter Kit with Tutorial for Arduino

Elegoo Inc. is a professional manufacturer and exporter that is concerned with the design, development production and marketing of arduino, ... Read More >
Fire 7 Kids Edition Tablet, 7" Display, 16 GB, Blue Kid-Proof CaseFire 7 Kids Edition Tablet, 7" Display, 16 GB, Blue Kid-Proof Case

Powerful, full-featured Fire 7 tablet--with 1 year of Amazon FreeTime Unlimited and a Kid-Proof Case--for a savings of up to $129--plus a 2-year w... Read More >

Fitbit Charge 2 Heart Rate + Fitness Wristband, Black, Large (US Version)Fitbit Charge 2 Heart Rate + Fitness Wristband, Black, Large (US Version)Make every beat count with Fitbit Charge 2—the all-new heart rate and fitness wristband built for all-day, workouts and beyond. PurePulse continuous... Read More >
Related Posts Plugin for WordPress, Blogger...