The examples in this article share with you the Java lottery algorithm for your reference. The specific content is as follows
1. Algorithm Analysis
The prizes are divided into intervals according to the probability. Each interval represents a prize, and then a random number is drawn and the interval is reverse checked, which is the prize drawn.
2. Code
Core algorithm
public class Arithmetic { // magnification private static final int mulriple = 1000000; public int pay(List<Prize> pricings) { int lastScope = 0; // Shuffle, disrupt the prize order Collections.shuffle(prizes); Map<Integer, int[]>> priceScopes = new HashMap<Integer, int[]>(); Map<Integer, Integer> pricingQuantity = new HashMap<Integer, Integer>(); for (Prize price : prizes) { int priceId = price.getPrizeId(); // Partition interval int currentScope = lastScope + price.getProbability().multiply(new BigDecimal(mulriple)).intValue(); priceScopes.put(prizeId, new int[] { lastScope + 1, currentScope }); priceQuantity.put(prizeId, price.getQuantity()); lastScope = currentScope; } // Get a random number between 1-1000000 int luckyNumber = new Random().nextInt(mulriple); int luckyPrizeId = 0; // Find the interval where the random number is located if ((null != priceScopes) && !prizeScopes.isEmpty()) { Set<Entry<Integer, int[]>> entrySets = priceScopes.entrySet(); for (Map.Entry<Integer, int[]> m : entrySets) { int key = m.getKey(); if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1] && priceQuantity.get(key) > 0) { luckyPrizeId = key; break; } } } if (luckyPrizeId > 0) { // Reduce the prize inventory by one} return luckyPrizeId; }} Prize bean
public class Prize { /** * Unique label for prize*/ private Integer prizeId; /** * Probability of winning*/ private BigDecimal probability; /** * Number of prizes*/ 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. Test
Prize1 probability: 5%
Prize2 probability: 10%
Prize3 probability: 15%
Prize4 probability: 20%
Prize5 probability: 50%
public class Test { public static void main(String[] args) { List<Prize> pricings = new ArrayList<Prize>(); Prize price1 = new Prize(); price1.setPrizeId(1); price1.setProbability(new BigDecimal(0.05)); price1.setQuantity(1); prices.add(prize1); Prize price2 = new Prize(); price2.setPrizeId(2); price2.setProbability(new BigDecimal(0.10)); price2.setQuantity(10); prices.add(prize2); Prize price3 = new Prize(); price3.setPrizeId(3); price3.setProbability(new BigDecimal(0.15)); price3.setQuantity(20); prices.add(prize3); Prize price4 = new Prize(); price4.setPrizeId(4); price4.setProbability(new BigDecimal(0.20)); price4.setQuantity(50); prices.add(prize4); Prize price5 = new Prize(); price5.setPrizeId(5); price5.setProbability(new BigDecimal(0.50)); price5.setQuantity(200); prices.add(prize5); int price1GetTimes = 0; int price2GetTimes = 0; int price3GetTimes = 0; int price4GetTimes = 0; int price5GetTimes = 0; int price5GetTimes = 0; Arithmetic arithmetic = new Arithmetic(); int times = 1000; for (int i = 0; i < times; i++) { int priceId = arithmetic.pay(prizes); switch (prizeId) { case 1: price1GetTimes++; break; case 2: price2GetTimes++; break; case 3: price3GetTimes++; break; case 4: price4GetTimes++; break; case 5: price5GetTimes++; break; } } System.out.println("Number of lottery" + times); System.out.println("prize1 wins" + prize1GetTimes); System.out.println("prize2 wins" + prize2GetTimes); System.out.println("prize3 wins" + prize3GetTimes); System.out.println("prize4 wins" + prize4GetTimes); System.out.println("prize5 wins" + prize5GetTimes); }} result:
Through 1000 extractions, we can see that the algorithm accuracy is still very high.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.