473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Choosing a random entry in a table by "weight"

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.

May 27 '07 #1
11 4827
al*********@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.
I dont think myqsl can do that (corect me if im wrong please)

If the weights aren't too large i would solve it something like this:

Offcourse this will get verry ineffective if a=1,b=100000000, etc

link->weight
a->1
b->3
c->2

->select all links
->create a new array $w=(a,b,b,b,c,c)

and voilla
$link=$w[rand(1,count($w))];

--
Arjen
http://www.hondenpage.com - mijn site over honden

May 27 '07 #2
Floortje wrote:

Whoops
$link=$w[rand(0,(count($w)-1))];
May 27 '07 #3
al*********@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

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 27 '07 #4
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

May 28 '07 #5
Rami Elomaa wrote:
al*********@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 :-(

say you have this
a-2
b-1
c-1
d-1

Valid random weight numbers are 1 and 2 (both 50%)

Then your chances of picking A are 62.5% while B,C,D only get a 12.5%
change of being picked (instead of 40-20-20-20)

--
Arjen
www.hondenpage.com
May 28 '07 #6
Floortje kirjoitti:
Rami Elomaa wrote:
>al*********@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".

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 28 '07 #7
Rami Elomaa schreef:
>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".

The op asked for 'weighted'. I really like your solution but it is not
weighted. http://en.wikipedia.org/wiki/Weight_function
--
Arjen
http://www.hondenpage.com
May 28 '07 #8
>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".
The op asked for 'weighted'. I really like your solution but it is not
weighted. http://en.wikipedia.org/wiki/Weight_function

--
Arjen
http://www.hondenpage.com
May 28 '07 #9
al*********@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.
SELECT *, weight*RAND() as result FROM Servers ORDER BY result LIMIT 1
May 29 '07 #10
Alexey Kulentsov wrote:
SELECT *, weight*RAND() as result FROM Servers ORDER BY result LIMIT 1
.... ORDER BY result DESC ...
May 29 '07 #11
On May 29, 12:55 pm, Alexey Kulentsov <criman...@crimaniak.comwrote:
SELECT *, weight*RAND() as result FROM Servers ORDER BY result LIMIT 1
SELECT *, Server_Weight*RAND() as result FROM Servers ORDER BY result
DESC LIMIT 1

Thanks, that works nice.

Jun 7 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Steven Bethard | last post by:
I feel like this has probably been answered before, but I couldn't find something quite like it in the archives. Feel free to point me somewhere if you know where this has already been answered. ...
3
by: Nicolae Fieraru | last post by:
Hi All, I try to build an asp page and I try to execute this sql string: dim weight weight = CLng(Request.Form("Weight") strQ = "SELECT * FROM tbFreightPrices WHERE MinWeight < weight"...
0
by: Brynn | last post by:
I thought since I am building my site a 'Contact Me' page ... I may has well include it in my sites scripts (I am doing this with quite a few other tools of my site as well). It isn't perfect...
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
4
by: Jesse Noller | last post by:
Hello - I'm probably missing something here, but I have a problem where I am populating a list of lists like this: list1 = list2 = list3 = main_list =
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
0
by: Adam | last post by:
Hello, I have a small app I am creating to crawl a directory and check that if it is moved to another a location it's path will not break a character limit. Usually the Windows path limit. ...
15
by: pbd22 | last post by:
Hi. I want to create a tab with a little "active tab" arrow below it to tell the user where they are. An example is here: http://personals.yahoo.com/us/search/dashboard Does anybody know of...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.