Table of Contents
In this tutorial, we will see how to generate random String in java.
There are many ways to generate random String.Let’s explore some of ways to generate random String.
Using simple java code with Random
You can use SecureRandom class to generate random String for you.
Let’s understand with the help of example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; import java.security.SecureRandom; public class RandomStringGeneratorMain { private static final String CHAR_LIST = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** * This method generates random string * @return */ public String generateRandomStringUsingSecureRandom(int length){ StringBuffer randStr = new StringBuffer(length); SecureRandom secureRandom = new SecureRandom(); for( int i = 0; i < length; i++ ) randStr.append( CHAR_LIST.charAt( secureRandom.nextInt(CHAR_LIST.length()) ) ); return randStr.toString(); } public static void main(String a[]){ RandomStringGeneratorMain rsgm = new RandomStringGeneratorMain(); System.out.println("Generating String of length 10: "+rsgm.generateRandomStringUsingSecureRandom(10)); System.out.println("Generating String of length 10: "+rsgm.generateRandomStringUsingSecureRandom(10)); System.out.println("Generating String of length 10: "+rsgm.generateRandomStringUsingSecureRandom(10)); System.out.println("Generating String of length 8: "+rsgm.generateRandomStringUsingSecureRandom(8)); System.out.println("Generating String of length 8: "+rsgm.generateRandomStringUsingSecureRandom(8)); System.out.println("Generating String of length 8: "+rsgm.generateRandomStringUsingSecureRandom(8)); System.out.println("Generating String of length 7: "+rsgm.generateRandomStringUsingSecureRandom(7)); System.out.println("Generating String of length 7: "+rsgm.generateRandomStringUsingSecureRandom(7)); System.out.println("Generating String of length 7: "+rsgm.generateRandomStringUsingSecureRandom(7)); } } |
Output:
Generating String of length 10: wSnjx6HNlv
Generating String of length 10: 4Wg9Iww0Is
Generating String of length 8: EdJmSrfC
Generating String of length 8: dAifHyQG
Generating String of length 8: HNnxieWg
Generating String of length 7: hQrqQ2L
Generating String of length 7: 0BWBtYI
Generating String of length 7: 3WStHON
Using Apache Common lang
You can use Apache Common lang to generate random String. It is quite easy to generate random String as you can use straight forward APIs to create random String.
Create AlphaNumericString
You can use RandomStringUtils.randomAlphanumeric method to generate alphanumeric random strn=ing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import org.apache.commons.lang3.RandomStringUtils; public class ApacheRandomStringMain { public static void main(String[] args) { System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphanumeric(10)); System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphanumeric(10)); System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphanumeric(10)); } } |
Output:
Generating String of length 10: urUnMHgAq9
Generating String of length 10: 8TddXvnDOV
Create random Alphabetic String
You can use RandomStringUtils.randomAlphabetic method to generate alphanumeric random strn=ing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; import org.apache.commons.lang3.RandomStringUtils; public class ApacheRandomStringMain { public static void main(String[] args) { System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphabetic(10)); System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphabetic(10)); System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphabetic(10)); } } |
Output:
Generating String of length 10: RWQlXuGbTk
Generating String of length 10: mmXRopdapr
That’s all about generating Random String in java.