通过“权重”选择表中的随机条目。 [英] Choosing a random entry in a table by "weight"

查看:94
本文介绍了通过“权重”选择表中的随机条目。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器的MySQL表,我使用RAND()来选择随机服务器每次使用
,但是如何为每个服务器条目添加一个数字

允许它比其他20台服务器更频繁地选择?


例如Server1的重量是80而Server2的重量是40并且

因此Server1比其他人更有可能被选中。

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.

推荐答案

al ********* @ googlemail.com 写道:

我有一个MySQL服务器表,我用RAND()来选择一个随机服务器
每次使用
,但我怎样才能为每个服务器条目添加一个数字

允许它比其他20台服务器更频繁地选择?


例如Server1的重量是80而Server2的重量是40和

因此Server1比其他人更有可能被选中。
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.



我不认为myqsl可以做到这一点(如果我错了请核心我)


如果重量不是太大我会解决这样的事情:


如果a = 1,b = 100000000等,这将变得非常无效


link-> weight

a-> 1

b-> 3

c-> 2


- >选择所有链接

- >创建一个新数组

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)


和voilla
w=(a,b,b,b,c,c)

and voilla


link =


这篇关于通过“权重”选择表中的随机条目。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆