關於盲盒開盒相關的流程介紹,請查閱我的另一篇文章
——盲盒想中大獎?還不如去買彩票!!!——從程序的角度揭秘盲盒。
盲盒規則概率 實體類
1 /** 2 * 盲盒規則概率 實體 3 * @author JustJavaIt 4 * @date 2022/01/29 14:18 5 */ 6 @Data 7 public class BlindBoxRule { 8 9 private Long id; 10 11 /** 12 * 盲盒ID 13 */ 14 private Long boxId; 15 16 /** 17 * 盲盒商品類型(1至尊,2超稀有,3稀有,4普通) 18 */ 19 private Integer label; 20 21 /** 22 * 概率 23 */ 24 private Integer realRate; 25 26 public BlindBoxRule(Long id, Long boxId, Integer label, Integer realRate) { 27 this.id = id; 28 this.boxId = boxId; 29 this.label = label; 30 this.realRate = realRate; 31 } 32 33 /** 34 * 根據用戶抽的盲盒id為19,查詢出對應商品類型的概率值。 35 * 補充說明:每個盲盒id都有4種商品類型(1至尊,2超稀有,3稀有,4普通),有不同的概率。每種類型下有不同的商品。 36 * @return 37 */ 38 public static List<BlindBoxRule> queryByBoxId(){ 39 List<BlindBoxRule> boxRuleList = new ArrayList<>(); 40 BlindBoxRule rule1 = new BlindBoxRule(1L, 19L, 1, 200); 41 BlindBoxRule rule2 = new BlindBoxRule(1L, 19L, 2, 600); 42 BlindBoxRule rule3 = new BlindBoxRule(1L, 19L, 3, 3200); 43 BlindBoxRule rule4 = new BlindBoxRule(1L, 19L, 4, 96000); 44 boxRuleList.add(rule1); 45 boxRuleList.add(rule2); 46 boxRuleList.add(rule3); 47 boxRuleList.add(rule4); 48 49 return boxRuleList; 50 } 51 }
盲盒開盒核心算法類
1 /** 2 * 盲盒開盒核心算法 3 * 需求說明:盲盒id為19(19.手機,20.手辦等)中有四種商品類型(1.至尊,2.超稀有,3.稀有,4.普通),至尊抽中的概率為0.02%,超稀有抽中的概率為0.06%,稀有抽中的概率為3.2%,普通抽中的概率為96%; 4 * @author JustJavaIt 5 * @date 2022/01/29 10:47 6 */ 7 public class BlindBoxDraw { 8 public static void main(String[] args) { 9 10 //模擬數據,根據用戶抽的盲盒id=19(前端傳),查詢對應盲盒id下商品類型((1.至尊,2.超稀有,3.稀有,4.普通))的概率 11 List<BlindBoxRule> blindBoxRules = BlindBoxRule.queryByBoxId(); 12 13 //將商品概率添加到集合中,用於下面抽獎 14 List<Integer> probList = new ArrayList<>(blindBoxRules.size()); 15 for (BlindBoxRule rule : blindBoxRules) { 16 probList.add(rule.getRealRate()); 17 } 18 19 //開盒核心方法 20 int drawResult = draw(probList); 21 System.out.println("draw返回值為:" + drawResult); 22 23 //注意List下標從0開始 24 switch (probList.get(drawResult)){ 25 case 200: System.out.println("恭喜你抽中至尊款商品"); return; 26 case 600: System.out.println("恭喜你抽中超稀有款商品"); return; 27 case 3200: System.out.println("恭喜你抽中稀有商品"); return; 28 case 96000: System.out.println("恭喜你抽中普通款商品"); return; 29 default: System.out.println("異常"); 30 } 31 32 //實際業務中根據命中的商品類型((1.至尊,2.超稀有,3.稀有,4.普通))獲得該類型下對應的商品(手機,平板等),每個商品也有一個權重值,也是通過類似上面的draw()抽取商品后,再判斷用戶是否符合一些別的門檻,符合的話就算開箱完成。 33 } 34 35 /** 36 * 大致流程說明:計算出的sortRateList里面的概率有四個,0.0002,0.0006,0.032,0.96,隨機生成的浮點數在0-1內,假如隨機數為0.2546, 37 * 那么sortRateList排序后為0.0002,0.0006,0.032,0.2546,0.96,那么返回的索引sortRateList.indexOf(random)就是4,既抽中的類型為普通款 38 * @param probList 39 * @return 40 */ 41 public static int draw(List<Integer> probList) { 42 //最終加入隨機值后排序的集合 43 List<Double> sortRateList = new ArrayList<>(); 44 45 // 計算概率總和 46 Integer sumRate = 0; 47 for (Integer prob : probList) { 48 sumRate += prob; 49 } 50 51 if (sumRate != 0) { 52 // 概率所占比例 53 double rate = 0D; 54 for (Integer prob : probList) { 55 rate += prob; 56 // 構建一個比例區段組成的集合(避免概率和不為1) 57 sortRateList.add(rate / sumRate); 58 } 59 60 // 隨機生成一個隨機數,並排序 61 ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); 62 //生成0-1之間的Double類型隨機數 eg:0.7570711...... 63 double random = threadLocalRandom.nextDouble(0, 1); 64 System.out.println("生成的隨機數值為:" + random); 65 sortRateList.add(random); 66 Collections.sort(sortRateList); 67 System.out.println("排序后的sortRateList為:" + sortRateList.toString()); 68 69 // 返回該隨機數在比例集合中的索引 70 return sortRateList.indexOf(random); 71 } 72 73 return -1; 74 } 75 }
<END>
⭐️希望本文章對您有幫助,您的「 轉發、點贊 」是我創作的無限動力。
掃描下方二維碼關注微信公眾號,您會收到更多優質文章推送。