提供各种随机生成各种数据的方法。
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
// 1、生成 [0, Integer.MAX_VALUE) 之间的随机 int 值
System.out.println(RandomUtils.nextInt()); // 150881120
// 2、生成 [startInclusive,endExclusive) 之间的随机整数,起始值不能小于终止值
System.out.println(RandomUtils.nextInt(10, 20)); // 19
// 3、随机生成一个布尔值,true 或者 false
System.out.println(RandomUtils.nextBoolean()); // true
// 4、生成 [0, Long.MAX_VALUE) 之间的 long 值
System.out.println(RandomUtils.nextLong()); // 2331387764569299625
// 5、生成 [startInclusive,endExclusive) 之间的随机 long 值
System.out.println(RandomUtils.nextLong(10, 20)); // 12
// 6、生成指定个数的字节数组
System.out.println(Arrays.toString(RandomUtils.nextBytes(5))); // [-81, 122, -126, -109, 2]
// 7、生成 [0, Double.MAX_VALUE) 之间的随机 double 值
System.out.println(RandomUtils.nextDouble()); // 5.778886902758481E306
// 8、生成 [startInclusive,endExclusive) 之间的随机 double 值
System.out.println(RandomUtils.nextDouble(10, 20)); // 19.05143192281762
// 9、生成 [0, Float.MAX_VALUE) 之间的随机 float 值
System.out.println(RandomUtils.nextFloat()); // 5.778886902758481E306
// 10、生成 [startInclusive,endExclusive) 之间的随机 float 值
System.out.println(RandomUtils.nextFloat(10, 20)); // 14.427974
package com.zibo.zibo2022.random_utils.main;
import org.apache.commons.lang3.RandomUtils;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// start
// 1、生成 [0, Integer.MAX_VALUE) 之间的随机 int 值
System.out.println(RandomUtils.nextInt()); // 150881120
// 2、生成 [startInclusive,endExclusive) 之间的随机整数,起始值不能小于终止值
System.out.println(RandomUtils.nextInt(10, 20)); // 19
// 3、随机生成一个布尔值,true 或者 false
System.out.println(RandomUtils.nextBoolean()); // true
// 4、生成 [0, Long.MAX_VALUE) 之间的 long 值
System.out.println(RandomUtils.nextLong()); // 2331387764569299625
// 5、生成 [startInclusive,endExclusive) 之间的随机 long 值
System.out.println(RandomUtils.nextLong(10, 20)); // 12
// 6、生成指定个数的字节数组
System.out.println(Arrays.toString(RandomUtils.nextBytes(5))); // [-81, 122, -126, -109, 2]
// 7、生成 [0, Double.MAX_VALUE) 之间的随机 double 值
System.out.println(RandomUtils.nextDouble()); // 5.778886902758481E306
// 8、生成 [startInclusive,endExclusive) 之间的随机 double 值
System.out.println(RandomUtils.nextDouble(10, 20)); // 19.05143192281762
// 9、生成 [0, Float.MAX_VALUE) 之间的随机 float 值
System.out.println(RandomUtils.nextFloat()); // 5.778886902758481E306
// 10、生成 [startInclusive,endExclusive) 之间的随机 float 值
System.out.println(RandomUtils.nextFloat(10, 20)); // 14.427974
// end
}
}