Java: Generating a random String (password, booking reference, etc)

If this is intended to be used as a password generator, make sure to use SecureRandom instead of Random in the examples below. You might also want to use char[] instead of String for storing the result.

Random string

int length = 8;
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
             + "abcdefghijklmnopqrstuvwxyz"
             + "0123456789";
String str = new Random().ints(length, 0, chars.length())
                         .mapToObj(i -> "" + chars.charAt(i))
                         .collect(Collectors.joining());

With at least 1 digit and 1 special character

int length = 8;
String digits = "0123456789";
String specials = "~=+%^*/()[]{}/!@#$?|";
String all = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           + "abcdefghijklmnopqrstuvwxyz"
           + digits + specials;
Random rnd = new Random();
List<String> result = new ArrayList<>();
Consumer<String> appendChar = s ->
        result.add("" + s.charAt(rnd.nextInt(s.length())));
appendChar.accept(digits);
appendChar.accept(specials);
while (result.size() < length)
    appendChar.accept(all);
Collections.shuffle(result, rnd);
String str = String.join("", result);

Apache Commons Lang

See also the various methods in RandomStringUtils from Apache Commons Lang:

random(int count) Creates a random string whose length is the number of characters specified.
random(int count,
       boolean letters,
       boolean numbers)
Creates a random string whose length is the number of characters specified.
random(int count,
       char... chars)
Creates a random string whose length is the number of characters specified.
random(int count,
       int start,
       int end,
       boolean letters,
       boolean numbers)
Creates a random string whose length is the number of characters specified.
random(int count,
       int start,
       int end,
       boolean letters,
       boolean numbers,
       char... chars)
Creates a random string based on a variety of options, using default source of randomness.
random(int count,
       int start,
       int end,
       boolean letters,
       boolean numbers,
       char[] chars,
       Random random)
Creates a random string based on a variety of options, using supplied source of randomness.
random(int count,
       String chars)
Creates a random string whose length is the number of characters specified.
randomAlphabetic(int count) Creates a random string whose length is the number of characters specified.
randomAlphabetic(
        int minLengthInclusive,
        int maxLengthExclusive)
Creates a random string whose length is between the inclusive minimum and the exclusive maximum.
randomAlphanumeric(int count) Creates a random string whose length is the number of characters specified.
randomAlphanumeric(
        int minLengthInclusive,
        int maxLengthExclusive)
Creates a random string whose length is between the inclusive minimum and the exclusive maximum.
randomAscii(int count) Creates a random string whose length is the number of characters specified.
randomAscii(
        int minLengthInclusive,
        int maxLengthExclusive)
Creates a random string whose length is between the inclusive minimum and the exclusive maximum.
randomGraph(int count) Creates a random string whose length is the number of characters specified.
randomGraph(
        int minLengthInclusive,
        int maxLengthExclusive)
Creates a random string whose length is between the inclusive minimum and the exclusive maximum.
randomNumeric(int count) Creates a random string whose length is the number of characters specified.
randomNumeric(
        int minLengthInclusive,
        int maxLengthExclusive)
Creates a random string whose length is between the inclusive minimum and the exclusive maximum.
randomPrint(int count) Creates a random string whose length is the number of characters specified.
randomPrint(
        int minLengthInclusive,
        int maxLengthExclusive)
Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Comments

Be the first to comment!