RE: [PHP] Re: Random Numbers..
| From: | Burrito Warrior | Date: | Sun, 07 Dec 2003 05:24:51 +0000 |
| Subject: | RE: [PHP] Re: Random Numbers.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-172116@lists.php.net to get a copy of this message | ||
No, I wouldn't do that. What if another quote gets deleted. The new
assigned
QuoteID would be 6 and your solution wouldn't work.
This would:
/* selecting a random record, where n = 1. Works fine on small tables */
mysql> SELECT value FROM table ORDER BY RAND() LIMIT n;
/* selecting a random record. This is good on large tables if numbering
sequence is used */
mysql> SET @val = FLOOR(RAND() * n) + 1;
mysql> SELECT value FROM table WHERE ID = @val;
-----Original Message-----
From: Alex [mailto:anthron@anthron.com]
Sent: Sunday, December 07, 2003 12:00 AM
To: php-general@lists.php.net
Subject: [PHP] Re: Random Numbers..
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...
>
> 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
>
> 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?
how about if ($randnumber == 4) $randnumber++; ?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php