package com.startx.http.wordfilter;
import java.util.*;
public class RandomDemo {
public static void main(String[] args) {
Map<String, Object> maps = new LinkedHashMap<>();
maps.put("0.1", "0.1111");
maps.put("1.5", "111111");
maps.put("5", "555555");
maps.put("30", "33333333333333");
maps.put("60", "66666666666666");
maps.put("90", "99999999999999");
maps.put("100", "00000000000000");
for (String s : maps.keySet()) {
String key = getTarget(s);
System.err.println("key = ===" + key);
if (key != null) {
System.err.println("value ============" + maps.get(key));
break;
}
}
}
/**
* 命中则将key返回
*
* @param p
* @return
*/
public static String getTarget(String p) {
//单位转换称整数
int conversion = 10;
//挑选到的数据
List<Integer> userList = new LinkedList<>();
//happy 号码
Integer happy = new Random().nextInt(100);
//几率
Double probability = new Double(p);
String probabilityString = probability.toString();
int total = 100;
int douat;
if ((douat = probabilityString.indexOf(".")) != -1) {
int sum = probabilityString.length();
//不是整数
if (!(sum - 2 == douat && probabilityString.charAt(douat + 1) == '0')) {
if (new Integer(probabilityString.substring(0, douat)) > 100) {
System.err.println(" probability is error!!!");
return null;
}
for (int i = 0; i < sum - douat - 1; i++) {
total *= conversion;
probability *= conversion;
}
}
}
// 5%概率在100个数字中连续拿5个数字
Set<String> targetSet = new HashSet<>();
for (int i = 0; i < probability; i++) {
//设置的获取成功的目标值
Integer target;
//判断数值是否在之前已经使用过
while (true) {
target = new Random().nextInt(total);
if (!targetSet.contains(target.toString())) {
targetSet.add(target.toString());
break;
}
}
userList.add(target);
if (i == probability - 1) {
System.err.println("happy=======" + happy);
System.err.println("选择的" + userList);
System.err.println("size=======" + userList.size());
}
if (target == happy) {
System.err.println("happy=======" + happy);
System.err.println("选择的" + userList);
System.err.println("size=======" + userList.size());
System.err.println("good luck!");
return p;
}
}
return null;
}
}