package com.paic.pad.info.common.utils;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class GetRedPacket {
public static Map<String, String> getRandomNum(double randomNum) {
Map<String, String> map = new HashMap<String, String>();
// 7. 产生一个随机数
Random random = new Random(); // 随机数实例
double base = random.nextDouble(); // 产生一个小于1 大于0的小数点数
// 8. 产生的随机数小于中奖概率数,则提示抽奖失败
if (base > randomNum) {
String result = "点子:" + base + "|<没中奖>";
map.put("code", "0");
map.put("message", result);
} else {
String result = "点子:" + base + "《《中奖》》";
map.put("code", "1");
map.put("message", result);
}
return map;
}
public static void main(String[] args) {
int sumGift = 8000;//总奖品
int sumPerson = 4000;//总人数
double randomNum = sumPerson * 1.0 / sumGift; //人数除奖品得到概率
int count =0;
for (int i = 0; i < 100; i++) {
Map<String, String> map = getRandomNum(randomNum);
String code = map.get("code");
String message = map.get("message");
if(code.equals("1")) {
count ++;
}
System.out.println(message);
}
System.out.println("概率:"+randomNum +",共中奖了:" + count + "次");
}
}