当前位置:   article > 正文

java权重比例分配计算_java按比例分配算法

java按比例分配算法
  1. int number = 1000;//假设1000个订单数
  2. Double[] weight = new Double[]{0D,2D,2D};
  3. //已分配订单数
  4. Integer[] count = new Integer[weight.length];
  5. for (int i = 0; i < number; i++) {
  6. //当前权重
  7. Double[] current = new Double[weight.length];
  8. for (int w = 0; w < weight.length; w++) {
  9. current[w] = weight[w] / (count[w] == null ? 1 : count[w]);
  10. }
  11. int index = 0;
  12. Double currentMax = current[0];
  13. for (int d = 1; d < current.length; d++) {
  14. //考虑全等的情况
  15. Boolean isTrue = true;
  16. while (isTrue) {
  17. Set set = new HashSet();
  18. for (Double c : current) {
  19. set.add(c);
  20. }
  21. if (set.size() == 1) {//代表全等
  22. for (int e = 0; e < current.length; e++) {
  23. current[e] = current[e] * Math.random();
  24. }
  25. } else {
  26. isTrue = false;
  27. }
  28. }
  29. //比较所有的数,寻找出下标最大的哪一位
  30. if (currentMax < current[d]) {
  31. currentMax = current[d];
  32. index = d;
  33. }
  34. }
  35. count[index] = count[index] == null ? 1 : count[index] + 1;
  36. }
  37. for (Integer i : count) {
  38. System.err.println(i);
  39. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/289159
推荐阅读
相关标签
  

闽ICP备14008679号