Well, while this isn't the most efficient way to do it, you could always add
the id or link or whatever to the database x times based on the weight you
want to give it.
Otherwise, you'd have to probably add all weights together, get a total and
rand() that total, then count from there using ints... so in the example
below, the total value of all weights is 40... rand(40) to get a number...
if it was 30, count:
+10=10 NO
+5=15 NO
+3=18 NO
+20=38 YES
or if the value was 11
+10=10 NO
+5=15 YES
Think of it like trying to determine random rolls of a x-sided die where x
is the value of the total number of sides. This would be the fairest way to
ditribute loads, say if you were using this for ad banner linking.
Hope this helps!
Jesse Williams
System Administrator
DowNET VoIP Team
Electronic Data Systems
-----Original Message-----
From: Noah Spitzer-Williams [mailto:[EMAIL PROTECTED]]
Sent: Friday, June 29, 2001 3:45 PM
To: [EMAIL PROTECTED]
Subject: [PHP-DB] most efficient weighting system? URGENT
if i have several rows in a table and i want some to have certain weights
what is the best way to pick a random row based on its weight?
ex of table:
id weight
1 10
2 5
3 3
4 20
5 2
therefore, since there is a total weight of 40, id 1 should be picked 25%
(10/40) of the time, id 4 50% (20/40), etc...
what is the best way to do this?
- Noah
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]