You can use RandomStringUtils
import org.apache.commons.lang.RandomStringUtils;
public class RandomStringUtilsTrial {
public static void main(String[] args) {
System.out.print("8 char string >>>");
System.out.println(RandomStringUtils.random(8, true, true));
}
}
Answer from bilash.saha on Stack Overflow Top answer 1 of 12
28
You can use RandomStringUtils
import org.apache.commons.lang.RandomStringUtils;
public class RandomStringUtilsTrial {
public static void main(String[] args) {
System.out.print("8 char string >>>");
System.out.println(RandomStringUtils.random(8, true, true));
}
}
2 of 12
15
The uniqueness property depends on the scope in which you use it. Java can certainly generate random strings, although if you want a universally unique identifier you can use UUID class.
String unique = UUID.randomUUID().toString();
GitHub
github.com › aventrix › jnanoid
GitHub - aventrix/jnanoid: A unique string ID generator for Java. · GitHub
It uses a url-friendly alphabet (A-Za-z0-9_-), a secure random number generator, and generates a unique ID with 21 characters. String id = NanoIdUtils.randomNanoId(); // "ku-qLNv1wDmIS5_EcT3j7"
Starred by 516 users
Forked by 58 users
Languages Java
CodeJava
codejava.net › coding › generate-random-strings-examples
Generate Random Strings in Java Examples
January 13, 2023 - You can use the java.util.UUID class to generate random strings that are kind of Universally Unique Identifier (UUID).
Mkyong
mkyong.com › home › java › java – how to generate a random string
Java - How to generate a random String - Mkyong.com
May 4, 2019 - Generate a random String of 32 alphanumeric characters (hexadecimal) and 4 hyphens, read this UUID format ... 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("-", ""); } }
Javacodepoint
javacodepoint.com › generate-random-string-in-java
Java Random String | Generate random Strings - Javacodepoint
August 11, 2023 - Write a Java program to find the common elements in two given ArrayLists. ArrayList partition into two separate lists of odd/even numbers. Write a program that extracts a sublist from an ArrayList, given a start and end index. Merge two sorted ArrayLists into a single sorted ArrayList. Find unique string in ArrayList with its frequency
Programiz
programiz.com › java-programming › examples › generate-random-string
Java Program to Create random strings
Using the random index number, we have generated the random character from the string alphabet. We then used the StringBuilder class to append all the characters together. If we want to change the random string into lower case, we can use the toLowerCase() method of the String.
Medium
medium.com › @mounikakurapati17 › how-to-generate-unique-number-in-java-ab3acfa82973
How To Generate Unique Number in Java | by Mounika | Medium
December 5, 2022 - To produce a positive unique long value (with extremely low chances of repetition) in Java, using the well-known UUID library, which is also widely used for generating unique strings, a UUID unique long number of the desired length can be obtained as below.
Top answer 1 of 11
441
Create a UUID.
String uniqueID = UUID.randomUUID().toString();
2 of 11
58
If you want short, human-readable IDs and only need them to be unique per JVM run:
private static long idCounter = 0;
public static synchronized String createID()
{
return String.valueOf(idCounter++);
}
Edit: Alternative suggested in the comments - this relies on under-the-hood "magic" for thread safety, but is more scalable and just as safe:
private static AtomicLong idCounter = new AtomicLong();
public static String createID()
{
return String.valueOf(idCounter.getAndIncrement());
}
Coderanch
coderanch.com › t › 565034 › java › Create-Unique-Character-String
How to Create a Unique 16 Character String (Java in General forum at Coderanch)
If it must be unique (that is, even the astronomically small chance of duplicates is unacceptable), and it can't be guessable, then your only real options are: 1) Randomly generate 16 characters, and then compare against a list of already generated IDs, and if it's a duplicate, try again.
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Generating unique IDs
A MessageDigest takes any input, and produces a String which: ... does not uniquely identify the input; however, similar input will produce dissimilar message digests MessageDigest is often used as a checksum, for verifying that data has not been altered since its creation. ... import java.security.SecureRandom; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public final class GenerateId { public static void main (String...
Liberian Geek
liberiangeek.net › home › how-to/tips › how to generate a random string in java?
How to Generate a Random String in Java? | Liberian Geek
December 19, 2023 - The output confirms that a unique alphabetic random String has been generated after each execution cycle: The custom regular expression can be used with the “Random” class to generate a random String of specific characters. The regular expression method gives more freedom to choose the type of characters users want to enter in their random String: import java.util.Random; import java.nio.charset.Charset; class javabeat { static String randomizer(int strSize) { byte[] tempArray = new byte[255]; new Random().nextBytes(tempArray); String entireRanStr = new String(tempArray, Charset.forName("U
Coderanch
coderanch.com › t › 448702 › java › Creating-Unique-ID-String
Creating a Unique ID from a String. (Java in General forum at Coderanch)
In Java, how can a create a Unique ID from a particular string? I have gone through the UUID class but could not find anything, also googled this but to find nothing. Can any one please Help me on this? ... Try permutation and combination of a string element appended with some Randomized generated string/number.