Hutool is a Java tool class library that provides rich tool methods, among which RandomUtil is a tool class in Hutool used to generate random numbers. It encapsulates common random number generation requirements and is very convenient to use.
The following are common methods and examples of RandomUtil:
1. Add Hutool dependencies
First, make sure that you introduce Hutool dependencies into your project. If using Maven, you can add the following dependencies in
<dependency> <groupId></groupId> <artifactId>hutool-all</artifactId> <version>5.8.20</version> <!-- Please use the latest version --> </dependency>
2. Common methods of RandomUtil
2.1 Generate random integers
(int limit): generates random integers in the range [0, limit).
(int min, int max): generates random integers in the range [min, max).
import ; public class RandomUtilExample { public static void main(String[] args) { // Generate random integers of [0, 100) int randomNumber1 = (100); ("Random number 1: " + randomNumber1); // Generate random integers of [10, 20) int randomNumber2 = (10, 20); ("Random number 2: " + randomNumber2); } }
2.2 Generate random long integers
(long limit): generates a random long integer in the range [0, limit).
(long min, long max): generates a random long integer in the range [min, max).
long randomLong1 = (1000L); ("Random long 1: " + randomLong1); long randomLong2 = (1000L, 2000L); ("Random long 2: " + randomLong2);
2.3 Generate random floating point numbers
(double limit): generates random floating point numbers in the range [0, limit).
(double min, double max): generates random floating point numbers in the range [min, max).
double randomDouble1 = (100.0); ("Random double 1: " + randomDouble1); double randomDouble2 = (10.0, 20.0); ("Random double 2: " + randomDouble2);
2.4 Generate random boolean values
(): Generate a random true or false.
boolean randomBoolean = (); ("Random boolean: " + randomBoolean);
2.5 Generate random strings
(int length): generates a random string of the specified length (including letters and numbers).
(int length): generates a random string of numericals of the specified length.
(int length): generates a random alphabetical string of the specified length.
String randomString = (10); ("Random string: " + randomString); String randomNumbers = (6); ("Random numbers: " + randomNumbers); String randomLetters = (8); ("Random letters: " + randomLetters);
2.6 Generate a random byte array
(int length): generates a random byte array of specified length.
byte[] randomBytes = (10); ("Random bytes: " + new String(randomBytes));
2.7 Randomly select elements from the collection
(List<T> list): Randomly select an element from the list.
(List<T> list, int count): Randomly select multiple elements from the list.
import ; import ; List<String> list = ("Apple", "Banana", "Cherry", "Date"); String randomElement = (list); ("Random element: " + randomElement); List<String> randomElements = (list, 2); ("Random elements: " + randomElements);
2.8 Generate a random UUID
(): Generate a random UUID.
String randomUUID = (); ("Random UUID: " + randomUUID);
3. Advanced usage
RandomUtil also supports custom random number generators (Random objects), as well as functions such as generating random dates and random colors.
import ; // Use custom Random objectsRandom customRandom = new Random(); int customRandomNumber = (customRandom, 10, 20); ("Custom random number: " + customRandomNumber);
4. Summary
RandomUtil is a very practical tool class in Hutool that can meet the needs of most random number generation. Its API is simple to design and easy to use, and is suitable for quickly implementing random number-related functions in Java projects. If you need more complex random number generation logic, you can combine Java native Random class or ThreadLocalRandom class to implement it.
The above is the detailed content of the tool class that uses Hutool to write a random number. For more information about Hutool random numbers, please pay attention to my other related articles!