当前位置:网站首页>C# 均衡权重分配
C# 均衡权重分配
2022-06-10 12:10:00 【游子吟i】
模拟实体类:
class Server
{
public String Ip_Port { get; set; }
public int weightip { get; set; }
public override string ToString()
{
Console.WriteLine($"Ip_Port={Ip_Port},weightip={weightip}");
return base.ToString();
}
}模拟数据源:
List<Server> servers = new List<Server>
{
new Server
{
Ip_Port="asdas:1223",
weightip=2
},
new Server
{
Ip_Port="tegwer:154782",
weightip=1
},
new Server
{
Ip_Port="kjhvd:41201",
weightip=3
},
new Server
{
Ip_Port="errtqwe:412177",
weightip=9
},
new Server
{
Ip_Port="hjyhdf:7878",
weightip=7
}
};主要代码:
int sum = 0;
var rand = new Random();
int num = rand.Next(1, servers.Select(t => t.weightip).Sum());
foreach (var item in servers)
{
sum += item.weightip;
if (num > sum)
continue;
Console.WriteLine(item.ToString());
break;
}10次模拟结果:
int n = 0;
var rand = new Random();
while (n++<10)
{
int sum = 0;
int num = rand.Next(1, servers.Select(t => t.weightip).Sum());
foreach (var item in servers)
{
sum += item.weightip;
if (num > sum)
continue;
Console.WriteLine(item.ToString());
break;
}
}
边栏推荐
猜你喜欢

(八)初始化列表

交出 Firehouse 数据库访问权:推特准备满足马斯克要求

425万预算招标:江苏电信Oracle、TeleDB、TelePG等核心系统数据库驻场维保服务集中采购

国际档案日|瀚高数据库以科技赋能数字档案建设

Remote access to tensorboard

2022 CISCN初赛 Satool

4.25 million budget bidding: centralized procurement of onsite maintenance services for Oracle, teledb, telepg and other core system databases of Jiangsu Telecom

绿盟数据库防火墙(DAS-FW)获得鲲鹏Validated认证

ShaderGraph——水面

【已解决】vagrant up下载box速度太慢的解决方法
随机推荐
list.remove(index)返回flase,移除失败
[cloud picture] every successful business system is inseparable from apig
4.25 million budget bidding: centralized procurement of onsite maintenance services for Oracle, teledb, telepg and other core system databases of Jiangsu Telecom
Xijun data cooperates with Alibaba cloud to obtain Alibaba cloud product integration certification!
【限时】,干货满满
合集资料,必看
【总结向】个人赛补题 POJ - 3041 Asteroids &CodeForces - 173B Chamber of Secrets
vs2010未定义的标识符 round
Dom4j parsing XML
搜狐员工遭遇工资补助诈骗 黑产与灰产有何区别 又要如何溯源?
MAX3051的can芯片的学习
C# 实现气泡窗体
2022 CISCN 初赛pwn完整wp
Const Modified member function
Clip usage
Introduction to the library of C language best practices (Part 1)
425万预算招标:江苏电信Oracle、TeleDB、TelePG等核心系统数据库驻场维保服务集中采购
Transfomer self implementation and official library
Unity打字机,使文本自动滚至文本框底部
【雲圖說】每個成功的業務系統都離不開APIG的保駕護航