Big Wheel Draw

Get into the habit of writing together! This is the 11th day that I participated in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Big Wheel Draw

Sweepstakes is a commonly used marketing tool. The lottery activity has been in existence for many years and has been adopted by all walks of life because it fits people's consumption psychology. For consumers, "taking advantage" is the eternal pursuit, and "small and big" also satisfies customers' pursuit of excitement. Moreover, compared with other forms of activities, the big turntable lottery activity is simple to operate, highly interesting, attracts users with a wide range of prizes, and has a high degree of participation.

The carousel lottery is often used to submit forms, and the lottery draws directional drainage. Pay to complete the order, draw a lottery to give probabilistic rewards, or directional drainage.

lottery algorithm

The basic idea of ​​the lottery algorithm is to randomize a number and see which range the random number falls in.

There are three prizes A, B and C, the probability is 10, 20 and 30 in 10,000 respectively. Then the probability of thanking you for participating in D (also as a prize) is 10000-10-20-30;

0__ 10 __ 30__60__10000

the winning range

A: [0,10]

B: [10,30]

C: [30,60]

D: [60,10000]

random A random number from 0-10000, which range falls in, that is, which prize is won.

implementation code

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @author lvzihai
 * @date 2022/4/15
 **/
public class LotteryA {


  private static int draw(){
    int totalCount = 10000;
    int pa = 10;
    int pb = 20;
    int pc = 30;
    // 谢谢参与
    int pd = totalCount -pa-pb-pc;
    int[] gifts = new int[]{pa,pb,pc,pd};

    int[] giftRange = new int[gifts.length];
    int sum = 0;
    for (int i = 0; i < gifts.length; i++) {
      sum+=gifts[i];
      giftRange[i] = sum;
    }
    StrUtil s;

    int randomVal = RandomUtil.randomInt(totalCount);
    for (int i = 0; i < giftRange.length; i++) {
      if(randomVal<giftRange[i]){
        return i;
      }
    }

    return -1;
  }

  public static void main (String[] args) {
    Map<Integer,Integer> map = new HashMap<>();
    int totalCount = 100000;
    for (int i = 0; i < totalCount; i++) {
      int index = draw();
      Integer val = map.get(index);
      val = val==null?1:val+1;
      map.put(index,val);
    }
    // 统计概率 万分之X
    for (Entry<Integer, Integer> entry : map.entrySet()) {
      System.out.println(entry.getKey()+" "+10000.0*entry.getValue()/totalCount);
    }
  }
}
复制代码

Algorithms whose total probability is not 1

For improved algorithms whose total probability is not 1. For example, probability: A: 0.1 B: 0.2, C: 0.3, D: 0.6, sum all the probabilities to get the total probability, and then calculate the relative probability of each item.

将随机值添加到序列并排序,通过indexOf来获取随机值所在的区间

List<Double> orignalRates = Arrays.asList(0.1,0.2,0.3,0.6);

public static int lottery (List<Double> orignalRates) {
    if (orignalRates == null || orignalRates.isEmpty()) {
      return -1;
    }

    int size = orignalRates.size();

    // 计算总概率,这样可以保证不一定总概率是1
    double sumRate = 0d;
    for (double rate : orignalRates) {
      sumRate += rate;
    }

    // 计算每个物品在总概率的基础下的概率情况
    List<Double> sortOrignalRates = new ArrayList<Double>(size);
    Double tempSumRate = 0d;
    for (double rate : orignalRates) {
      tempSumRate += rate;
      sortOrignalRates.add(tempSumRate / sumRate);
    }

    // 根据区块值来获取抽取到的物品索引
    double nextDouble = Math.random();
    // 将随机值添加到序列并排序,通过indexOf来获取随机值所在的区间
    sortOrignalRates.add(nextDouble);
    Collections.sort(sortOrignalRates);

    return sortOrignalRates.indexOf(nextDouble);
  }
复制代码
Summarize

The overall idea of ​​the lottery algorithm is to arrange the awards in order, thank you for participating as an award, the order of the arrangement is irrelevant, and then randomize a random number to see which interval the random number falls in, that is, which award. For prizes in stock, the stock can be calculated separately, if the stock is used up, it will be returned directly. Thank you for participating.

Guess you like

Origin juejin.im/post/7087015478115172388