现在转盘抽奖的活动是层出不穷。最近也做了一个。当然这里是一个实现的简单例子,可以根据我们的需要去新增其他内容。
这里实现的方式就是根据我们设置的概率来做一个随机数的算法,得到我们的奖品。下面就贴上我们的代码来直接看。
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.thinkive.base.jdbc.DataRow;
public class TurnTable {
static Object[][] prizeArr = new Object[][]{
//里面的指针转动
{1,"一等奖",0.0001},
{2,"不要灰心",39.9994},
{3,"祝您好运",20.0},
{4,"二等奖",0.0002},
{5,"再接再厉",20.0},
{6,"运气先攒着",10.0},
{7,"三等奖",0.0003},
{8,"谢谢参与",10.0}
};
Object result[] = award(prizeArr);//抽奖后返回奖品等级及奖品描述
//抽奖后返回奖品等级及奖品描述
public static Object[] award(Object[][] prizeArr){
//概率数组
float obj[] = new float[prizeArr.length];
for(int i=0;i<prizeArr.length;i++){
obj[i] = Float.parseFloat(prizeArr[i][2].toString());
}
Integer prizeId = getRand(obj); //根据概率获取奖项号码
String msg = (String) prizeArr[prizeId][1];//奖品信息
Integer prizeIds = prizeId +1;
System.out.println("奖品号: "+prizeIds +" 获奖等级:"+msg);
return new Object[]{prizeId,msg};
}
//根据概率获取奖项
public static Integer getRand(float obj[]){
Integer result = null;
try {
float sum = 0.0f;//概率数组的总概率精度
float min = 0.0f;//
for(int i=0;i<obj.length;i++){
BigDecimal beforSum = new BigDecimal(Float.toString(sum));
BigDecimal objValue = new BigDecimal(Float.toString(obj[i]));
sum = beforSum.add(objValue).floatValue();
}
for(int i=0;i<obj.length;i++){//概率数组循环
BigDecimal db = new BigDecimal(Math.random() * (sum - min) + min);
BigDecimal b = new BigDecimal(Float.toString(obj[i]));
if(compareMethod(db,b) == -1){//中奖
result = i;
break;
}else{
sum -=obj[i];
}
}
} catch (Exception e) {
e.printStackTrace();
}
/**
* 奖品控制代码,这里可以控制我们的奖品的等级
* **/
return result;
}
public static void main(String[] args) {
DataRow data = new DataRow();//此处DataRow为封装了的 HashMap
List<DataRow> resultList = new ArrayList<DataRow>();
for (int i = 0; i < 10000; i++) {
int k = 1;
Object result[] = award(prizeArr);//抽奖后返回角度和奖品等级
String prize = "";
String comment = "";
for (Object object : result) {
if(k==1){
prize = object.toString();
}else if(k==2){
comment = object.toString();
}
k++;
}
if(k==3){
data.set("prize", Integer.valueOf(prize)+1);
data.set("comment", comment);
resultList.add(data);
}
}
}
public static int compareMethod(BigDecimal a,BigDecimal b){
return a.compareTo(b);
}
}
首先我们先来看看我们的奖品初始化,就是这段代码了
static Object[][] prizeArr = new Object[][]{
//里面的指针转动
{1,"一等奖",0.0001},
{2,"不要灰心",39.9994},
{3,"祝您好运",20.0},
{4,"二等奖",0.0002},
{5,"再接再厉",20.0},
{6,"运气先攒着",10.0},
{7,"三等奖",0.0003},
{8,"谢谢参与",10.0}
};
这里我们可以在对象中添加更多的参数,如果你的抽奖是需要角度的或者奖品位置的那你就加入角度数据(两个值,一个开始一个结束),同时也需要根据角度数据产生一个随机数。
整个程序最主要的就是这一个抽奖算法了。在这里我们可以控制奖品的等级,数量等等,具体的需要自己去添加啦,这里仅实现了抽奖。这里有个小小的建议,在传入奖品数组的时间可以把大概率的排在前面,这样抽奖的时候算法循环的次数会有一定的减少。
//根据概率获取奖项
public static Integer getRand(float obj[]){
Integer result = null;
try {
float sum = 0.0f;//概率数组的总概率精度
float min = 0.0f;//
for(int i=0;i<obj.length;i++){
BigDecimal beforSum = new BigDecimal(Float.toString(sum));
BigDecimal objValue = new BigDecimal(Float.toString(obj[i]));
sum = beforSum.add(objValue).floatValue();
}
for(int i=0;i<obj.length;i++){//概率数组循环
BigDecimal db = new BigDecimal(Math.random() * (sum - min) + min);
BigDecimal b = new BigDecimal(Float.toString(obj[i]));
if(compareMethod(db,b) == -1){//中奖
result = i;
break;
}else{
sum -=obj[i];
}
}
} catch (Exception e) {
e.printStackTrace();
}
/**
* 奖品控制代码,这里可以控制我们的奖品的等级
* **/
return result;
}
以上就是转盘抽奖算法了。希望对有需要的程序员们有一定帮助。
有位同学反应没有提供DataRow类,这里加上,其实就是对Map进行了封装。
import java.util.HashMap;
public class DataRow extends HashMap {
public DataRow() {
}
public void set(String name, String value) {
if(name != null && !name.equals("")) {
if(value == null) {
this.put(name, "");
} else {
this.put(name, value);
}
}
}
public void set(String name, int value) {
this.put(name, new Integer(value));
}
public void set(String name, boolean value) {
this.put(name, new Boolean(value));
}
public void set(String name, long value) {
this.put(name, new Long(value));
}
public void set(String name, float value) {
this.put(name, new Float(value));
}
public void set(String name, double value) {
this.put(name, new Double(value));
}
public void set(String name, Object value) {
this.put(name, value);
}
public String getString(String name) {
if(name != null && !name.equals("")) {
Object obj = this.get(name);
return obj == null?"":obj.toString();
} else {
return "";
}
}
public int getInt(String name) {
if(name != null && !name.equals("")) {
boolean value = false;
if(!this.containsKey(name)) {
return 0;
} else {
Object obj = this.get(name);
if(obj == null) {
return 0;
} else {
int value1;
if(!(obj instanceof Integer)) {
try {
value1 = Integer.parseInt(obj.toString());
} catch (Exception var5) {
value1 = 0;
}
} else {
value1 = ((Integer)obj).intValue();
obj = null;
}
return value1;
}
}
} else {
return 0;
}
}
public long getLong(String name) {
if(name != null && !name.equals("")) {
long value = 0L;
if(!this.containsKey(name)) {
return 0L;
} else {
Object obj = this.get(name);
if(obj == null) {
return 0L;
} else {
if(!(obj instanceof Long)) {
try {
value = Long.parseLong(obj.toString());
} catch (Exception var6) {
value = 0L;
}
} else {
value = ((Long)obj).longValue();
obj = null;
}
return value;
}
}
} else {
return 0L;
}
}
public float getFloat(String name) {
if(name != null && !name.equals("")) {
float value = 0.0F;
if(!this.containsKey(name)) {
return 0.0F;
} else {
Object obj = this.get(name);
if(obj == null) {
return 0.0F;
} else {
if(!(obj instanceof Float)) {
try {
value = Float.parseFloat(obj.toString());
} catch (Exception var5) {
value = 0.0F;
}
} else {
value = ((Float)obj).floatValue();
obj = null;
}
return value;
}
}
} else {
return 0.0F;
}
}
public double getDouble(String name) {
if(name != null && !name.equals("")) {
double value = 0.0D;
if(!this.containsKey(name)) {
return 0.0D;
} else {
Object obj = this.get(name);
if(obj == null) {
return 0.0D;
} else {
if(!(obj instanceof Double)) {
try {
value = Double.parseDouble(obj.toString());
} catch (Exception var6) {
value = 0.0D;
}
} else {
value = ((Double)obj).doubleValue();
obj = null;
}
return value;
}
}
} else {
return 0.0D;
}
}
public boolean getBoolean(String name) {
if(name != null && !name.equals("")) {
boolean value = false;
if(!this.containsKey(name)) {
return false;
} else {
Object obj = this.get(name);
if(obj == null) {
return false;
} else if(obj instanceof Boolean) {
return ((Boolean)obj).booleanValue();
} else {
value = Boolean.valueOf(obj.toString()).booleanValue();
obj = null;
return value;
}
}
} else {
return false;
}
}
public Object getObject(String name) {
return name != null && !name.equals("")?(!this.containsKey(name)?null:this.get(name)):null;
}
}