当前位置:   article > 正文

java实现权重算法

java权重算法

一、简单介绍

如有4个元素A、B、C、D,权重分别为1、2、3、4,随机结果中A:B:C:D的比例要为1:2:3:4。
总体思路:累加每个元素的权重A(1)-B(3)-C(6)-D(10),则4个元素的的权重管辖区间分别为[0,1)、[1,3)、[3,6)、[6,10)。然后随机出一个[0,10)之间的随机数。
落在哪个区间,则该区间之后的元素即为按权重命中的元素。

二、核心代码

  1. public static String getWeight(List<WeightCategory> categorys) {
  2. Integer weightSum = 0;
  3. String result=null;
  4. for (WeightCategory wc : categorys) {
  5. weightSum += wc.getWeight();
  6. }
  7. if (weightSum <= 0) {
  8. System.err.println("Error: weightSum=" + weightSum.toString());
  9. return result;
  10. }
  11. Random random = new Random();
  12. Integer n = random.nextInt(weightSum); // n in [0, weightSum)
  13. Integer m = 0;
  14. for (WeightCategory wc : categorys) {
  15. if (m <= n && n < m + wc.getWeight()) {
  16. result=wc.getCategory();
  17. break;
  18. }
  19. m += wc.getWeight();
  20. }
  21. return result;
  22. }

三、完整实例

  1. public class WeightTest {
  2. public static void main(String[] args){
  3. //测试数据
  4. List<WeightCategory> categoryList=new ArrayList<>();
  5. WeightCategory weightCategory1=new WeightCategory("一等奖",10);
  6. WeightCategory weightCategory2=new WeightCategory("二等奖",20);
  7. WeightCategory weightCategory3=new WeightCategory("三等奖",30);
  8. WeightCategory weightCategory4=new WeightCategory("四等奖",40);
  9. categoryList.add(weightCategory1);
  10. categoryList.add(weightCategory2);
  11. categoryList.add(weightCategory3);
  12. categoryList.add(weightCategory4);
  13. String result="";
  14. int a1=0,a2=0,a3=0,a4=0;
  15. for (int i=0;i<100;i++){
  16. result = getWeight(categoryList);
  17. System.out.println(i+" 开奖结果: "+result);
  18. if(result.equals("一等奖")){
  19. a1++;
  20. }
  21. else if(result.equals("二等奖")){
  22. a2++;
  23. }
  24. else if(result.equals("三等奖")){
  25. a3++;
  26. }
  27. else if(result.equals("四等奖")){
  28. a4++;
  29. }
  30. }
  31. System.out.println("一等奖共出现 "+a1);
  32. System.out.println("二等奖共出现 "+a2);
  33. System.out.println("三等奖共出现 "+a3);
  34. System.out.println("四等奖共出现 "+a4);
  35. }
  36. /**
  37. * 权重获取方法
  38. * @param categorys
  39. * @return
  40. */
  41. public static String getWeight(List<WeightCategory> categorys) {
  42. Integer weightSum = 0;
  43. String result=null;
  44. for (WeightCategory wc : categorys) {
  45. weightSum += wc.getWeight();
  46. }
  47. if (weightSum <= 0) {
  48. System.err.println("Error: weightSum=" + weightSum.toString());
  49. return result;
  50. }
  51. Random random = new Random();
  52. Integer n = random.nextInt(weightSum); // n in [0, weightSum)
  53. Integer m = 0;
  54. for (WeightCategory wc : categorys) {
  55. if (m <= n && n < m + wc.getWeight()) {
  56. result=wc.getCategory();
  57. break;
  58. }
  59. m += wc.getWeight();
  60. }
  61. return result;
  62. }
  63. }
  64. class WeightCategory{
  65. private String category;//类别
  66. private int weight;//权重值
  67. public WeightCategory(String category, int weight) {
  68. this.category = category;
  69. this.weight = weight;
  70. }
  71. public String getCategory() {
  72. return category;
  73. }
  74. public void setCategory(String category) {
  75. this.category = category;
  76. }
  77. public int getWeight() {
  78. return weight;
  79. }
  80. public void setWeight(int weight) {
  81. this.weight = weight;
  82. }
  83. }

四、改进版

现在对每个奖项设置数量限制:
第一步,改进WeightCategory实体

  1. private String category;//类别
  2. private int weight;//权重值
  3. private int maxNum;//最大出现次数


第二步,修改main方法进行测试

  1. public static void main(String[] args){
  2. List<WeightCategory> categoryList=new ArrayList<>();
  3. WeightCategory weightCategory1=new WeightCategory("一等奖",10,10);
  4. WeightCategory weightCategory2=new WeightCategory("二等奖",20,20);
  5. WeightCategory weightCategory3=new WeightCategory("三等奖",30,30);
  6. WeightCategory weightCategory4=new WeightCategory("四等奖",40,40);
  7. categoryList.add(weightCategory1);
  8. categoryList.add(weightCategory2);
  9. categoryList.add(weightCategory3);
  10. categoryList.add(weightCategory4);
  11. String result="";
  12. int a1=0,a2=0,a3=0,a4=0;
  13. //抽奖次数120次,观察结果
  14. for (int i=0;i<120;i++){
  15. result = getWeight(categoryList);
  16. System.out.println(i+" 开奖结果: "+result);
  17. if(result.equals("一等奖")){
  18. a1++;
  19. weightCategory1.setMaxNum(weightCategory1.getMaxNum()-1);
  20. }
  21. else if(result.equals("二等奖")){
  22. a2++;
  23. weightCategory2.setMaxNum(weightCategory2.getMaxNum()-1);
  24. }
  25. else if(result.equals("三等奖")){
  26. a3++;
  27. weightCategory3.setMaxNum(weightCategory3.getMaxNum()-1);
  28. }
  29. else if(result.equals("四等奖")){
  30. a4++;
  31. weightCategory4.setMaxNum(weightCategory4.getMaxNum()-1);
  32. }
  33. if(weightCategory1.getMaxNum()==0){
  34. System.out.println("一等奖抽奖结束");
  35. weightCategory1.setMaxNum(-1);
  36. categoryList.remove(weightCategory1);
  37. }
  38. if(weightCategory2.getMaxNum()==0){
  39. System.out.println("二等奖抽奖结束");
  40. weightCategory2.setMaxNum(-1);
  41. categoryList.remove(weightCategory2);
  42. }
  43. if(weightCategory3.getMaxNum()==0){
  44. System.out.println("三等奖抽奖结束");
  45. weightCategory3.setMaxNum(-1);
  46. categoryList.remove(weightCategory3);
  47. }
  48. if(weightCategory4.getMaxNum()==0){
  49. System.out.println("四等奖抽奖结束");
  50. weightCategory4.setMaxNum(-1);
  51. categoryList.remove(weightCategory4);
  52. }
  53. }
  54. System.out.println("一等奖共出现 "+a1);
  55. System.out.println("二等奖共出现 "+a2);
  56. System.out.println("三等奖共出现 "+a3);
  57. System.out.println("四等奖共出现 "+a4);
  58. }

 

转载于:https://my.oschina.net/u/3387320/blog/2961404

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

闽ICP备14008679号