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));

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM