工具类拿去用吧
可以根据实际情况和需求进行修改
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
public class LuckyMachineUtil {
private static List<Integer> stringT=new ArrayList<>();
private static List<Integer> stringC=new ArrayList<>();
/**
* 根据传入的百分比将参数分组(初始化奖池),抽奖
*/
public static boolean luckDraw(String percentage){
//解析中奖概率
boolean statDoubel=judgeDoubel(percentage);
//判断能否将小数转换成整型
boolean statInt=judgeInt(percentage);
if(!statInt && !statDoubel){
System.out.println("概率参数有问题,默认不中奖");
return false;
}
if(statInt){
stringT=new ArrayList<>();
stringC=new ArrayList<>();
BitSet bs = new BitSet(100);
//生成中奖奖池
for (int count = 0; count < Integer.valueOf(percentage);) {
int random = (int) (Math.random() * 100);
if (!bs.get(random)) {
bs.set(random);
stringT.add(random);
count++;
}
}
//生成不中奖奖池
for (int count = 0; count < 100;count++) {
//输出不重复的随机数
//System.out.print(i + " ");
if(!stringT.contains(count)){
stringC.add(count);
}
}
//获取中奖随机数判断是否中奖
int random = (int) (Math.random() * 100);
boolean t =stringT.contains(random);
boolean f =stringC.contains(random);
System.out.println("中奖奖池"+stringT.toString());
System.out.println("不中奖奖池"+stringC.toString());
System.out.println("号码"+random);
if (t==true && f==false){
return true;
}else {
return false;
}
}else if(statDoubel){
//将概率转换成小数
Double jdoube=Double.valueOf(percentage);
if(jdoube==0){
return false;
}
//获取小数位数
int p= checkIsDoublePointTwo(jdoube);
stringT = new ArrayList<>();
stringC = new ArrayList<>();
BitSet bs = new BitSet(100 * (p * 100));
for (int count = 0; count < (p * 100); ) {
int random = (int) (Math.random() * 100 * (p * 100));
if (!bs.get(random)) {
bs.set(random);
stringT.add(random);
count++;
}
}
for (int count = 0; count < 100 * (p * 100);count++) {
//输出不重复的随机数
//System.out.print(i + " ");
if(!stringT.contains(count)) {
stringC.add(count);
}
}
//获取中奖随机数判断是否中奖
int random = (int) (Math.random() * 100 * (p * 100));
boolean t = stringT.contains(random);
boolean f = stringC.contains(random);
System.out.println("中奖奖池"+stringT.toString());
System.out.println("不中奖奖池"+stringC.toString());
System.out.println("号码"+random);
if (t == true && f == false) {
return true;
} else {
return false;
}
}
return false;
}
/**
* 尝试将doube转换成数字
* @param doubelString
* @return
*/
public static boolean judgeDoubel(String doubelString){
try {
Double d=Double.valueOf(doubelString);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
* 尝试将doube转换成数字
* @param doubelString
* @return
*/
public static boolean judgeInt(String doubelString){
try {
Integer d=Integer.valueOf(doubelString);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
/**
* 获取double的位数
* @param param
* @return
*/
public static int checkIsDoublePointTwo(Double param) {
if (param == null) {
return 0;
}
BigDecimal bd = new BigDecimal(String.valueOf(param));
String[] ss = bd.toString().split("\\.");
if (ss.length <= 1){
return 0;
}
return ss[1].length();
}
public static void main(String[] args) {
System.out.println(luckDraw("0"));
}
}