Java commons-lang3 库 RandomUtils 随机数工具类

RandomUtils 类全路径:

org.apache.commons.lang3.RandomUtils

生成随机 boolean 值:

System.out.print(RandomUtils.nextBoolean());

生成随机 int 数:

随机数的取数范围是[0, Integer.MAX_VALUE)。

System.out.print( RandomUtils.nextInt());

生成指定范围内的随机 int 数

例如生成 [1, 100) 范围内的随机 int 数:

System.out.print(RandomUtils.nextInt(1, 100));

生成随机 long 数

随机数的取数范围是[0, Long.MAX_VALUE)

System.out.print(RandomUtils.nextLong());

生成随机 float 数

随机数的取数范围是 [0, Float.MAX_VALUE]

System.out.print(RandomUtils.nextFloat());

生成指定范围的随机 float 数

例如生成 [1, 100] 范围内的随机 float 数:

System.out.print(RandomUtils.nextFloat(1, 100));

生成随机 double 数

随机数的取数范围是 [0, Double.MAX_VALUE]

System.out.print(RandomUtils.nextDouble());

生成指定范围的随机 double 数

例如生成 [1, 100] 范围内的随机 double 数:

System.out.print(RandomUtils.nextDouble(1, 100));
原文地址:https://www.cnblogs.com/_ymw/p/14686191.html