上段时间运营的公众号时想做一个抽奖的功能,用户先参与抽奖,等到开奖时间进行开奖,从用户中抽取指定数量的人;
上图中第一个用户就是我们运营的小伙伴,通过她分享参与的人很多,所以她的中奖几率是最大的,最后中奖了。。。
如果只是抽取指定数量的人,很简单,通过MYSQL的SQL语句一句话就可以实现;
但是我的项目期望抽奖用户在参与后分享页面给其他用户,其他用户参与后分享者的中奖概率因子+1,MYSQL就实现不了了。自己动手写了一个工具类,分享给大家。
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;i<awardSize; i++) {
int value = getRandomNumberInRange(totalSize);
Record record = map.get(value);
if(result.get(record.getOpenId()) == null)
{
//抽中
result.put(record.getOpenId(), record);
}
else
{
i--;
}
}
return result;
}
/**
* 抽奖调用函数入口
* @param records 所有参与抽奖的记录
* @param totalSize 权重分母
* @param awardSize 奖品数量
* @return
*/
public static Map<String,Record> lottery(List<Record> records,int totalSize,int awardSize)
{
Map<Integer,Record> map = new HashMap<Integer,Record>();
int min = 1;
int max = 0;
for (Record record : records) {
min = max + record.getMax() + 1;
max += record.getMax() + record.getRate();
for(int i = min;i<=max;i++)
{
map.put(i, new Record(record.getAwardCode(), record.getOpenId(), record.getRate(), min, max));
}
}
return lottery(map,totalSize,awardSize);
}
/**
* 示例调用方法
* @param args
*/
public static void main(String[] args) {
List<Record> list = new ArrayList<Record>();
list.add(new Record("test","openid001",12));
list.add(new Record("test", "openid002", 12));
list.add(new Record("test", "openid003", 5));
list.add(new Record("test", "openid004", 1));
list.add(new Record("test", "openid005", 1));
//31为以上抽奖重量的和,即 12 + 12 + 5 + 1 + 1
Map<String,Record> result = lottery(list, 31, 2);
//结果即为抽奖返回结果
Iterator<String> it = result.keySet().iterator();
while(it.hasNext())
{
System.out.println(result.get(it.next()));
}
}
}
package com.fengchen.award;
/**
* 抽奖实例对象
* @author FlyerZuo
*
*/
public class Record {
//抽奖活动编码
private String awardCode;
//针对微信抽奖的openId
private String openId;
//中奖概率
private int rate;
private int min;
private int max;
public Record() {
}
public Record(String awardCode, String openId, int rate) {
this.awardCode = awardCode;
this.openId = openId;
this.rate = rate;
}
public Record(String awardCode, String openId, int rate, int min, int max) {
this.awardCode = awardCode;
this.openId = openId;
this.rate = rate;
this.min = min;
this.max = max;
}
public String getAwardCode() {
return awardCode;
}
public void setAwardCode(String awardCode) {
this.awardCode = awardCode;
}
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public String toString()
{
return "openId:"+openId+",awardCode="+awardCode;
}
}