On Saturday 06 December 2003 10:39 pm, TheHeadSage wrote:
> Hey,
>
> I've got a script which used to generate a random quote. It worked fine
> untill this problem occurred..
>
> Say we have 4 quotes with QuoteID's of 1 through to 4
> The script would grab a number of rows, and generate a random number
> between 1 and this value (being 4 in this case) This worked fine untill,
> one of the quotes was removed and another added, leaving 4 quotes once
> again, but the last one had an ID of 5. The problem is the Random number
> generated is between 1 and 4, so the final quote is never shown..
>
> I tried using the RAND() function in the MySQL statement it's self, but
> it's not random enough...

Perhaps something like

SELECT quote FROM quotes ORDER BY RAND('.mt_rand().') LIMIT 1

>
> Then I thought of doing this:
>
> Select all the Quote ID's from the DB.
> Store them in an Array.
> Generate a random number between 1 and the last ID.
> If the Random Number matches a Quote ID, then display the quote,
> otherwise Generate another

Wow, sounds complicated. How about

shuffle($quotes);
echo $quotes[0];

but much better speed-wise to do it from the DB.

>
> But I couldn't get the code to work. Any suggestions on either how to
> write the above, or made the MySQL RAND() function, more random?

um, yeah.

-- 
Evan Nemerson
[EMAIL PROTECTED]
http://coeusgroup.com/en

--
"A popular government, without popular information, or the means of acquiring 
it, is but a Prologue to a Farce or a Tragedy - or perhaps both. Knowledge 
will forever govern ignorance, and a people who mean to be their own 
Governors must arm themselves with the power which knowledge gives."

-James Madison

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to