【功能實現】實現設置概率的隨機數
阿新 • • 發佈:2018-03-09
private wan cti 就是 common getch integer turn num
1.故事背景
生成一個隨機紅包,紅包的數值0.1元-100元不等,其具體概率為:0.1元為最小單位,0.1元到0.5元的概率為40%,0.5元到1元的概率為50%,1元-2元的概率為5%,2元-3元的概率為3%,3元-4元的概率為1%,4元-5元概率為0.99%,5元-100元的概率為0.01%。
2.思考過程
其實問題很簡單,把概率放到一條線段上,我們先把問題簡單化,假設隨機生成0的概率是40%, 生成1的概率是50%,生成2的概率為5%,生成3的概率是3%...,線段上的0.4/0.9/0.95都是計算上兩個概率後的和,那麽如果我們此時生成一個隨機數n,他的隨機 的區間是0到1,如果生成0.2,那麽就會落到0-0.4的概率區間中,如果是0.7就是 0.4-0.9的概率區間中,符合問題邏輯。
3.實現方式
回到剛開始的故事,就是把第一層的概率邏輯拿過來,然後具體區間中生成的紅包數額再進行一次隨機數的生成,所以我設計了一個CommonRandom類來存放紅包區間的最小值和最大值,還有個該區間的概率,
CommonRandom類
8 /** 9 * @author wangjun 10 * @Date 2018/3/6 11 */ 12 public class CommonRandom { 13 private Double min; 14 private Double max; 15 private Double chance; 16 privateDouble calculateRate; 17 18 private transient Integer reality = 0; 19 private transient Double realityRate; 20 21 public static CommonRandom getInstance(Double min, Double max, Double rate) { 22 CommonRandom commonRandom = new CommonRandom(); 23 commonRandom.setMin(min);24 commonRandom.setMax(max); 25 commonRandom.setChance(rate); 26 return commonRandom; 27 } 28 29 public static String getDefault() { 30 return JsonUtils.toJson(getCommonRandom()); 31 } 32 33 public Double getMin() { 34 return min; 35 } 36 37 private void setMin(Double min) { 38 this.min = min; 39 } 40 41 public Double getMax() { 42 return max; 43 } 44 45 private void setMax(Double max) { 46 this.max = max; 47 } 48 49 public Double getChance() { 50 return chance; 51 } 52 53 private void setChance(Double chance) { 54 this.chance = chance; 55 } 56 57 public Double getCalculateRate() { 58 return calculateRate; 59 } 60 61 public void setCalculateRate(Double calculateRate) { 62 this.calculateRate = calculateRate; 63 } 64 65 public Integer getReality() { 66 return reality; 67 } 68 69 public void setReality(Integer reality) { 70 this.reality = reality; 71 } 72 73 public Double getRealityRate() { 74 return realityRate; 75 } 76 77 public void setRealityRate(Double realityRate) { 78 this.realityRate = realityRate; 79 } 80 81 private static List<CommonRandom> getCommonRandom() { 82 List<CommonRandom> commonRandomList = Lists.newArrayList(); 83 commonRandomList.add(CommonRandom.getInstance(0.1, 0.5, 0.4)); 84 commonRandomList.add(CommonRandom.getInstance(0.5, 1.0, 0.5)); 85 commonRandomList.add(CommonRandom.getInstance(1.0, 2.0, 0.05)); 86 commonRandomList.add(CommonRandom.getInstance(2.0, 3.0, 0.03)); 87 commonRandomList.add(CommonRandom.getInstance(3.0, 4.0, 0.01)); 88 commonRandomList.add(CommonRandom.getInstance(4.0, 5.0, 0.0099)); 89 commonRandomList.add(CommonRandom.getInstance(5.0, 100.0, 0.0001)); 90 return commonRandomList; 91 } 92 } 93 94 CommonRandom
生成randomUtil
8 /** 9 * @author wangjun 10 * @Date 2018/3/6 11 */ 12 public class RandomUtil { 13 /** 14 * 保留小數位數 15 */ 16 private static final int POW = (int) Math.pow(10, 3); 17 private static final double ERROR_RANDOM_RESULT = -1D; 18 19 public static Double randomRedPackMoney() { 20 List<CommonRandom> commonRandoms = CommonRandom.getCommonRandom(); 21 Double result = random(commonRandoms); 22 if (result == ERROR_RANDOM_RESULT) { 23 return 1; 24 } 25 return result 26 } 27 28 /** 29 * 返回指定概率生成的隨機數 30 * 傳入集合中的chance概率和應為1 31 * @param commonRandomList 配置生成隨機數概率及區間 32 * @return 隨機數 33 */ 34 public static Double random(List<CommonRandom> commonRandomList) { 35 if (CollectionUtils.isEmpty(commonRandomList)) { 36 return ERROR_RANDOM_RESULT; 37 } 38 double randomNumber = Math.random() * getLastRate(commonRandomList); 39 for (CommonRandom item : commonRandomList) { 40 if (randomNumber < item.getCalculateRate()) { 41 return getRandomNumber(item.getMax(), item.getMin()); 42 } 43 } 44 return ERROR_RANDOM_RESULT; 45 } 46 47 private static double getLastRate(List<CommonRandom> commonRandomList) { 48 return commonRandomList.get(commonRandomList.size() - 1).getCalculateRate(); 49 } 50 51 private static Double getRandomNumber(Double max, Double min) { 52 return Math.floor((Math.random() * (max - min) + min) * POW) / POW; 53 } 54 }
【功能實現】實現設置概率的隨機數