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