The demand is to do the lottery function of several small games. There are different lottery rules according to different games, among which there are many commonalities, which can be summed up as follows: only draw according to the proportion of prizes, the proportion of prizes and the number of prizes, and section extraction. For the sake of convenience, these lottery rules are encapsulated into the tool class. The core logic of the lottery is implemented by a discrete algorithm.

**1、 Overview**

The discrete algorithm is used to segment according to the proportion of prizes, and then the corresponding interval of random number matching is generated.

First, define the prize entity class, which has the attributes of prizename (prize name), prizeweight (prize weight) and prizecount (prize quantity). The following is the core code:

```
/**
*Select one item at random in proportion
*@ param list prize list
*@ return type value
*/
public static String ratioExtract(List<Prize> list) {
//Non empty judgment
if (list==null || list.size()<1) {
return null;
}
//The sum of the proportion
double sum=0.00;
//Segmented array (20,30,60)
double[] subArray=new double[list.size()+1];
//The probability is segmented
for (int i = 0; i < list.size(); i++) {
subArray[i]=sum;
//Here, in addition to considering the proportion of prizes, the number of prizes should be calculated by sections
sum+=list.get(i).getPrizeWeight()*list.get(i).getPrizeCount();
}
//Plus take the maximum value
subArray[subArray.length-1]=sum;
/*Generating random numbers*/
Random random=new Random();
double rand = random.nextDouble()*sum;
//Return character
String field=null;
for (int i = 0; i < subArray.length; i++) {
if (i==subArray.length-1) {
return field;
}
if (rand>=subArray[i] && rand<subArray[i+1]) {
field=list.get(i).getPrizeName();
break;
}
}
return field;
}
```

**2、 Testing**

The following is a complete lottery tool class

```
import lombok.Data;
import org.apache.commons.lang.math.RandomUtils;
import java.util.List;
import java.util.Random;
/**
*@ Description: lottery tool class
* @author: xiake
* @Date: 2020/1/5 13:23
* @ModifiedDate：2020/1/5 13:23
* @Copyright: miaoxaike.com
*/
public class PrizeMathRandom {
/**
*Select one item at random in proportion
*@ param fieldarray type value array
*Ratio of @ param proportions to type values
*@ return type value
*/
public static String ratioExtract(String[] fieldArray,double[] proportions) {
//Determine whether the length of two arrays is equal
if(fieldArray.length!=proportions.length) {
Return "the length of two arrays is not equal, unable to execute.";
}
//The sum of the proportion
double sum=0.00;
//Segmented array (20,30,60)
double[] subArray=new double[proportions.length+1];
//The probability is segmented
for (int i = 0; i < proportions.length; i++) {
subArray[i]=sum;
sum+=proportions[i];
}
//Plus take the maximum value
subArray[subArray.length-1]=sum;
Random random=new Random();
/*The interval of generating random number is (0, sum)*/
double rand = random.nextDouble()*sum;
//Return character
String field=null;
for (int i = 0; i < subArray.length; i++) {
if (rand>=subArray[i] && rand<subArray[i+1]) {
field=fieldArray[i];
}
}
return field;
}
/**
*Select one item at random in proportion
*@ param list prize list
*@ return type value
*/
public static String ratioExtract(List<Prize> list) {
//Non empty judgment
if (list==null || list.size()<1) {
return null;
}
//The sum of the proportion
double sum=0.00;
//Segmented array (20,30,60)
double[] subArray=new double[list.size()+1];
//The probability is segmented
for (int i = 0; i < list.size(); i++) {
subArray[i]=sum;
sum+=list.get(i).getPrizeWeight()*list.get(i).getPrizeCount();
}
//Plus take the maximum value
subArray[subArray.length-1]=sum;
/*Generating random numbers*/
Random random=new Random();
double rand = random.nextDouble()*sum;
//Return character
String field=null;
for (int i = 0; i < subArray.length; i++) {
if (i==subArray.length-1) {
return field;
}
if (rand>=subArray[i] && rand<subArray[i+1]) {
field=list.get(i).getPrizeName();
break;
}
}
return field;
}
/**
*Double segmented extraction,
*@ param fieldarray is a segmented array, and the parameter values are assembled with "-" (example: {"6-14", "14-23", "23-32", "32-40"})
*The probability of occurrence of each segment at @ param proportions
*@ return returns a random value within the segmented range after extracting by proportion
*/
public static Integer ratioExtractDouble(String[] fieldArray,double[] proportions) {
String string = ratioExtract(fieldArray,proportions);
String[] split = string.split("-");
int result = RandomUtils.nextInt(Integer.parseInt(split[1]))+Integer.parseInt(split[0]);
return result;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
class Prize{
//Prize name
private String prizeName;
//Percentage of prizes
private double prizeWeight;
//Number of prizes
private int prizeCount;
}
}
```

In addition to the core implementation method, two extended methods are added to meet the rules of the game. Here’s a simple test

```
public static void main(String[] args) {
//Initialize prize information
List<Prize> prizeList=new ArrayList<>();
prizeList.add (New prize ("first prize", 1,1));
prizeList.add (New prize ("second prize", 3,4));
prizeList.add (New prize ("third prize", 6,5));
for (int i = 0; i < 12; i++) {
Prize prize = ratioExtract(prizeList);
if (prize!=null){
System.out.println ("the first" + (I + 1) + "time, draw"+ prize.getPrizeName () + "number of remaining prizes ="+ prize.getPrizeCount ());
}else {
System.out.println ("the first" + (I + 1) + "times, the prize has been drawn");
}
}
}
```

The operation effect is as follows

The implementation method is very simple, there may be some unreasonable places, but it is enough to meet the current needs. Basically, the use of arrays and random numbers will not be explained in detail. If you have any questions, please leave a message in the comment area!

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.