Java抽獎算法第二例。本站提示廣大學習愛好者:(Java抽獎算法第二例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java抽獎算法第二例正文
本文實例為年夜家分享了java抽獎算法,供年夜家參考,詳細內容以下
1. 算法剖析
依據幾率將獎品劃分區間,每一個區間代表一個獎品,然後抽取隨機數,反查落在誰人區間上,即為所抽取的獎品。
2. 代碼
焦點算法
public class Arithmetic { // 縮小倍數 private static final int mulriple = 1000000; public int pay(List<Prize> prizes) { int lastScope = 0; // 洗牌,打亂獎品順序 Collections.shuffle(prizes); Map<Integer, int[]> prizeScopes = new HashMap<Integer, int[]>(); Map<Integer, Integer> prizeQuantity = new HashMap<Integer, Integer>(); for (Prize prize : prizes) { int prizeId = prize.getPrizeId(); // 劃分區間 int currentScope = lastScope + prize.getProbability().multiply(new BigDecimal(mulriple)).intValue(); prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope }); prizeQuantity.put(prizeId, prize.getQuantity()); lastScope = currentScope; } // 獲得1-1000000之間的一個隨機數 int luckyNumber = new Random().nextInt(mulriple); int luckyPrizeId = 0; // 查找隨機數地點的區間 if ((null != prizeScopes) && !prizeScopes.isEmpty()) { Set<Entry<Integer, int[]>> entrySets = prizeScopes.entrySet(); for (Map.Entry<Integer, int[]> m : entrySets) { int key = m.getKey(); if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1] && prizeQuantity.get(key) > 0) { luckyPrizeId = key; break; } } } if (luckyPrizeId > 0) { // 獎品庫存減一 } return luckyPrizeId; } }
Prize bean
public class Prize { /** * 獎品獨一標示 */ private Integer prizeId; /** * 中獎幾率 */ private BigDecimal probability; /** * 獎品數目 */ private Integer quantity; public Integer getPrizeId() { return prizeId; } public void setPrizeId(Integer prizeId) { this.prizeId = prizeId; } public BigDecimal getProbability() { return probability; } public void setProbability(BigDecimal probability) { this.probability = probability; } public Integer getQuantity() { return quantity; } public void setQuantity(Integer quantity) { this.quantity = quantity; } }
3. 測試
prize1幾率: 5%
prize2幾率: 10%
prize3幾率: 15%
prize4幾率: 20%
prize5幾率: 50%
public class Test { public static void main(String[] args) { List<Prize> prizes = new ArrayList<Prize>(); Prize prize1 = new Prize(); prize1.setPrizeId(1); prize1.setProbability(new BigDecimal(0.05)); prize1.setQuantity(1); prizes.add(prize1); Prize prize2 = new Prize(); prize2.setPrizeId(2); prize2.setProbability(new BigDecimal(0.10)); prize2.setQuantity(10); prizes.add(prize2); Prize prize3 = new Prize(); prize3.setPrizeId(3); prize3.setProbability(new BigDecimal(0.15)); prize3.setQuantity(20); prizes.add(prize3); Prize prize4 = new Prize(); prize4.setPrizeId(4); prize4.setProbability(new BigDecimal(0.20)); prize4.setQuantity(50); prizes.add(prize4); Prize prize5 = new Prize(); prize5.setPrizeId(5); prize5.setProbability(new BigDecimal(0.50)); prize5.setQuantity(200); prizes.add(prize5); int prize1GetTimes = 0; int prize2GetTimes = 0; int prize3GetTimes = 0; int prize4GetTimes = 0; int prize5GetTimes = 0; Arithmetic arithmetic = new Arithmetic(); int times = 1000; for (int i = 0; i < times; i++) { int prizeId = arithmetic.pay(prizes); switch (prizeId) { case 1: prize1GetTimes++; break; case 2: prize2GetTimes++; break; case 3: prize3GetTimes++; break; case 4: prize4GetTimes++; break; case 5: prize5GetTimes++; break; } } System.out.println("抽獎次數" + times); System.out.println("prize1中獎次數" + prize1GetTimes); System.out.println("prize2中獎次數" + prize2GetTimes); System.out.println("prize3中獎次數" + prize3GetTimes); System.out.println("prize4中獎次數" + prize4GetTimes); System.out.println("prize5中獎次數" + prize5GetTimes); } }
成果:
經由過程1000次抽取,我們看出算法精度照樣很高的。
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。