The example of this article for everyone to share the Java Lottery algorithm for your reference, the specific contents are as follows
1. Algorithm analysis
according to the probability the prizes are divided into intervals, each interval represents a prize, and then the random number is extracted, and the retrieval falls on that interval, which is the prize.
2. Code
Core algorithm
public class Arithmetic {//magnification private static final int mulriple = 1000000;
public int Pay (list<prize> prizes) {int lastscope = 0;
Shuffle, upset 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 ();
Division 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 1-1000000 of a random number int luckynumber = new Random (). Nextint (Mulriple);
int Luckyprizeid = 0; Find the interval of random numbers if ((null!= prizescopes) &&!prizescopes.isempty ()) {Set<entry<integer, int[]>& Gt 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) {//Trophy inventory minus one} return Luckyprizeid;
}
}
Prize Bean
public class Prize {
/**
* Prize unique Mark * * *
private Integer Prizeid;
/**
* Winning probability * *
private BigDecimal probability;
/**
* Prize quantity * *
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> 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 ("lottery number" + times);
System.out.println ("Prize1 winning number" + Prize1gettimes);
System.out.println ("Prize2 winning number" + Prize2gettimes);
System.out.println ("Prize3 winning number" + Prize3gettimes);
System.out.println ("Prize4 winning number" + Prize4gettimes);
System.out.println ("Prize5 winning number" + Prize5gettimes);
}
}
Results:
Through 1000 extraction, we can see that the accuracy of the algorithm is still very high.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.