/** * 随机项 * @author healy * * @param <E> */ public class RandomEntry<E> { /**概率,单位%%(万分之)*/ private int probability; /**概率对应的物品*/ private E entry; public RandomEntry(){ } /** * * @param p 概率单位%%(取值范围:[0-10000)) * @param entry */ public RandomEntry(int p, E entry) { this.probability = p; this.entry = entry; } /** * * @param p 小数(取值范围:[0-1)) * @param entry */ public RandomEntry(double p, E entry) { this.probability = (int)(p*RandomInstance.maxBaseInt); this.entry = entry; } public int getProbability() { return probability; } public void setProbability(int probability) { this.probability = probability; } public E getEntry() { return entry; } public void setEntry(E entry) { this.entry = entry; } @Override public String toString() { return "RandomEntry [probability=" + probability + ", entry=" + entry + "]"; } }
1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Random; 4 5 /** 6 * 随机实例,应用于一个随机事件场景 7 * 8 * @author healy 9 * 10 */ 11 public class RandomInstance<E> 12 { 13 14 Random r = new Random(); 15 16 /** 总概率值 */ 17 int totalProbability = 0; 18 19 public final static int maxBaseInt = 10000; 20 21 /** 22 * 随机项列表 23 */ 24 List<RandomEntry<?>> reList = new ArrayList<RandomEntry<?>>(); 25 26 ConditionController<?> cc; 27 28 public RandomInstance() 29 { 30 31 } 32 33 public RandomInstance(ConditionController<?> cc) 34 { 35 this.cc = cc; 36 37 } 38 39 /** 40 * 根据随机概率获取对应的实例 41 * 42 * @return 43 */ 44 public E getRandomEntry() 45 { 46 E retObj = null; 47 48 int randomValue = r.nextInt(totalProbability); 49 50 int curPoint = 0; 51 52 int retry = 0; 53 54 for (int i = 0; i < reList.size(); i++) 55 { 56 RandomEntry<?> re = reList.get(i); 57 // LogUtil.info("i:" + i + "re:" + re + "curPoint:" + curPoint + "totalProbability:" + totalProbability 58 // + "retry:" + retry); 59 if (randomValue >= curPoint && randomValue < curPoint + re.getProbability()) 60 { 61 if (cc != null && cc.rejectByCondition(re)) 62 { 63 // 如果当前概率物品已经达到最大限制,则跳过 64 randomValue = (randomValue + re.getProbability()) % totalProbability; 65 if (++retry >= reList.size()) 66 {// 最大跳过次数为列表长度 67 break; 68 } 69 } 70 else 71 { 72 retObj = (E)re.getEntry(); 73 if (cc != null) 74 { 75 // 更新条件值 76 cc.updateConditionValue(re); 77 } 78 break; 79 } 80 } 81 else 82 { 83 curPoint += re.getProbability(); 84 } 85 } 86 87 return retObj; 88 89 } 90 91 public void addRandomEntry(RandomEntry<?>... entries) 92 { 93 int total = 0; 94 if (entries.length > 0) 95 { 96 97 // 遍历参数内容 98 for (RandomEntry<?> re : entries) 99 { 100 total += re.getProbability(); 101 reList.add(re); 102 } 103 totalProbability += total; 104 } 105 } 106 107 public void removeRandomEntry(RandomEntry<?>... entries) 108 { 109 int total = 0; 110 if (entries.length > 0) 111 { 112 113 // 遍历参数内容 114 for (RandomEntry<?> re : entries) 115 { 116 total += re.getProbability(); 117 reList.remove(re); 118 } 119 totalProbability -= total; 120 } 121 } 122 123 @Override 124 public String toString() 125 { 126 return "RandomInstance [r=" + r + ", totalProbability=" + totalProbability + ", reList=" + reList + ", cc=" 127 + cc + "]"; 128 } 129 130 }
1 RandomInstance<Integer> randomIns = new RandomInstance<Integer>(); 2 3 for (Mine_reward info : rewardList) { 4 5 RandomEntry<Integer> re = new RandomEntry<Integer>(info.getWeight(), info.getRewardId()); 6 7 randomIns.addRandomEntry(re); 8 } 9 10 int rewardId = randomIns.getRandomEntry();