[PHP-DB] Random numbers with range in mysql?

2002-09-28 Thread Leif K-Brooks

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?


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




RE: [PHP-DB] Random numbers with range in mysql?

2002-09-28 Thread John W. Holmes

 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