[PHP-DB] Random numbers with range in mysql?
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?
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
[PHP-DB] Random numbers?
Hi there everyone, I need to generate a random number between 1 and 15 for insertion into a DB, I can easily do the DB side of it but what is the best method for quickly generating a random number between 1 and 15 (including 1 and 15 in the equation)? Thanks for all your help. Regards Chris www.planetoxygenet.com
Re: [PHP-DB] Random numbers?
On sön, 03 feb 2002, Chris Payne wrote: Hi there everyone, I need to generate a random number between 1 and 15 for insertion into a DB, I can easily do the DB side of it but what is the best method for quickly generating a random number between 1 and 15 (including 1 and 15 in the equation)? srand((double)microtime()*100); $randomnumber = rand(1,15); Something like this? -- patrik_wallstrom-foodfight-[EMAIL PROTECTED]+46-709580442 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Random numbers?
Hi there, That is absolutely PERFECT, thank you so much for that you are a lifesaver :-) Regards Chris Payne www.planetoxygene.com On sön, 03 feb 2002, Chris Payne wrote: Hi there everyone, I need to generate a random number between 1 and 15 for insertion into a DB, I can easily do the DB side of it but what is the best method for quickly generating a random number between 1 and 15 (including 1 and 15 in the equation)? srand((double)microtime()*100); $randomnumber = rand(1,15); Something like this? -- patrik_wallstrom-foodfight-[EMAIL PROTECTED]+46-709580442 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php