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:

Omron 10 Series Wireless Upper Arm Blood Pressure Monitor with Cuff that fits Standard and Large Arms (BP786/BP786N) with Bluetooth Smart ConnectivityOmron 10 Series Wireless Upper Arm Blood Pressure Monitor with Cuff that fits Standard and Large Arms (BP786/BP786N) with Bluetooth Smart ConnectivityOffering precision and comfort with our Contoured Wide Range Cuff that fits standard and large Arms, the Omron Advanced home blood pressure monitor (B... Read More >
All-New Fire HD 8 Tablet with Alexa, 8" HD Display, 16 GB, Black - with Special OffersAll-New Fire HD 8 Tablet with Alexa, 8" HD Display, 16 GB, Black - with Special Offers

The all-new Fire HD 8 is the next generation of our best-reviewed Fire tablet ever--with up to 12 hours of battery life, a vibrant 8" HD display, ... Read More >

Related Posts Plugin for WordPress, Blogger...