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:

Acer Aspire E 15 E5-575-33BM 15.6-Inch Full HD Notebook (Intel Core i3-7100U Processor 7th Generation , 4GB DDR4,  1TB 5400RPM Hard Drive,  Intel HD Graphics 620, Windows 10 Home), Obsidian BlackAcer Aspire E 15 E5-575-33BM 15.6-Inch Full HD Notebook (Intel Core i3-7100U Processor 7th Generation , 4GB DDR4, 1TB 5400RPM Hard Drive, Intel HD Graphics 620, Windows 10 Home), Obsidian BlackAcer Aspire E5-575-33BM comes with these high level specs: 7th Generation Intel Core i3-7100U Processor (2.4GHz, 3MB L3 cache), Windows 10 Home, 15.6"... Read More >
Practical Electronics for Inventors, Fourth EditionPractical Electronics for Inventors, Fourth Edition

A Fully-Updated, No-Nonsense Guide to Electronics

Advance your electronics knowledge and gain the skills necessary to develop and construct y... Read More >

All-New Amazon Fire HD 8 Tablet Case (7th Generation, 2017 Release), Marine BlueAll-New Amazon Fire HD 8 Tablet Case (7th Generation, 2017 Release), Marine Blue

Slim, form-fitting fabric case perfectly fits your all-new Fire HD 8 and provides front and back protection at home or on the go. Easy to a... Read More >

Related Posts Plugin for WordPress, Blogger...