代码:
public float mockFloatBetween2(int begin, int end) {
BigDecimal between = new BigDecimal(end - begin);
BigDecimal point = new BigDecimal(Math.random());
BigDecimal pointBetween = point.multiply(between);
BigDecimal result = pointBetween.add(new BigDecimal(begin)).setScale(2, BigDecimal.ROUND_FLOOR);
return result.floatValue();
}
步骤解释:
/**
* @Description: 生成int随机数
* @Author: wsp
* @Date: 2021/9/24 17:56
**/
public int mockIntBetween2(int begin, int end) {
int between = end - begin;
double random = Math.random();
int randomBetween = new Double(random * between).intValue();
int result = begin + randomBetween;
return result;
}
/**
* @Description: 生成long随机数
* @Author: wsp
* @Date: 2021/9/24 17:56
**/
public long mockLongBetween2(long begin, long end) {
long between = end - begin;
double random = Math.random();
long randomBetween = new Double(random * between).longValue();
long result = begin + randomBetween;
return result;
}