当前位置:网站首页>【commons-lang3专题】002- RandomUtils 专题
【commons-lang3专题】002- RandomUtils 专题
2022-07-28 23:29:00 【訾博ZiBo】
【commons-lang3专题】002- RandomUtils 专题
文章目录
〇、准备
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
}
}
边栏推荐
- flask与七牛云上传图片
- 我不建议你使用SELECT *
- 时间序列数据的预处理方法总结
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
- COPU陆首群教授应邀在开放原子全球开源峰会上做主旨演讲
- I don't know how lucky the boy who randomly typed the log is. There must be a lot of overtime!
- day8
- Anomaly detection and unsupervised learning (1)
- 云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
- Depth first search (DFS) and its matlab code
猜你喜欢

Five interesting magic commands in jupyter notebook

16. Influence of deviation, variance, regularization and learning curve on the model

Requestvideoframecallback() simple instance
![Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]](/img/e3/496247afdb3ea5b9a9cdb8afb0d41b.png)
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]

Outlier detection and open set identification (1)

Tips for API interface optimization

Router view cannot be rendered (a very low-level error)

第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄

I was asked several questions about string in the interview. Can you answer them?

AQS原理
随机推荐
DRF -- authentication, authority, frequency source code analysis, global exception handling, automatic generation of interface documents, RBAC introduction
[Yugong series] go teaching course in July 2022, an array of 020 go containers
Outlier detection and open set identification (1)
Flash and seven cattle cloud upload pictures
(20211130更新)关于jupyter notebook的下载安装及自己的配置、主题
“吃货联盟定餐系统”
selenium对接代理与seleniumwire访问开发者工具NetWork
execute immediate 简单示例合集(DML)
Tips for API interface optimization
Outlier detection and Gan network (1)
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
There is a span tag. If you want to do click events on it, how can you expand the click area
Camera Hal OEM module ---- CMR_ preview.c
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Introduction of shortest path tree (SPT) and matlab code
【开发教程10】疯壳·开源蓝牙心率防水运动手环-蓝牙 BLE 收发
Several methods of multi-threaded sequential operation can be asked casually in the interview
时间序列数据的预处理方法总结
Five interesting magic commands in jupyter notebook
[development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver