使用Hutool編寫生成隨機(jī)數(shù)的工具類
Hutool 是一個 Java 工具類庫,提供了豐富的工具方法,其中 RandomUtil 是 Hutool 中用于生成隨機(jī)數(shù)的工具類。它封裝了常見的隨機(jī)數(shù)生成需求,使用起來非常方便。
以下是 RandomUtil 的常用方法及其使用示例:
一、添加 Hutool 依賴
首先,確保你的項(xiàng)目中引入了 Hutool 依賴。如果使用 Maven,可以在 pom.xml 中添加以下依賴
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.20</version> <!-- 請使用最新版本 -->
</dependency>
二、RandomUtil 常用方法
2.1 生成隨機(jī)整數(shù)
RandomUtil.randomInt(int limit):生成 [0, limit) 范圍內(nèi)的隨機(jī)整數(shù)。
RandomUtil.randomInt(int min, int max):生成 [min, max) 范圍內(nèi)的隨機(jī)整數(shù)。
import cn.hutool.core.util.RandomUtil;
public class RandomUtilExample {
public static void main(String[] args) {
// 生成 [0, 100) 的隨機(jī)整數(shù)
int randomNumber1 = RandomUtil.randomInt(100);
System.out.println("Random number 1: " + randomNumber1);
// 生成 [10, 20) 的隨機(jī)整數(shù)
int randomNumber2 = RandomUtil.randomInt(10, 20);
System.out.println("Random number 2: " + randomNumber2);
}
}
2.2 生成隨機(jī)長整數(shù)
RandomUtil.randomLong(long limit):生成 [0, limit) 范圍內(nèi)的隨機(jī)長整數(shù)。
RandomUtil.randomLong(long min, long max):生成 [min, max) 范圍內(nèi)的隨機(jī)長整數(shù)。
long randomLong1 = RandomUtil.randomLong(1000L);
System.out.println("Random long 1: " + randomLong1);
long randomLong2 = RandomUtil.randomLong(1000L, 2000L);
System.out.println("Random long 2: " + randomLong2);
2.3 生成隨機(jī)浮點(diǎn)數(shù)
RandomUtil.randomDouble(double limit):生成 [0, limit) 范圍內(nèi)的隨機(jī)浮點(diǎn)數(shù)。
RandomUtil.randomDouble(double min, double max):生成 [min, max) 范圍內(nèi)的隨機(jī)浮點(diǎn)數(shù)。
double randomDouble1 = RandomUtil.randomDouble(100.0);
System.out.println("Random double 1: " + randomDouble1);
double randomDouble2 = RandomUtil.randomDouble(10.0, 20.0);
System.out.println("Random double 2: " + randomDouble2);
2.4 生成隨機(jī)布爾值
RandomUtil.randomBoolean():生成隨機(jī)的 true 或 false。
boolean randomBoolean = RandomUtil.randomBoolean();
System.out.println("Random boolean: " + randomBoolean);
2.5 生成隨機(jī)字符串
RandomUtil.randomString(int length):生成指定長度的隨機(jī)字符串(包含字母和數(shù)字)。
RandomUtil.randomNumbers(int length):生成指定長度的隨機(jī)數(shù)字字符串。
RandomUtil.randomLetters(int length):生成指定長度的隨機(jī)字母字符串。
String randomString = RandomUtil.randomString(10);
System.out.println("Random string: " + randomString);
String randomNumbers = RandomUtil.randomNumbers(6);
System.out.println("Random numbers: " + randomNumbers);
String randomLetters = RandomUtil.randomLetters(8);
System.out.println("Random letters: " + randomLetters);
2.6 生成隨機(jī)字節(jié)數(shù)組
RandomUtil.randomBytes(int length):生成指定長度的隨機(jī)字節(jié)數(shù)組。
byte[] randomBytes = RandomUtil.randomBytes(10);
System.out.println("Random bytes: " + new String(randomBytes));
2.7 從集合中隨機(jī)選擇元素
RandomUtil.randomEle(List<T> list):從列表中隨機(jī)選擇一個元素。
RandomUtil.randomEles(List<T> list, int count):從列表中隨機(jī)選擇多個元素。
import java.util.Arrays;
import java.util.List;
List<String> list = Arrays.asList("Apple", "Banana", "Cherry", "Date");
String randomElement = RandomUtil.randomEle(list);
System.out.println("Random element: " + randomElement);
List<String> randomElements = RandomUtil.randomEles(list, 2);
System.out.println("Random elements: " + randomElements);
2.8 生成隨機(jī) UUID
RandomUtil.randomUUID():生成隨機(jī)的 UUID。
String randomUUID = RandomUtil.randomUUID();
System.out.println("Random UUID: " + randomUUID);
三、高級用法
RandomUtil 還支持自定義隨機(jī)數(shù)生成器(Random 對象),以及生成隨機(jī)日期、隨機(jī)顏色等功能。
import java.util.Random;
???????// 使用自定義 Random 對象
Random customRandom = new Random();
int customRandomNumber = RandomUtil.randomInt(customRandom, 10, 20);
System.out.println("Custom random number: " + customRandomNumber);
四、總結(jié)
RandomUtil 是 Hutool 中非常實(shí)用的工具類,能夠滿足大多數(shù)隨機(jī)數(shù)生成的需求。它的 API 設(shè)計(jì)簡潔,使用方便,適合在 Java 項(xiàng)目中快速實(shí)現(xiàn)隨機(jī)數(shù)相關(guān)的功能。如果你需要更復(fù)雜的隨機(jī)數(shù)生成邏輯,可以結(jié)合 Java 原生的 Random 類或 ThreadLocalRandom 類來實(shí)現(xiàn)。
以上就是使用Hutool編寫生成隨機(jī)數(shù)的工具類的詳細(xì)內(nèi)容,更多關(guān)于Hutool隨機(jī)數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Docker?DockerFile部署java?jar項(xiàng)目包及Mysql和Redis的詳細(xì)過程
Dockerfile是一種用于構(gòu)建Docker鏡像的文件格式,可以通過Dockerfile部署Java項(xiàng)目,這篇文章主要給大家介紹了關(guān)于Docker?DockerFile部署java?jar項(xiàng)目包及Mysql和Redis的詳細(xì)過程,需要的朋友可以參考下2023-12-12
Java輕松實(shí)現(xiàn)Excel與CSV格式互轉(zhuǎn)(含批量轉(zhuǎn)換)
在 Java 開發(fā)中,經(jīng)常會遇到需要在 Excel 和 CSV 文件之間進(jìn)行數(shù)據(jù)轉(zhuǎn)換的場景,本文將分享如何在 Java 中實(shí)現(xiàn)單文件和批量的 Excel 與 CSV 轉(zhuǎn)換方法,希望對大家有所幫助2025-09-09
5分鐘快速搭建SpringBoot3?+?MyBatis-Plus工程/項(xiàng)目的實(shí)現(xiàn)示例
本文主要介紹了使用IntelliJ?IDEA創(chuàng)建Spring?Boot工程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Java面試題-實(shí)現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享
這篇文章主要介紹了Java面試題-實(shí)現(xiàn)復(fù)雜鏈表的復(fù)制代碼分享,小編覺得還是挺不錯的,具有參考價值,需要的朋友可以了解下。2017-10-10
Spring中過濾器和攔截器的區(qū)別及具體實(shí)現(xiàn)方式
在Spring框架中,和都是用于處理 HTTP 請求的中間件,但它們在作用范圍、實(shí)現(xiàn)方式和生命周期上有顯著區(qū)別,本文給大家介紹Spring中過濾器和攔截器的區(qū)別及具體實(shí)現(xiàn),感興趣的朋友一起看看吧2025-07-07
Java并發(fā)編程中的ConcurrentLinkedQueue詳解
這篇文章主要介紹了Java并發(fā)編程中的ConcurrentLinkedQueue詳解,GetThread線程不會因?yàn)镃oncurrentLinkedQueue隊(duì)列為空而等待,而是直接返回null,所以當(dāng)實(shí)現(xiàn)隊(duì)列不空時,等待時,則需要用戶自己實(shí)現(xiàn)等待邏輯,需要的朋友可以參考下2023-12-12

