
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate Random Hexadecimal Value in Java
In this article, we will learn how to generate random hexadecimal number values in Java. Random hexadecimal values are used in many applications, like unique identifiers, cryptographic keys, session tokens, or colour codes. We will discuss three different ways to generate random hexadecimal values in Java ?
- Using Random - A simple, fast method to make random hexadecimal values.
- Using SecureRandom - Best for secure random hexadecimal numbers.
- Using BigInteger - Helps produce large random hexadecimal numbers.
1. Using Random
In this approach, we use the Random class to produce random numbers from 0 to 15. After generating a random integer value, we convert it into a hexadecimal value. The method is simple and fast but does not ensure cryptographic security, making this unsuitable for applications like encrypting keys, passwords, etc.
Steps to Implement
Follow the steps below to implement this method effectively.
Step 1: Take length as input.
Step 2: Initialize a StringBuilder.
Step 3: Create an instance of the Random class and generate a random integer value between 0 and 15 using random.nextInt(16).
Step 4: Convert the integer to hexadecimal using the Integer.toHexString(value) method.
Step 5: Append this value to the string builder.
Step 6: Repeat step 3 to step 5 till the hexadecimal reaches the required length.
Step 7: Return the final hex value.
Implementation code
Below is the Java program using the Random class to generate random hexadecimal numbers.
import java.util.Random; public class tutorialspoint { public static void main(String[] args) { String hexvalue = hexadecimal(4); // 4 is the length of the hexaedcimal number System.out.println("Random hexadecimal: " + hexvalue); } public static String hexadecimal(int length) { Random random = new Random(); StringBuilder hexstring = new StringBuilder(); for (int i = 0; i < length; i++) { int value = random.nextInt(16); // Generate a number from 0 to 15 hexstring.append(Integer.toHexString(value)); } return hexstring.toString(); } }
Output
Random hexadecimal: 45a9
2. Using SecureRandom
This approach uses the SecureRandom class to generate random numbers. This method is safer than the previously discussed random class and is used in security essential applications like encryption keys and authentication tokens. The implementation steps are almost the same as the previous approach. The only difference is we create an instance of the SecureRandom class instead of the Random class.
Implementation code
Below is the Java program using the SecureRandom class to generate secure random hexadecimal numbers.
import java.security.SecureRandom; public class tutorialspoint { public static void main(String[] args) { String hexvalue = hexadecimal(6); //6 is the length of random hex number System.out.println("Random hexadecimal: " + hexvalue); } public static String hexadecimal(int length) { SecureRandom random = new SecureRandom(); StringBuilder hexstring = new StringBuilder(); for (int i = 0; i < length; i++) { int value = random.nextInt(16); hexstring.append(Integer.toHexString(value)); } return hexstring.toString(); } }
Output
Random hexadecimal: 5e7ef8
3. Using BigInteger
This method uses BigInteger to create large random hexadecimal numbers. It works very well for generating secure and large random values because BigInteger can handle numbers of very large size.
Steps to Implement
Follow the steps below to implement this method effectively.
Step 1: Create an instance of the SecureRandom class.
Step 2: Using the BigInteger(bitlength, random) method, create a large random number. It's important to note that we take input length as bits. 1 hex digit equals 4 bits. So if you want to get hexadecimal of length of 10 we have to give input as 40 bits.
Step 3: Convert the BigInteger value into a hexadecimal string using toString(16).
Step 4: Return the hexadecimal value.
Implementation code
Below is the Java program using BigInteger to generate large and secure random hexadecimal numbers.
import java.math.BigInteger; import java.security.SecureRandom; public class tutorialspoint { public static void main(String[] args) { String hexvalue = hexadecimal(80); // Generates a 80-bit hex number (1 hex digit is 4 bit so it will generate number with 20 digits) System.out.println("Random hexadecimal: " + hexvalue); } public static String hexadecimal(int bitlength) { SecureRandom random = new SecureRandom(); BigInteger bigint = new BigInteger(bitlength, random); return bigint.toString(16); } }
Output
Random hexadecimal: e39f787ed84ae063f75b