RandomUtil.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.fdkankan.common.util;
  2. import java.util.Random;
  3. import java.util.UUID;
  4. public class RandomUtil {
  5. public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
  6. "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  7. "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
  8. "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
  9. "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
  10. "W", "X", "Y", "Z" };
  11. public static String generateShortUuid() {
  12. StringBuffer shortBuffer = new StringBuffer();
  13. String uuid = UUID.randomUUID().toString().replace("-", "");
  14. int index = 0;
  15. for (int i = 0; i < 11; i++) {
  16. index = i;
  17. if(index >= 8){
  18. index = i % 8;
  19. uuid = UUID.randomUUID().toString().replace("-", "");
  20. }
  21. String str = uuid.substring(index * 4, index * 4 + 4);
  22. int x = Integer.parseInt(str, 16);
  23. shortBuffer.append(chars[x % 0x3E]);
  24. }
  25. return shortBuffer.toString();
  26. }
  27. /**
  28. * 获取随机字符串(相机sn码)
  29. *
  30. * @param num
  31. * @return
  32. */
  33. public static String getRandomNum(Integer num) {
  34. String base = "0123456789ABCDEF";
  35. Random random = new Random();
  36. StringBuffer sb = new StringBuffer();
  37. for (int i = 0; i < num; i++) {
  38. int number = random.nextInt(base.length());
  39. sb.append(base.charAt(number));
  40. }
  41. return sb.toString();
  42. }
  43. }