1. Algorithm Analysis
The prizes are divided according to probabilities, each interval represents a prize, and then a random number is taken, and a counter-check falls on that interval, which is the prize drawn.
2. Code
Core algorithms
public class Arithmetic {//magnification private static final int mulriple = 1000000; public int Pay (list<prize> prizes) {int lastscope = 0; Shuffle, disrupt prize order 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 (); Divide the interval 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; }//Get a random number between 1-1000000 int luckynumber = new Random (). Nextint (Mulriple); int Luckyprizeid = 0; Find the interval of the random number 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) {//prize inventory minus one} return Luckyprizeid; }}Prize Bean
public class Prize { /** * Prizes only marked */ private Integer Prizeid; /** * Winning probability */ private BigDecimal probability; /** * Prize number */ 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. Testing
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> prizes = new ARRAYLIST<PRIZE&G t; (); 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 ("Number of draws" + times); System.out.println ("Prize1 winning times" + Prize1gettimes); System.out.println ("Prize2 winning times" + Prize2gettimes); System.out.println ("Prize3 Winning Times"+ prize3gettimes); System.out.println ("Prize4 winning times" + Prize4gettimes); System.out.println ("Prize5 winning times" + Prize5gettimes); }}
Results:
Through 1000 extraction, we see that the accuracy of the algorithm is still very high.
Java Lottery algorithm