0
点赞
收藏
分享

微信扫一扫

【commons-lang3专题】002- RandomUtils 专题


【commons-lang3专题】002- RandomUtils 专题

文章目录

  • ​​【commons-lang3专题】002- RandomUtils 专题​​
  • ​​〇、准备​​
  • ​​1、RandomUtils 主要作用​​
  • ​​2、引入依赖​​
  • ​​一、生成 int 值​​
  • ​​1、生成 [0, Integer.MAX_VALUE) 之间的随机 int 值​​
  • ​​2、生成 [startInclusive,endExclusive) 之间的随机整数,起始值不能小于终止值​​
  • ​​二、生成 boolean 值​​
  • ​​3、随机生成一个布尔值,true 或者 false​​
  • ​​三、生成 long 值​​
  • ​​4、生成 [0, Long.MAX_VALUE) 之间的 long 值​​
  • ​​5、生成 [startInclusive,endExclusive) 之间的随机 long 值​​
  • ​​四、生成字节数组​​
  • ​​6、生成指定个数的字节数组​​
  • ​​五、生成 double 值​​
  • ​​7、生成 [0, Double.MAX_VALUE) 之间的随机 double 值​​
  • ​​8、生成 [startInclusive,endExclusive) 之间的随机 double 值​​
  • ​​六、生成 float 值​​
  • ​​9、生成 [0, Float.MAX_VALUE) 之间的随机 float 值​​
  • ​​10、生成 [startInclusive,endExclusive) 之间的随机 float 值​​
  • ​​七、完整代码​​

〇、准备

1、RandomUtils 主要作用

提供各种随机生成各种数据的方法。

2、引入依赖

<!-- 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>

一、生成 int 值

1、生成 [0, Integer.MAX_VALUE) 之间的随机 int 值

// 1、生成 [0, Integer.MAX_VALUE) 之间的随机 int 值
System.out.println(RandomUtils.nextInt()); // 150881120

2、生成 [startInclusive,endExclusive) 之间的随机整数,起始值不能小于终止值

// 2、生成 [startInclusive,endExclusive) 之间的随机整数,起始值不能小于终止值
System.out.println(RandomUtils.nextInt(10, 20)); // 19

二、生成 boolean 值

3、随机生成一个布尔值,true 或者 false

// 3、随机生成一个布尔值,true 或者 false
System.out.println(RandomUtils.nextBoolean()); // true

三、生成 long 值

4、生成 [0, Long.MAX_VALUE) 之间的 long 值

// 4、生成 [0, Long.MAX_VALUE) 之间的 long 值
System.out.println(RandomUtils.nextLong()); // 2331387764569299625

5、生成 [startInclusive,endExclusive) 之间的随机 long 值

// 5、生成 [startInclusive,endExclusive) 之间的随机 long 值
System.out.println(RandomUtils.nextLong(10, 20)); // 12

四、生成字节数组

6、生成指定个数的字节数组

// 6、生成指定个数的字节数组
System.out.println(Arrays.toString(RandomUtils.nextBytes(5))); // [-81, 122, -126, -109, 2]

五、生成 double 值

7、生成 [0, Double.MAX_VALUE) 之间的随机 double 值

// 7、生成 [0, Double.MAX_VALUE) 之间的随机 double 值
System.out.println(RandomUtils.nextDouble()); // 5.778886902758481E306

8、生成 [startInclusive,endExclusive) 之间的随机 double 值

// 8、生成 [startInclusive,endExclusive) 之间的随机 double 值
System.out.println(RandomUtils.nextDouble(10, 20)); // 19.05143192281762

六、生成 float 值

9、生成 [0, Float.MAX_VALUE) 之间的随机 float 值

// 9、生成 [0, Float.MAX_VALUE) 之间的随机 float 值
System.out.println(RandomUtils.nextFloat()); // 5.778886902758481E306

10、生成 [startInclusive,endExclusive) 之间的随机 float 值

// 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
}

}


举报

相关推荐

0 条评论