1. Introduction

Random hexadecimal values in applications can serve as unique identifiers for various purposes like database entries, session tokens, or game mechanics. They can also contribute to cryptographic security and testing processes.

In this quick tutorial, we’ll learn about different ways of generating random hexadecimal values in Java.

2. Using java.util.Random

The Random class in java.util provides a simple way to generate random Integer and Long values. We can convert these to hex values.

2.1. Generate an Unbounded Hex Value

Let’s start with generating an unbounded Integer and then converting it into a hex string using the toHexString() method:

String generateUnboundedRandomHexUsingRandomNextInt() {
    Random random = new Random();
    int randomInt = random.nextInt();
    return Integer.toHexString(randomInt);
}

If our application needs a larger hexadecimal value, we can use the nextLong() method from the Random class to generate a random Long value. This value can then be converted to a hexadecimal string using its toHexString() method:

String generateUnboundedRandomHexUsingRandomNextLong() {
    Random random = new Random();
    long randomLong = random.nextLong();
    return Long.toHexString(randomLong);
}

We can also use String.format() method to convert it into hex String. This method allows us to create formatted strings using placeholders and format specifiers:

String generateRandomHexWithStringFormatter() {
    Random random = new Random();
    int randomInt = random.nextInt();
    return String.format("%02x", randomInt);
}

2.2. Generate a Bounded Hex Value

We can use the nextInt() method from the Random class with a parameter to generate a bounded random Integer, which we can then convert to hexadecimal String:

String generateRandomHexUsingRandomNextIntWithInRange(int lower, int upper) {
    Random random = new Random();
    int randomInt = random.nextInt(upper - lower) + lower;
    return Integer.toHexString(randomInt);
}

3. Using java.security.SecureRandom

For applications requiring cryptographically secure random numbers, we should consider using the SecureRandom class. The SecureRandom class inherits from the java.util.Random class, so we can use the nextInt() method to generate both bounded and unbounded integers.

3.1. Generate an Unbounded Secure Hex Value

Let’s generate a random integer using the nextInt() method and convert it to a hex value:

String generateRandomHexUsingSecureRandomNextInt() {
    SecureRandom secureRandom = new SecureRandom();
    int randomInt = secureRandom.nextInt();
    return Integer.toHexString(randomInt);
}

We can also generate a random Long value using the nextLong() method and convert it to a hex value:

String generateRandomHexUsingSecureRandomNextLong() {
    SecureRandom secureRandom = new SecureRandom();
    long randomLong = secureRandom.nextLong();
    return Long.toHexString(randomLong);
}

3.2. Generate a Bounded Secure Hex Value

Let’s generate a random Integer within a range and convert it to a hex value:

String generateRandomHexUsingSecureRandomNextIntWithInRange(int lower, int upper) {
    SecureRandom secureRandom = new SecureRandom();
    int randomInt = secureRandom.nextInt(upper - lower) + lower;
    return Integer.toHexString(randomInt);
}

4. Using Apache commons-math3

Apache commons-math3 provides a utility class RandomDataGenerator, which offers more options to generate random values. This class provides several utility methods to generate random data.

To use it, let’s first add the dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.6.1</version>
</dependency>

The latest version of the dependency can be checked here.

4.1. Generate a Bounded Random Hex Value

We can generate random integers using the nextInt() method from the RandomDataGenerator class. This method is similar to the Random and SecureRandom classes, but provides additional flexibility and functionality:

String generateRandomHexWithCommonsMathRandomDataGeneratorNextIntWithRange(int lower, int upper) {
    RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    int randomInt = randomDataGenerator.nextInt(lower, upper);
    return Integer.toHexString(randomInt);
}

4.2. Generate a Secure Bounded Random Hex Value

We can also generate a secure Integer for cryptographically secure applications and convert it to a hex String to get a secure random hex value:

String generateRandomHexWithCommonsMathRandomDataGeneratorSecureNextIntWithRange(int lower, int upper) {
    RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    int randomInt = randomDataGenerator.nextSecureInt(lower, upper);
    return Integer.toHexString(randomInt);
}

4.3. Generate a Random Hex String of a Given Length

We can use the RandomDataGenerator class to generate a random hex String of a given length:

String generateRandomHexWithCommonsMathRandomDataGenerator(int len) {
    RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    return randomDataGenerator.nextHexString(len);
}

This method simplifies the process by directly generating a hexadecimal String of the desired length. Using nextHex() is more straightforward than generating integers and converting them, providing a direct way to obtain a random hexadecimal String.

4.4. Generate a Secure Random Hex String of a Given Length

For security-sensitive applications, we can also generate a secure hex string using the nextSecureHexString() method:

String generateSecureRandomHexWithCommonsMathRandomDataGenerator(int len) {
    RandomDataGenerator randomDataGenerator = new RandomDataGenerator();
    return randomDataGenerator.nextSecureHexString(len);
}

This method uses a secure random number generator to produce a hexadecimal String, making it ideal for applications where security is a critical concern.

5. Conclusion

In this article, we learned several methods to generate random hexadecimal values. By leveraging these methods, we can ensure our applications generate robust and reliable random hexadecimal values tailored to our specific needs.

The full source code of this article is available over on GitHub.