List:General Discussion« Previous MessageNext Message »
From:Gurhan Ozen Date:May 7 2002 5:28pm
Subject:RE: Using RAND()
View as plain text  
Quoted from the manual page I have given below is:

"RAND() is not meant to be a perfect random generator, but instead a fast
way to generate ad hoc random numbers that will be portable between
platforms for the same MySQL version."

So it was intentionally built so...

Gurhan


-----Original Message-----
From: webmaster@stripped [mailto:webmaster@stripped]
Sent: Tuesday, May 07, 2002 12:55 PM
To: mysql@stripped
Subject: Re: Using RAND()


Hi

When I used the RAND() function (MySQL 3.23.45) I found the results were not
very random, instead I use an extra bit of php code to make sure the results
are more random.

mt_srand((double)microtime()*1000000);
$sql = "select .... order by RAND(".mt_rand(0,32767).") limit 50"
$rs = mysql_query($sql);
$num=mysql_num_rows($rs);
if($num>1) mysql_data_seek($rs,mt_rand(0,$num-1));
$row=mysql_fetch_array($rs);

To get a random record from earlier versions of MySQL you could do something
like this.

mt_srand((double)microtime()*1000000);
$sql = "select... ..where .... limit 0,50"
$rs = mysql_query($sql);
$num=mysql_num_rows($rs);
if($num>1) mysql_data_seek($rs,mt_rand(0,$num-1));
$row=mysql_fetch_array($rs);

if you have a lot of records you can always put random numbers in the limit
clause,  (select ... where ...limit  random_number, random_number + 50) this
will return a random block of records, then use the seek functions to
randomly pick a record from the record block, just make sure random_number
isn't greater than the total records in your recordset.


Pete Kelly


----- Original Message -----
From: "Gurhan Ozen" <c-gurhan.ozen@stripped>
To: "Cummings, Shawn (GNAPs)" <shawn@stripped>
Cc: <mysql@stripped>
Sent: Tuesday, May 07, 2002 5:11 PM
Subject: RE: Using RAND()


> You have to have MySQL 3.23 or greater..
>
> http://www.mysql.com/doc/M/a/Mathematical_functions.html
>
> Gurhan
>
> -----Original Message-----
> From: Cummings, Shawn (GNAPs) [mailto:shawn@stripped]
> Sent: Tuesday, May 07, 2002 11:33 AM
> To: monty@stripped; Ulf Harnhammar
> Cc: mysql@stripped
> Subject: Using RAND()
>
>
>
>
> The RAND() syntax does not appear to be working for me.
>
> What version mySQL is required?
>
>
> At 04:43 PM 1/29/2002 +0200, Michael Widenius wrote:
>
> >Hi!
> >
> > >>>>> "Ulf" == Ulf Harnhammar <ulf@stripped> writes:
> >
> >Ulf> On Sun, 27 Jan 2002, Michael Widenius wrote:
> > >> One way to do this is to do as follows:
> > >> SELECT something FROM sometable WHERE somevar=somevalue ORDER BY
> > >> RAND() LIMIT 10
>
>
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
> To request this thread, e-mail <mysql-thread108227@stripped>
> To unsubscribe, e-mail
> <mysql-unsubscribe-c-gurhan.ozen=wcom.com@stripped>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>
>
> ---------------------------------------------------------------------
> Before posting, please check:
>    http://www.mysql.com/manual.php   (the manual)
>    http://lists.mysql.com/           (the list archive)
>
> To request this thread, e-mail <mysql-thread108230@stripped>
> To unsubscribe, e-mail
<mysql-unsubscribe-webmaster=trafficg.com@stripped>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>
>
>
>


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <mysql-thread108238@stripped>
To unsubscribe, e-mail
<mysql-unsubscribe-c-gurhan.ozen=wcom.com@stripped>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Thread
Using LIMIT to select random rowsUlf Harnhammar23 Jan
  • Re: Using LIMIT to select random rowsSinisa Milivojevic23 Jan
    • Re: Using LIMIT to select random rowsMichael Widenius27 Jan
      • Re: Using LIMIT to select random rowsUlf Harnhammar29 Jan
        • Re: Using LIMIT to select random rowsMichael Widenius29 Jan
          • Using RAND()GNAPs)7 May
            • RE: Using RAND()Gurhan Ozen7 May
              • Re: Using RAND()webmaster7 May
                • RE: Using RAND()Gurhan Ozen7 May
            • Re: Using RAND()Paul DuBois7 May
  • RE: Using LIMIT to select random rowsGordon29 Jan
RE: Using LIMIT to select random rowsSteven Lagerweij23 Jan