How to create a random string in Java

I have an object called Student , and it has studentName , studentId , studentAddress , etc. For studentId I have to create a random string consisting of seven numeric characters, for example.

 studentId = getRandomId(); studentId = "1234567" <-- from the random generator. 

And I have to make sure there is no duplicate id.

+70
java string char random
May 19 '10 at 8:18
source share
7 answers

Generating a random string of characters is simple - just use java.util.Random and a string containing all the characters you want to use, for example

 public static String generateString(Random rng, String characters, int length) { char[] text = new char[length]; for (int i = 0; i < length; i++) { text[i] = characters.charAt(rng.nextInt(characters.length())); } return new String(text); } 

Now, for uniqueness, you will need to store the generated lines somewhere. How you do this will depend on the rest of your application.

+129
May 19 '10 at 8:23 a.m.
source share

It is very nice:

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/RandomStringUtils.html

If you want uniqueness (with a high probability), consider using MD5 or SHA hashes.

+53
May 19 '10 at 8:53 a.m.
source share

You can also use the UUID class from the java.util package, which returns a random uuid of 32-bit String characters.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html

+17
May 19 '10 at 11:38
source share
 Random ran = new Random(); int top = 3; char data = ' '; String dat = ""; for (int i=0; i<=top; i++) { data = (char)(ran.nextInt(25)+97); dat = data + dat; } System.out.println(dat); 
+5
Jan 21 2018-12-12T00:
source share

I think the following class code will help you. It supports multithreading, but you can make some improvements, such as removing the synchronization block and synchronizing with the getRandomId () method.

 public class RandomNumberGenerator { private static final Set<String> generatedNumbers = new HashSet<String>(); public RandomNumberGenerator() { } public static void main(String[] args) { final int maxLength = 7; final int maxTry = 10; for (int i = 0; i < 10; i++) { System.out.println(i + ". studentId=" + RandomNumberGenerator.getRandomId(maxLength, maxTry)); } } public static String getRandomId(final int maxLength, final int maxTry) { final Random random = new Random(System.nanoTime()); final int max = (int) Math.pow(10, maxLength); final int maxMin = (int) Math.pow(10, maxLength-1); int i = 0; boolean unique = false; int randomId = -1; while (i < maxTry) { randomId = random.nextInt(max - maxMin - 1) + maxMin; synchronized (generatedNumbers) { if (generatedNumbers.contains(randomId) == false) { unique = true; break; } } i++; } if (unique == false) { throw new RuntimeException("Cannot generate unique id!"); } synchronized (generatedNumbers) { generatedNumbers.add(String.valueOf(randomId)); } return String.valueOf(randomId); } } 
+2
May 20 '10 at 6:14 a.m.
source share

The first question you need to ask is whether you really need an identifier to be random. Sometimes consecutive identifiers are good enough.

Now, if you want this to be random, we first notice that the generated sequence of numbers that do not contain duplicates cannot be called randomly .: p Now that we have it fixed, the fastest way to do this is to have a Hashtable or HashMap containing all identifiers already created. Whenever a new identifier is generated, check it on the hash table, recreate if the identifier already exists. This usually works well if the number of students is much less than the number of identifiers. If not, you are experiencing deeper problems, as the likelihood of having to recover an identifier increases, P (generate a new identifier) ​​= number_of_id_already_generated / number_of_all_possible_ids. In this case, check the first paragraph (do you need the ID to be random?).

Hope this helps.

+1
May 19 '10 at 8:26 a.m.
source share

A lot of possibilities...

Do you know how to correctly generate an integer? So you can create char from it ... (ex 65 → A)

It depends on what you need, the level of randomness related to security ... but for a school project, I assume that getting the UUID substring will match :)

+1
May 19 '10 at 12:16
source share



All Articles