A simple idea of java to realize the big turntable lottery

Big Wheel Draw


A requirement I encountered recently, to implement a function similar to the big turntable lottery, it is necessary to customize the awards, the probability of winning each award, the maximum number of lottery draws on the day, the cost of the lottery, etc. Share the idea of ​​​​a simple java code implementation, and thank you for your corrections.


Design ideas

1. Prize probability

The winning probability of each prize is independent, and it is not necessary to consider whether the sum of the probability is equal to 1, that is, the setting method that the probability of prize A is 100% and the probability of prize B is 80% can be established;

2. The lottery method

  • Take out the list of prizes (arranged in reverse order according to the probability of winning)
  • Generate a random number of 0-1 through java's Random() method, and compare it with the set prize probability cyclically
  • If the random number is less than the probability value, the prize will be drawn
  • In other words, according to the random numbers, the probability is from small to large, and the winning is in turn.

For example: the probability of A award is 100%, the probability of B award is 30%, the probability of C award is 1%, the user clicks the lottery, the random number is 0.2, at this time, compare the C award first, (0.2<0.01)=false, then enter the next award Judgment, (0.2<0.3)=true, it is judged that the user has won the lottery B, and jumped out of the loop;

3. Prize Handling

Generally speaking, the prize settings are nothing more than the following types:

  • Account balance bonus;
  • cash reward();
  • bonus points;
  • Virtual item rewards (props, members, etc. are all classified into this category)
  • Physical rewards (valuable items, or commodities that need logistics, etc.)
    Therefore, it is recommended to write a "winner processor" factory class, and use different implementation classes to implement the winning processing logic of different types of prizes.

4. Other

Some unimportant but necessary precautions, such as maintenance of lottery costs, login verification, and account balance judgment, are determined according to project needs.

core code

1. Award settings (mostly simple CUID operations, if the relationship in the project is complex, please think about the relationship, and I will not repeat it here)

2. Generate random numbers (here for the convenience of data processing *100)

        Random randomTool = new Random();
        Double userSelect = randomTool.nextDouble()*100;
        Award award = awardManager.duageAward(userSelect);

3. The duageAward method uses a very simple method to compare the probability and the size of the random number to judge the winning

        for (Award award : awardList) {
            if(userSelect < award.getProbability()){
                return award;
            }
        }

4. Prize distribution processing

  • Prize distribution factory

    public final  class SendAwardProcessorFactory {
        public static ISendAwardProcessor getProcessor(String awardType){
            if("valuables".equals(awardType)){//贵重物品类奖品流程
                return SpringContextHolder.getBean("awardForValuablesProcessor");
            }
            return null;
        }
    }
    通过一个awardType的参数来判断奖品类型,并创建一个处理方式的实例,每一个Processor都是一个处理接口,通过实现类来最终处理奖品发放业务
    

There is only one idea adopted by the author here. As a note and communication, there is a better way to deal with it. Welcome to correct me.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324889675&siteId=291194637