private Map<String,Integer> serverMap = new HashMap<String,Integer>(){{ put("192.168.1.100",1); put("192.168.1.101",1); put("192.168.1.102",4); put("192.168.1.103",1); put("192.168.1.104",1); put("192.168.1.105",3); put("192.168.1.106",1); put("192.168.1.107",2); put("192.168.1.108",1); put("192.168.1.109",1); put("192.168.1.110",1); }}; private List<String> servers = new ArrayList<>(serverMap.keySet());
public void weightRandom(){ Set<String> keySet = serverMap.keySet(); List<String> servers = new ArrayList<String>(); for(Iterator<String> it = keySet.iterator();it.hasNext();){ String server = it.next(); int weight = serverMap.get(server); for(int i=0;i<weight;i++){ servers.add(server); } } String server = null; Random random = new Random(); int idx = random.nextInt(servers.size()); server = servers.get(idx); System.out.println(server); }
public String randomWeight(){ int length = serverMap.size(); // 總個數 int totalWeight = 0; // 總權重 boolean sameWeight = true; // 權重是否都同樣 for (int i = 0; i < length; i++) { int weight = serverMap.get(servers.get(i)); totalWeight += weight; // 累計總權重 if (sameWeight && i > 0 && weight != serverMap.get(servers.get(i-1))) { sameWeight = false; // 計算全部權重是否同樣 } } if (totalWeight > 0 && ! sameWeight) { // 若是權重不相同且權重大於0則按總權重數隨機 int offset = ThreadLocalRandom.current().nextInt(totalWeight); // 並肯定隨機值落在哪一個片段上 for (int i = 0; i < length; i++) { offset -= serverMap.get(servers.get(i)); if (offset < 0) { return servers.get(i); } } } // 若是權重相同或權重爲0則均等隨機 return servers.get(ThreadLocalRandom.current().nextInt(length)); }
想獲取最新內容,請關注微信公衆號git