[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-db
Subject:    RE: [PHP-DB] Random numbers with range in mysql?
From:       "John W. Holmes" <holmes072000 () charter ! net>
Date:       2002-09-28 19:40:35
[Download RAW message or body]

> 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...



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

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic