Reply to Re: Choosing a random entry in a table by "weight"

Your name:

Reply:


Posted by Rami Elomaa on 05/28/07 13:23

Floortje kirjoitti:
> Rami Elomaa wrote:
>> alpha.beta0@googlemail.com 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
>
> Nice .. real nice but wrong :-(

I don't know what would make it particularily "wrong", it does basicly
what it should, but the distribution of percentages is different. How
would I know what is "wrong" and what "right", so I just say it's
"different". The original question does not define exactly what is "more
often than the others".

--
Rami.Elomaa@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация