The logic of the algorithm program of lucky wheel lottery

Posted by ajfton on Mon, 23 Dec 2019 16:21:01 +0100

The logic of the algorithm program of lucky wheel lottery

Recently encountered a demand, to achieve a function similar to a large rotary table lottery, you need to customize the awards, the winning probability of each award, the maximum number of sweepstakes on that day, the cost of sweepstakes, etc. Share a simple java code implementation ideas, there are shortcomings thank you for your correction.

Preliminary method

First define several prizes, such as:

  • iphone 10%
  • 30% chance of winning in 100 yuan shopping volume
  • 50% chance of winning the prize in 10 yuan shopping volume

The total winning rate is 10% + 30% + 50% = 90%

The remaining 10% are thank you for your patronage

Design ideas

This is to allocate all commodities to the array according to the probability

  • A[0] = iphone
  • A[1] = iphone
  • A[2] = iphone
  • ...
  • A[10] = iphone

  • A[11] = 100 yuan shopping volume
  • A[12] = 100 yuan shopping volume
  • ...

Then random a number from 0 to 99, for example, now the random number is 2

Then A[2] is the winning product A[2] = iphone

//Define winning rate denominator%
int probabilityCount = 100;  
String[] prizesId = new String[probabilityCount];  
//Get product list
List<AdPrizeInfo> prizeInfoList = prizeInfoService.getPrizeInfo();  
int num = 0;  
//Cycle all items
for (AdPrizeInfo prize : prizeInfoList) {  
    Integer probability = prize.getOdds();
    //Cycle commodity probability
    for (int i = 0; i < probability; i++) {
        prizesId[num] = prize.getId();
        num ++;
    }
}

//A random number
int index = (int) (Math.random() * probabilityCount);  
//Get random item ID
String prizeId = prizesId[index];  

optimization method

Design ideas

If the above methods are of high probability, they are memory hungry. The sorting and optimization methods are as follows:

Use range algorithm

//Define winning rate denominator%
int probabilityCount = 100;  
//Minimum probability
String min = "min";  
//Maximum probability
String max = "max";  
Integer tempInt = 0;  
//Items to be won
Map<String,Map<String,Integer>> prizesMap = new HashMap<>();  
//Get product list
List<AdPrizeInfo> prizeInfoList = prizeInfoService.getPrizeInfo();  
for (AdPrizeInfo prize : prizeInfoList) {  
    Map<String,Integer> oddsMap = new HashMap<>();
    //Minimum probability
    oddsMap.put(min,tempInt);
    tempInt = tempInt + prize.getOdds();
    //Maximum probability
    oddsMap.put(max,tempInt);
    prizesMap.put(prize.getId(),oddsMap);
}


//Random number
int index = (int) (Math.random() * probabilityCount);  
AdPrizeInfo prizeInfo = null;  
Set<String> prizesIds = prizesMap.keySet();  
for(String prizesId : prizesIds){  
    Map<String,Integer> oddsMap = prizesMap.get(prizesId);
    Integer minNum = oddsMap.get(min);
    Integer maxNum = oddsMap.get(max);
    //Check whether index is in the middle of commodity probability
    if(minNum <= index && maxNum > index){
        prizeInfo = prizeInfoService.selectByPrimaryKey(prizesId);
        break;
    }
}

//If it's empty, it won't win
if(prizeInfo == null){  
    prizeInfo = null;
}

Topics: Java