上段时间运营的公众号时想做一个抽奖的功能,用户先参与抽奖,等到开奖时间进行开奖,从用户中抽取指定数量的人;
上图中第一个用户就是我们运营的小伙伴,通过她分享参与的人很多,所以她的中奖几率是最大的,最后中奖了。。。
如果只是抽取指定数量的人,很简单,通过MYSQL的SQL语句一句话就可以实现;
但是我的项目期望抽奖用户在参与后分享页面给其他用户,其他用户参与后分享者的中奖概率因子+1,MYSQL就实现不了了。自己动手写了一个工具类,分享给大家。
工具类 AwardUtil.java
package com.fengchen.award;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* 抽奖程序帮助类
* @author FlyerZuo
*
*/
public class AwardUtil {
private static int getRandomNumberInRange(int max) {
int min = 1;
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
private static Map<String,Record> lottery(Map<Integer,Record> map,int totalSize,int awardSize)
{
Map<String,Record> result = new HashMap<String, Record>();
for(int i = 0