103

As the title suggest I need to create a random, 17 characters long, ID. Something like "AJB53JHS232ERO0H1". The order of letters and numbers is also random. I thought of creating an array with letters A-Z and a 'check' variable that randoms to 1-2. And in a loop;

Randomize 'check' to 1-2. If (check == 1) then the character is a letter. Pick a random index from the letters array. else Pick a random number. 

But I feel like there is an easier way of doing this. Is there?

2
  • 2
    stackoverflow.com/questions/41107/… Commented Dec 12, 2013 at 6:36
  • you can put your letters and digits into an array and then randomly choose elements from it until you reach your desired size. Commented Dec 12, 2013 at 6:37

4 Answers 4

145

Here you can use my method for generating Random String

protected String getSaltString() { String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; StringBuilder salt = new StringBuilder(); Random rnd = new Random(); while (salt.length() < 18) { // length of the random string. int index = (int) (rnd.nextFloat() * SALTCHARS.length()); salt.append(SALTCHARS.charAt(index)); } String saltStr = salt.toString(); return saltStr; } 

The above method from my bag using to generate a salt string for login purpose.

Sign up to request clarification or add additional context in comments.

6 Comments

May I suggest to replace StringBuffer by a StringBuilder, no need to have a thread-safe impl. here
Or just create a char[] given that you know exactly how long it will be. No need to append anything. I'd also use Random.nextInt rather than calling nextFloat and multiplying it by the length.
Note that the average amount of numbers won't be the same as the amount of letters.
@RC. Yes. I'm using it in a servlet environment. So need it. Otherwise a StringBuilder
One thing to keep in mind if you use this is that, the Random class is sudo random and not a true random. Tried to use this to print 400 Strings to a file and found that it wasn't as random as I wanted. :(
|
130

RandomStringUtils from Apache commons-lang might help:

RandomStringUtils.randomAlphanumeric(17).toUpperCase() 

2017 update: RandomStringUtils has been deprecated, you should now use RandomStringGenerator.

5 Comments

Consider updating the answer as Apache replaced it with RandomStringGenerator in commons-text.
You can also use RandomStringUtils.random(length, useLetters, useNumbers) where length is int while useLetters and useNumbers are boolean values.
Superb,Single line code,we require this type of codes.
@NeriaNachum where are you seeing that RandomStringUtils has been replaced/deprecated? I'm just seeing "RandomStringUtils is intended for simple use cases. For more advanced use cases consider using Apache Commons Text's RandomStringGenerator instead." here
Using RandomStringGenerator (from apachae common) you can produce a random String of desired length. Following snippet will generate a String of length between 5 - 15 ( both inclusive) // char [][] pairs = {{'a','z'},{'A','Z'},{'0','9'}}; RandomStringGenerator randomStringGenerator = new RandomStringGenerator.Builder() .withinRange(pairs) .build(); String randomString = randomStringGenerator.generate(5, 15); //
23

Three steps to implement your function:

Step#1 You can specify a string, including the chars A-Z and 0-9.

Like.

 String candidateChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 

Step#2 Then if you would like to generate a random char from this candidate string. You can use

 candidateChars.charAt(random.nextInt(candidateChars.length())); 

Step#3 At last, specify the length of random string to be generated (in your description, it is 17). Writer a for-loop and append the random chars generated in step#2 to StringBuilder object.

Based on this, here is an example

public class RandomTest { public static void main (String[] args) { System.out.println (generateRandomChars ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 17)); } /** * * @param candidateChars the candidate chars * @param length the number of random chars to be generated * * @return */ public static String generateRandomChars (String candidateChars, int length) { StringBuilder sb = new StringBuilder (); Random random = new Random (); for (int i = 0; i < length; i ++) { sb.append (candidateChars.charAt (random.nextInt (candidateChars .length ()))); } return sb.toString (); } } 

Comments

10

You can easily do that with a for loop,

public static void main(String[] args) { String aToZ="ABCD.....1234"; // 36 letter. String randomStr=generateRandom(aToZ); } private static String generateRandom(String aToZ) { Random rand=new Random(); StringBuilder res=new StringBuilder(); for (int i = 0; i < 17; i++) { int randIndex=rand.nextInt(aToZ.length()); res.append(aToZ.charAt(randIndex)); } return res.toString(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.