Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
Choosing a random entry in a table by "weight"
Old
  (#1)
Guest
Guest
 
Posts: n/a
Default Choosing a random entry in a table by "weight" - 06-02-2007, 08:55 PM

I have a MySQL table of servers, I use RAND() to pick a random server
to use each time, but how can I add a number to each server entry that
allows it be to picked more often than the other 20 servers?

For example Server1's weight is 80 and Server2's weight is 40 and
hence Server1 is more likely to be picked than the others.

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Choosing a random entry in a table by "weight"
Old
  (#2)
Rami Elomaa
Guest
 
Posts: n/a
Default Re: Choosing a random entry in a table by "weight" - 06-02-2007, 08:55 PM

EMAIL REMOVED kirjoitti:
> I have a MySQL table of servers, I use RAND() to pick a random server
> to use each time, but how can I add a number to each server entry that
> allows it be to picked more often than the other 20 servers?
>
> For example Server1's weight is 80 and Server2's weight is 40 and
> hence Server1 is more likely to be picked than the others.
>


In theory like this: pick a random weight and take into group all the
items that weigh more, it's more likely that heavier items are taken
into the group, than the lighter. From the group you have created, pick
one randomly. If no items were in the group, your random weight was too
big, so you might limit it to < maxweight, so that always at least one
item is picked.

Transforming this into a query is another thing. This is very rough
example but you'll get the idea, I suppose:

SELECT * FROM ( SELECT * FROM servers WHERE weight > RAND() ) ORDER BY
RAND() LIMIT 1,1

--
EMAIL REMOVED

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Choosing a random entry in a table by "weight"
Old
  (#3)
petersprc
Guest
 
Posts: n/a
Default Re: Choosing a random entry in a table by "weight" - 06-02-2007, 08:55 PM

On May 27, 12:10 pm, alpha.be...@googlemail.com wrote:
> I have a MySQL table of servers, I use RAND() to pick a random server
> to use each time, but how can I add a number to each server entry that
> allows it be to picked more often than the other 20 servers?
>
> For example Server1's weight is 80 and Server2's weight is 40 and
> hence Server1 is more likely to be picked than the others.


You could also do something like:

select serverId from server order by rand() / weight limit 1

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top