27

I have a short utility in which I am generating a UUID using randomUUID().

String uuid = UUID.randomUUID().toString();

However, the uuid generated is too long which is 36 in length.

Is there any way I can reduce the length of the UUID from 36 to near 16 or make the UUID length dynamic?

PKS
  • 618
  • 1
  • 7
  • 19
AppleBud
  • 1,501
  • 8
  • 31
  • 47
  • 13
    A "shorter UUID" is no longer a UUID, by the way. –  Jan 08 '14 at 11:48
  • possible duplicate of [generating UUID but only for 8 characters](http://stackoverflow.com/questions/4267475/generating-uuid-but-only-for-8-characters) – GaryF Jan 08 '14 at 11:48
  • 1
    The avoidance of Hash or UUID collisions is proportional to the size the value space and the square of the number of elements. Decreasing to 16 random hex characters would most likely lose a UUID's effective guarantee of uniqueness. – Thomas W Aug 27 '20 at 05:25

7 Answers7

36

If you don't need it to be unique, you can use any length you like.

For example, you can do this.

Random rand = new Random();
char[] chars = new char[16];
for(int i=0;i<chars.length;i++) {
    chars[i] = (char) rand.nextInt(65536);
    if (!Character.isValidCodePoint(chars[i]))
        i--;
}
String s = new String(chars);

This will give you almost the same degree of randomness but will use every possible character between \u0000 and \ufffd

If you need printable ASCII characters you can make it as short as you like but the likelihood of uniqueness drops significantly. What can do is use base 36 instead of base 16

UUID uuid = UUID.randomUUID();
String s = Long.toString(uuid.getMostSignificantBits(), 36) + '-' + Long.toString(uuid.getLeastSignificantBits(), 36);

This will 26 characters on average, at most 27 character.

You can use base64 encoding and reduce it to 22 characters.

If you use base94 you can get it does to 20 characters.

If you use the whole range of valid chars fro \u0000 to \ufffd you can reduce it to just 9 characters or 17 bytes.

If you don't care about Strings you can use 16, 8-bit bytes.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hi, @Peter Lawrey. I want to keep my numbers unique and within the range of integers in Java. I have modified your code according to my need, which is `UUID uuid = UUID.randomUUID();` `String idString = Long.toString(uuid.getMostSignificantBits(), 36).replaceAll("-", "").replaceAll("[a-z]","");` `int id; System.out.println(id = Integer.parseInt(idString));` but when I was testing my code, there was a repetitive number within 12 numbers. So can you please suggest me any foolproof solution for this? – Vibhav Chaddha Apr 03 '17 at 10:27
5
String uuid = String.format("%040d", new BigInteger(UUID.randomUUID().toString().replace("-", ""), 16));

String uuid16digits = uuid.substring(uuid.length() - 16);

This will return the last 16 digits of actual uuid.

Derrick
  • 3,669
  • 5
  • 35
  • 50
Pramin Senapati
  • 136
  • 1
  • 4
2

Convert it from base 16(0-9,A-F) to base 36(0-9,A-Z).. You could go to base 62 (0-9, A-Z, a-z) but if you need to read it over a phone or something then this can be error prone. https://github.com/salieri/uuid-encoder is a lib that might work for you...

Also this means you still have a GUID -you haven't truncated it like the other answers

baradhili
  • 514
  • 1
  • 7
  • 27
1

You can use the substring method to decrease the string length while generating uuid.

UUID.randomUUID().toString().substring(0, 5)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

You could evaluate using the ${__time()} function

0

The following snippet is a dynamic UUID code. By default, the legth of the UUID depends on bits but the function randomly chooses characters from the UUID

 public String myUUID(int length) {
    String allChars = UUID.randomUUID().toString().replace("-", "");
    Random random = new Random();
    char[] otp = new char[length];
    for (int i = 0; i < length; i++) {
      otp[i] =
          allChars.charAt(random.nextInt(allChars.length()));
    }
    return String.valueOf(otp);
  }
PKS
  • 618
  • 1
  • 7
  • 19
-2

Yes,You can create by using this function.

public static String shortUUID() {
  UUID uuid = UUID.randomUUID();
  long l = ByteBuffer.wrap(uuid.toString().getBytes()).getLong();
  return Long.toString(l, Character.MAX_RADIX);
}
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Pritam Panja
  • 39
  • 1
  • 5