Jump to content


Photo

rand() always returns the same..


  • Please log in to reply
6 replies to this topic

#1 w0rtel

w0rtel
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 13 January 2003 - 11:26 PM

I want to get a random row from my table. And i use this:


$sql = "SELECT * FROM random ORDER BY RAND() LIMIT 1";



$result = mysql_query($sql);

$object = mysql_fetch_object($result);



$rid = $object->rid;

But I always get the first row. Never a different one.. Why?

(MySQL v. MySQL 3.23.54-nt)

Thnx 4 the help..

#2 pallevillesen

pallevillesen
  • Members
  • PipPipPip
  • Advanced Member
  • 135 posts
  • LocationDenmark

Posted 14 January 2003 - 10:53 AM

I want to get a random row from my table. And i use this:


$sql = "SELECT * FROM random ORDER BY RAND() LIMIT 1";



$result = mysql_query($sql);

$object = mysql_fetch_object($result);



$rid = $object->rid;

But I always get the first row. Never a different one.. Why?

(MySQL v. MySQL 3.23.54-nt)

Thnx 4 the help..


Probably because you\'re opening a new connection each time.. The rand() function is not truly random, so you need to give it a near random seed, i.e. use rand(seedvalue)...

You may use something like rand(seconds(curdat())) or some other stuff. If you\'re using php you could take a variable and assign a random number to it and them use that as seed in you query.

P., denmark
Palle Villesen, www.birc.dk [br]Bioinformatics Research Center

#3 axyonline

axyonline
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 15 February 2003 - 11:13 AM

may be u can random selection by doing that:
get the last record from table ( u can do that \"select last_insert_id()\" )
then $min=1 ; $max=$last_record_id ;
$random_record=rand($min,$max);
then u print the random row by mysql_fetch_row..
if u have 10000 records in your table it will search all the records from table then randomize 1 row. searching is too late. but doing like this searching is quite time and it is easy for me :)
see u later
Cihan
php-mysql freak. electronic engineer student. [br]creative webmaster.and ask for more :)

#4 dmeade

dmeade
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationChicago

Posted 23 February 2003 - 08:53 PM

thanks to the information above I managed to get a good solution:

[php:1:d09dd98aa6]<?php
$random_seed=rand(1, time());
$query = \"SELECT * FROM $table ORDER BY RAND($random_seed) limit 1\";
?>[/php:1:d09dd98aa6]

Thanks all.
- Dave

#5 sohaibshaheen

sohaibshaheen
  • Members
  • PipPip
  • Member
  • 23 posts
  • LocationPakistan

Posted 29 August 2010 - 04:12 AM

thanks to the information above I managed to get a good solution:

[php:1:d09dd98aa6]<?php
$random_seed=rand(1, time());
$query = \"SELECT * FROM $table ORDER BY RAND($random_seed) limit 1\";
?>[/php:1:d09dd98aa6]

Thanks all.
- Dave


What if the rand output doesnot equal row value.. It may produce 100 when u have 30 results only...
Follow me on twitter @sohaibshaheen

http://t-bait.blogspot.com/

#6 Pikachu2000

Pikachu2000
  • Staff Alumni
  • I hate everything.
  • 11,384 posts
  • LocationFuture Independent Republic of Texas
  • Age:106

Posted 29 August 2010 - 04:27 PM

Google "mysql why order by rand is bad".

"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf *

Random Quote: "

Remember, you're unique. Just like everybody else!


#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 30 August 2010 - 02:37 PM

Google "mysql why order by rand is bad".

Or read the stickies.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users