-1

Is there nay way to generate random String using this code but with size 42 symbols?

public static final String RANDOM_STRING = UUID.randomUUID().toString();
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

0

No you cannot create more than 36 characters as UUID is 128 bit in length. More info can be found here.

java.util.UUID.randomUUID().toString() length

If you need , will have to implement your own functionality to generate a random string having 6 characters. For that you can use Apache Commons Lang package. Then concatenate it with the UUID string.

UVM
  • 9,776
  • 6
  • 41
  • 66
0

In order to get true randomness for a UUID of whatever characters longer than the typically generated 36 characters in length then you can pull a piece off (whatever length you want) of a second generated UUID. This can be all done in the same line of code:

String RANDOM_STRING = UUID.randomUUID().toString() + "-" + 
                UUID.randomUUID().toString().substring(0, 5);

System.out.println("The UUID String is: " + RANDOM_STRING + 
        " and it is " + RANDOM_STRING.length() + " characters long.");

Keep in mind though...it would not be a true UUID.

Example output:

The UUID String is: c8e1aceb-ee63-49f5-bd95-e6f3eebced20-d5c39 and it is 42 characters long.
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22