SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Truly Randomizing Records?

    Hey guys,

    I've got this little script here:

    PHP Code:
    <?php
    include 'connect.php';
    $connection mysql_connect($server$user$pass);
    mysql_select_db($db,$connection);
        
    $query "select * from jokes where short='yes' order by rand() limit 1";
        
    $result mysql_query($query) or die;
        
    $row mysql_fetch_array($resultMYSQL_ASSOC);
        
        
    $id $row['id'];
        
    $joke addslashes(preg_replace("/\r\n|\r|\n/","",$row['joke'] . " <a href="http://www.happytwice.com/" target='_blank'>see more</a>"));
        
    echo "document.write('$joke'); ";
    ?>
    It works fine, except it tends to select the first record about 50% of the time, and I'd like to clean that up... How would I get it so it actually randomly selected records? I don't care if there are repeats occasionally (so I don't want to tick off records that have been used, reset them when there are none left, etc).

    Help?

    J
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  2. #2
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea, I had this problem too...

    I read somewhere from the great Dr. Larry that if you do this:

    PHP Code:
    mysql_query('SELECT RAND(), RAND(), RAND()'); 
    before
    PHP Code:
    $query "select * from jokes where short='yes' order by rand() limit 1"
    it will end up more random...

  3. #3
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ... Just insert that line? Seems odd...

    J
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Hong Kong
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or just run the below code twice is ok too

    $query = "select * from jokes where short='yes' order by rand() limit 1";
    $result = mysql_query($query) or die;
    $row = mysql_fetch_array($result, MYSQL_ASSOC);

  5. #5
    SitePoint Wizard silver trophy Jeremy W.'s Avatar
    Join Date
    Jun 2001
    Location
    Toronto, Canada
    Posts
    9,121
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just run it twice?

    Why do I feel confused... I've never had this problem with any other DBMS's

    J
    SVP Marketing, SoCast SRM
    Personal blog: Strategerize
    Twitter: @jeremywright

  6. #6
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hmmm... when I took a random item out of an array for my amazon contest, I seeded the array first to make sure it was completely random. Perhaps the same could be done here? *shrug*
    Mike
    It's not who I am underneath, but what I do that defines me.

  7. #7
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah there's some bug in mySQL

    Do a search

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The random number generator for MySQL isn't that good.

    You can try seeding it with a RAND(NOW()) or some other random seed value.

  9. #9
    SitePoint Guru dragonhawk's Avatar
    Join Date
    Apr 2002
    Location
    Melbourne
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried it with RAND(NOW()) but that didn't really work for me. When I inserted the line:
    PHP Code:
    mysql_query('SELECT RAND(), RAND(), RAND()'); 
    then it came out more random.

    Read here:
    http://sitepointforums.com/showthrea...threadid=98872


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •