2019-10-11 16:32:20 +08:00
|
|
|
|
package util;
|
2019-01-22 09:25:44 +08:00
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2019-07-15 17:41:36 +08:00
|
|
|
|
import java.util.*;
|
2019-01-22 09:25:44 +08:00
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by admin on 2014/11/14.
|
|
|
|
|
* 随机工具类
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class MathUtils {
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(MathUtils.class);
|
|
|
|
|
|
2019-01-22 09:56:56 +08:00
|
|
|
|
private static final Random random = new Random();
|
|
|
|
|
|
2019-06-29 10:11:52 +08:00
|
|
|
|
|
2019-09-07 19:26:27 +08:00
|
|
|
|
public static void main(String[] args){
|
|
|
|
|
String i = "0|00|111";
|
|
|
|
|
String[] split = i.split("|", 1);
|
|
|
|
|
}
|
2019-01-22 14:37:10 +08:00
|
|
|
|
public static int randomForStrArray(String [] rateArray) {
|
2019-01-22 09:25:44 +08:00
|
|
|
|
return randomStrArray(rateArray, -1);
|
|
|
|
|
}
|
2019-01-22 14:37:10 +08:00
|
|
|
|
public static int randomStrArray(String [] rateArray, int minSum) {
|
2019-01-22 09:25:44 +08:00
|
|
|
|
if (null == rateArray) {
|
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"The random array must not be null!");
|
|
|
|
|
}
|
|
|
|
|
int arrayLength = rateArray.length;
|
|
|
|
|
if (arrayLength == 0) {
|
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"The random array's length must not be zero!");
|
|
|
|
|
}
|
|
|
|
|
// 依次累加的和
|
|
|
|
|
int rateSum = 0;
|
|
|
|
|
// 从头开始 依次累加之后的各个元素和 的临时数组
|
|
|
|
|
int[] rateSumArray = new int[arrayLength];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < arrayLength; i++) {
|
|
|
|
|
|
2019-01-22 14:37:10 +08:00
|
|
|
|
if (Integer.parseInt(rateArray[i]) < 0) {
|
2019-01-22 09:25:44 +08:00
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"The array's element must not be equal or greater than zero!");
|
|
|
|
|
}
|
2019-01-22 14:37:10 +08:00
|
|
|
|
rateSum += Integer.parseInt(rateArray[i]);
|
2019-01-22 09:25:44 +08:00
|
|
|
|
rateSumArray[i] = rateSum;
|
|
|
|
|
}
|
|
|
|
|
if (rateSum <= 0) {
|
|
|
|
|
// 所有概率都为零,必然没有选中的元素,返回无效索引:-1
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
//如果有保底分母设置,当所有权值之和小于保底分母时,则启用保底分母
|
|
|
|
|
if(minSum < rateSum) {
|
|
|
|
|
minSum = rateSum;
|
|
|
|
|
}
|
|
|
|
|
int randomInt = MathUtils.random(1, minSum);
|
|
|
|
|
if(randomInt > rateSum) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int bingoIndex = -1;
|
|
|
|
|
for (int i = 0; i < arrayLength; i++) {
|
|
|
|
|
if (randomInt <= rateSumArray[i]) {
|
|
|
|
|
bingoIndex = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (bingoIndex == -1) {
|
|
|
|
|
throw new IllegalStateException("Cannot find out bingo index!");
|
|
|
|
|
}
|
|
|
|
|
return bingoIndex;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 返回>=low, <=hi的整数随机数,均匀分布
|
|
|
|
|
*
|
|
|
|
|
* @param low
|
|
|
|
|
* @param hi
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int random(int low, int hi) {
|
|
|
|
|
// return (int) (low + (hi - low + 0.9) * Math.random());
|
|
|
|
|
return low + ThreadLocalRandom.current().nextInt(hi - low + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 09:56:56 +08:00
|
|
|
|
public static int randomInt(int n){
|
|
|
|
|
return random.nextInt(n);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 16:14:22 +08:00
|
|
|
|
public static int randomInt(){
|
|
|
|
|
return random.nextInt();
|
|
|
|
|
}
|
2019-01-24 17:53:13 +08:00
|
|
|
|
|
|
|
|
|
public static int numRount(float num){
|
|
|
|
|
return Math.round(num);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 17:54:50 +08:00
|
|
|
|
/**
|
|
|
|
|
* 限置为 >=min <= max的值
|
|
|
|
|
*
|
|
|
|
|
* @param original
|
|
|
|
|
* @param min
|
|
|
|
|
* @param max
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int setBetweenWithMax(int original, int min, int max) {
|
|
|
|
|
if (original >= max) {
|
|
|
|
|
original = max;
|
|
|
|
|
}
|
|
|
|
|
if (original < min) {
|
|
|
|
|
original = min;
|
|
|
|
|
}
|
|
|
|
|
return original;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 21:07:45 +08:00
|
|
|
|
public static int getMaxNum(int a, int b) {
|
|
|
|
|
if(a>=b){
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
return b;
|
|
|
|
|
}
|
2019-03-02 18:30:38 +08:00
|
|
|
|
|
|
|
|
|
//ab x 公式
|
|
|
|
|
public static float calABX(float source,float[] base){
|
|
|
|
|
float result = 0;
|
|
|
|
|
int length = base.length;
|
|
|
|
|
for(int i=0;i< base.length;i++){
|
|
|
|
|
double pow = Math.pow(source, --length);
|
|
|
|
|
result += base[i]*pow;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ab x 公式
|
|
|
|
|
public static float calABX(float source,int[] base){
|
|
|
|
|
float result = 0;
|
|
|
|
|
int length = base.length;
|
|
|
|
|
for(int i=0;i< base.length;i++){
|
|
|
|
|
double pow = Math.pow(source, --length);
|
|
|
|
|
result += base[i]*pow;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-07-22 11:11:06 +08:00
|
|
|
|
public static int randomFromWeightReturnIndex(int[][] array){
|
2019-06-29 16:29:28 +08:00
|
|
|
|
int amount = 0;
|
|
|
|
|
int result = 0;
|
|
|
|
|
for(int i = 0 ; i <array.length;i++){
|
|
|
|
|
amount+=array[i][1];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
int rate = MathUtils.randomInt(amount);
|
|
|
|
|
amount = 0;
|
|
|
|
|
for (int i = 0 ; i<array.length;i++){
|
|
|
|
|
amount+=array[i][1];
|
|
|
|
|
if(rate<amount){
|
2019-07-22 11:11:06 +08:00
|
|
|
|
result = i;
|
2019-06-29 16:29:28 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2019-07-12 10:19:23 +08:00
|
|
|
|
public static int randomFromArray(int[] array){
|
2019-07-12 13:33:56 +08:00
|
|
|
|
if(array.length>0){
|
|
|
|
|
return array[(int) Math.floor(Math.random()*array.length)];
|
2019-07-12 10:19:23 +08:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-06-29 16:29:28 +08:00
|
|
|
|
|
2019-07-15 17:41:36 +08:00
|
|
|
|
/**
|
|
|
|
|
* 根据数组索引就行随机
|
|
|
|
|
* @param sourceArray
|
|
|
|
|
* @param randomNums
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2019-07-15 14:35:38 +08:00
|
|
|
|
public static List<Integer> randomForOneArray(int sourceArray[], int randomNums) {
|
|
|
|
|
List<Integer> result = new ArrayList<>();
|
|
|
|
|
int length = sourceArray.length;
|
2019-07-15 17:41:36 +08:00
|
|
|
|
Set<Integer> cacheIndexs = new HashSet<>();
|
2019-07-15 14:35:38 +08:00
|
|
|
|
while (randomNums>0){
|
2019-07-15 17:41:36 +08:00
|
|
|
|
int randomPos = randomInt(length);
|
|
|
|
|
if(cacheIndexs.contains(randomPos)){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
cacheIndexs.add(randomPos);
|
|
|
|
|
result.add(sourceArray[randomPos]);
|
2019-07-15 14:35:38 +08:00
|
|
|
|
randomNums--;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
2019-07-18 19:04:04 +08:00
|
|
|
|
|
2019-07-22 11:36:58 +08:00
|
|
|
|
public static int getTheIndexByTarget(int[][] sourceArray,int target){
|
2019-07-18 19:04:04 +08:00
|
|
|
|
for(int i=0;i<sourceArray.length;i++){
|
|
|
|
|
int[] item = sourceArray[i];
|
|
|
|
|
if(target>=item[0] && target<=item[1]){
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-07-22 11:11:06 +08:00
|
|
|
|
public static int randomFromWeight(int[][] array){
|
|
|
|
|
int index = randomFromWeightReturnIndex(array);
|
|
|
|
|
return array[index][0];
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 10:02:10 +08:00
|
|
|
|
public static int[] randomFromWeight(int[][][] arrays){
|
|
|
|
|
int[] result = new int[arrays.length];
|
|
|
|
|
int i=0;
|
|
|
|
|
for(int[][] array : arrays){
|
|
|
|
|
int index = randomFromWeightReturnIndex(array);
|
|
|
|
|
result[i++] = array[index][0];
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 11:11:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据权重,不放回随机
|
|
|
|
|
* @param array
|
|
|
|
|
* @param count 随机个数
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int[] randomFromWeightWithTaking(int[][] array,int count){
|
2020-02-09 19:48:55 +08:00
|
|
|
|
|
|
|
|
|
if(array.length<count){
|
|
|
|
|
throw new IllegalArgumentException(array.length+"<"+count);
|
|
|
|
|
}
|
2019-07-23 17:41:48 +08:00
|
|
|
|
if(count==1){
|
|
|
|
|
return new int[]{randomFromWeight(array)};
|
|
|
|
|
}
|
2019-07-22 11:11:06 +08:00
|
|
|
|
int[] result = new int[count];
|
|
|
|
|
int[][] tempArray = new int[array.length][];
|
|
|
|
|
for(int i = 0 ; i <array.length;i++){
|
|
|
|
|
tempArray[i] = array[i];
|
|
|
|
|
}
|
|
|
|
|
for(int i = 0; i < count; i++){
|
|
|
|
|
int index = randomFromWeightReturnIndex(tempArray);
|
2019-07-23 17:41:48 +08:00
|
|
|
|
result[i] = tempArray[index][0];
|
|
|
|
|
tempArray[index] = tempArray[tempArray.length-1];
|
|
|
|
|
tempArray = Arrays.copyOf(tempArray,tempArray.length-1);
|
2019-07-22 11:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据层数重置
|
|
|
|
|
* @param tower
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static int resetMethod(int tower){
|
|
|
|
|
int afterResetTower = tower-(tower%5);
|
|
|
|
|
// LOGGER.info("重置当前层,当前层为{},重置为{}",mapManager.getTower(),afterResetTower);
|
|
|
|
|
if(tower==0){
|
|
|
|
|
return 1;
|
|
|
|
|
} else if(tower>=5){
|
|
|
|
|
return afterResetTower;
|
|
|
|
|
}else{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 13:45:45 +08:00
|
|
|
|
|
|
|
|
|
public static int randomWeight(int[][] source,Set<Integer> excluIndexs){
|
|
|
|
|
int length = source.length;
|
|
|
|
|
int randomIndex = 0;
|
2019-09-09 12:38:21 +08:00
|
|
|
|
int times=100;
|
|
|
|
|
while (times-->0){
|
2019-08-31 13:45:45 +08:00
|
|
|
|
randomIndex= randomInt(length);
|
|
|
|
|
if(!excluIndexs.contains(randomIndex)){
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-09 12:38:21 +08:00
|
|
|
|
if(times<=0){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-08-31 13:45:45 +08:00
|
|
|
|
excluIndexs.add(randomIndex);
|
|
|
|
|
return randomIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 14:17:54 +08:00
|
|
|
|
//计算最大公约数
|
|
|
|
|
public static int getGCD(int a,int b){
|
|
|
|
|
if(a<0 || b<0){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if(b==0){
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
while (a%b!=0){
|
|
|
|
|
int temp = a % b;
|
|
|
|
|
a = b;
|
|
|
|
|
b = temp;
|
|
|
|
|
}
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-09 19:48:55 +08:00
|
|
|
|
|
|
|
|
|
public static List<Integer> combineList(int start,int end){
|
|
|
|
|
List<Integer> list = new ArrayList<>();
|
|
|
|
|
while (start<=end){
|
|
|
|
|
list.add(start);
|
|
|
|
|
start++;
|
|
|
|
|
}
|
|
|
|
|
Collections.shuffle(list);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将数组均匀拆分为几份
|
|
|
|
|
*/
|
|
|
|
|
public static List<Integer> average(int sum, int split) {
|
|
|
|
|
List<Integer> result = new ArrayList<>();
|
|
|
|
|
if (split == 0) {
|
|
|
|
|
LOGGER.error("randomForOneArray::IllegalArgumentException");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
int remain = sum % split;
|
|
|
|
|
int out = split - remain;
|
|
|
|
|
int item = (sum+out) / split;
|
|
|
|
|
for (int i = 0; i < split; i++) {
|
|
|
|
|
int innerItem = i == 0 ? item-out : item;
|
|
|
|
|
result.add(innerItem);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-01-22 09:25:44 +08:00
|
|
|
|
}
|