Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. UUID myuuid = UUID.randomUUID();
  2. long highbits = myuuid.getMostSignificantBits();
  3. long lowbits = myuuid.getLeastSignificantBits();
  4. System.out.println("My UUID is: " + highbits + " " + lowbits);
  5.  
  6. long hi = id.getMostSignificantBits();
  7. long lo = id.getLeastSignificantBits();
  8. byte[] bytes = ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
  9. BigInteger big = new BigInteger(bytes);
  10. String numericUuid = big.toString().replace('-','1'); // just in case
  11.  
  12. final int[] pattern = { 8, 4, 4, 4, 12 };
  13.  
  14. final int[] versionBit = { 2, 0 }; /* 3rd group, first bit */
  15. final int version = 4;
  16.  
  17. final int[] reservedBit = { 3, 0 }; /* 4rd group, first bit */
  18. final int reserved = 8; /* 8, 9, A, or B */
  19.  
  20. Random rand = new Random();
  21.  
  22. String numericUuid = "";
  23.  
  24. for (int i = 0; i < pattern.length; i++) {
  25. for (int j = 0; j < pattern[i]; j++) {
  26. if (i == versionBit[0] && j == versionBit[1])
  27. numericUuid += version;
  28. else if (i == reservedBit[0] && j == reservedBit[1])
  29. numericUuid += reserved;
  30. else
  31. numericUuid += rand.nextInt(10);
  32. }
  33.  
  34. numericUuid += "-";
  35. }
  36.  
  37. UUID uuid = UUID.fromString(numericUuid.substring(0, numericUuid.length() - 1));
  38. System.out.println(uuid);
  39.  
  40. UUID uuid = UUID.randomUUID();
  41.  
  42. while (StringUtils.containsAny(uuid.toString(), new char[] { 'a', 'b', 'c', 'd', 'e', 'f' })) {
  43. uuid = UUID.randomUUID();
  44. }
  45.  
  46. System.out.println(uuid);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement