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