Java How to generate a random String
By:Roy.LiuLast updated:2019-08-11
Few Java examples to show you how to generate a random alphanumeric String, with a fixed length.
1. Random [a-ZA-Z0-9]
1.1 Generate a random alphanumeric String [a-ZA-Z0-9], with a length of 8.
RandomExample.java
package com.mkyong; import java.security.SecureRandom; public class RandomExample { private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz"; private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase(); private static final String NUMBER = "0123456789"; private static final String DATA_FOR_RANDOM_STRING = CHAR_LOWER + CHAR_UPPER + NUMBER; private static SecureRandom random = new SecureRandom(); public static void main(String[] args) { System.out.println("String : " + DATA_FOR_RANDOM_STRING); for (int i = 0; i < 5; i++) { System.out.println("result : " + generateRandomString(8)); System.out.println("\n"); public static String generateRandomString(int length) { if (length < 1) throw new IllegalArgumentException(); StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { // 0-62 (exclusive), random returns 0-61 int rndCharAt = random.nextInt(DATA_FOR_RANDOM_STRING.length()); char rndChar = DATA_FOR_RANDOM_STRING.charAt(rndCharAt); // debug System.out.format("%d\t:\t%c%n", rndCharAt, rndChar); sb.append(rndChar); return sb.toString();
Output
String : abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 35 : J 6 : g 45 : T 45 : T 28 : C 41 : P 38 : M 53 : 1 result : JgTTCPM1 16 : q 16 : q 31 : F 34 : I 30 : E 58 : 6 1 : b 38 : M result : qqFIE6bM 6 : g 8 : i 34 : I 31 : F 59 : 7 37 : L 58 : 6 41 : P result : giIF7L6P 16 : q 31 : F 59 : 7 45 : T 55 : 3 30 : E 30 : E 59 : 7 result : qF7T3EE7 9 : j 14 : o 37 : L 49 : X 7 : h 59 : 7 53 : 1 17 : r result : joLXh71r
2. Apache Commons Lang
pom.xml
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
CommonLangExample.java
package com.mkyong; import org.apache.commons.lang3.RandomStringUtils; public class CommonLangExample { public static void main(String[] args) { System.out.println("\n[0-9]"); for (int i = 0; i < 5; i++) { System.out.println(RandomStringUtils.randomNumeric(10)); System.out.println("\n[a-zA-Z]"); for (int i = 0; i < 5; i++) { System.out.println(RandomStringUtils.randomAlphabetic(10)); System.out.println("\n[a-zA-Z0-9]"); for (int i = 0; i < 5; i++) { System.out.println(RandomStringUtils.randomAlphanumeric(10));
Output
[0-9] 9210279856 5752527714 9990371127 8726804704 3122660564 [a-zA-Z] RoBKeGEXjn qEirzwwvDB QblTQrRRCz qfLzdGWRFC TPKoFpdlPJ [a-zA-Z0-9] eLLKjFGjdS qCR6gRrS2R RSkDkWoEb7 zx42NHWosf KorCbSGDwQ
3. UUID
Generate a random String of 32 alphanumeric characters (hexadecimal) and 4 hyphens, read this UUID format
UUIDExample.java
package com.mkyong; import java.util.UUID; public class UUIDExample { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println(generateRandomStringByUUIDNoDash()); for (int i = 0; i < 10; i++) { System.out.println(generateRandomStringByUUIDNoDash()); public static String generateRandomStringByUUID() { return UUID.randomUUID().toString(); public static String generateRandomStringByUUIDNoDash() { return UUID.randomUUID().toString().replace("-", "");
Output
3d453240-cfe5-4295-9901-8dc6a8b8135b 510efd74-9d3b-4776-b75e-e9b98193f3ce 9fcf9ff9-2067-41a7-9f0f-2b8332efe68c 0f2ac57b-9bb4-4fa0-84e2-d0aaf5ca673a 882fe502-c94e-4e19-9dd7-6a1430e9b08a 73e5295a-72df-4acf-a4b8-52d1f23f8059 ef85c1c8-89a4-4bd0-b6cd-07ea7c4218c8 c3f8783a-6390-41f7-8744-134b0c9c025c d6ac1494-a7d1-48fe-8b26-4c0cbe6ad93f 756543e4-c66c-4e6d-8ea0-bbaf97e7197d 96fdf100f35049d598b154d314628caf 55b5ccd00a524baca2d64b36f36ffb50 680bf34291af46929ff0cbf6f6f2e928 4a4fdab30d8f453bb2a9c36b878ad985 82734287f9c444c288653dc90834d34e e976f4b5ebf243a780aa126dc7aa3901 7e0ef8d1cebd41bb9f20d677d0710b21 056bce8e8218481380683d195c4919b4 31cdc9f08c814e6fbe6f52d2a3b1eb36 9f8f4628012f4ec7a15997e40d52bc89
From:一号门
Previous:Java password generator example
COMMENTS