RE: [PHP-DB] Random numbers with range in mysql?
| From: | John W. Holmes | Date: | Sat, 28 Sep 2002 19:40:35 +0000 |
| Subject: | RE: [PHP-DB] Random numbers with range in mysql? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-22071@lists.php.net to get a copy of this message | ||
> I'm trying to make "shops" that sell virtual objects. They restock
once
> every 15 minutes. This is all set up. The problem is, I would like
to
> have objects in these shops with a random chance of being restocked.
My
> idea is to store two numbers in the restock list: chance, and outof.
> (For a 1/5 chance on each restock, those values would be 1 for
chance,
> 5 for outof). The problem is, I need a way of generating a random
> number within a certain range. I thought of going through php and
> mt_rand(), but that would make for a very inefficient script. Any
ideas?
MySQL has a RAND() function. Returns a random number between zero and
one. Multiply it by your limit and add one to get a range.
SELECT FLOOR(RAND()*5)+1
To get a random number between one and five. To make an update random,
try something like this:
UPDATE yourtable SET ... WHERE FLOOR(RAND()*5)+1 = 1 AND ...
---John Holmes...