93

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

4 Answers 4

138

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.

6
  • 1
    May I suggest to replace StringBuffer by a StringBuilder, no need to have a thread-safe impl. here
    – user180100
    Dec 12, 2013 at 6:42
  • 12
    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.
    – Jon Skeet
    Dec 12, 2013 at 6:44
  • 2
    Note that the average amount of numbers won't be the same as the amount of letters. Dec 12, 2013 at 6:44
  • @RC. Yes. I'm using it in a servlet environment. So need it. Otherwise a StringBuilder Dec 12, 2013 at 6:47
  • 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. :(
    – Sharp Dev
    Dec 26, 2018 at 1:46
120

RandomStringUtils from Apache commons-lang might help:

RandomStringUtils.randomAlphanumeric(17).toUpperCase()

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

5
  • 3
    Consider updating the answer as Apache replaced it with RandomStringGenerator in commons-text. Aug 8, 2017 at 8:23
  • 2
    You can also use RandomStringUtils.random(length, useLetters, useNumbers) where length is int while useLetters and useNumbers are boolean values. Sep 22, 2018 at 11:42
  • 1
    Superb,Single line code,we require this type of codes.
    – Rajesh Om
    Nov 13, 2018 at 7:41
  • 3
    @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 May 29, 2020 at 14:20
  • 1
    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); //
    – Ajay Singh
    Jun 8, 2022 at 10:28
20

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();
}

}
8

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();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.