RandomUtil.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package com.fdkankan.common.util;
  2. import java.util.*;
  3. public class RandomUtil {
  4. public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
  5. "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  6. "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
  7. "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
  8. "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
  9. "W", "X", "Y", "Z" };
  10. public static String generateShortUuid() {
  11. StringBuffer shortBuffer = new StringBuffer();
  12. String uuid = UUID.randomUUID().toString().replace("-", "");
  13. int index = 0;
  14. for (int i = 0; i < 9; i++) {
  15. index = i;
  16. if(index >= 8){
  17. index = i % 8;
  18. uuid = UUID.randomUUID().toString().replace("-", "");
  19. }
  20. String str = uuid.substring(index * 4, index * 4 + 4);
  21. int x = Integer.parseInt(str, 16);
  22. shortBuffer.append(chars[x % 0x3E]);
  23. }
  24. return shortBuffer.toString();
  25. }
  26. /**
  27. * 获取随机字符串
  28. *
  29. * @param num
  30. * @return
  31. */
  32. public static String getRandomNum(Integer num) {
  33. String base = "0123456789ABCDEF";
  34. Random random = new Random();
  35. StringBuffer sb = new StringBuffer();
  36. for (int i = 0; i < num; i++) {
  37. int number = random.nextInt(base.length());
  38. sb.append(base.charAt(number));
  39. }
  40. return sb.toString();
  41. }
  42. public static void main(String[] args) {
  43. // Set<String> set = new HashSet<>();
  44. //
  45. // while(true){
  46. // if(set.size() == 650){
  47. // break;
  48. // }
  49. // set.add("4DKKPRO_07" + getRandomNum(7));
  50. //// set.add("4DKKLITE_11" + getRandomNum(7));
  51. // }
  52. //
  53. // for(String s : set){
  54. // System.out.println(s);
  55. // }
  56. // String[] nums = num.split(",");
  57. //
  58. // List arrayList = new ArrayList();
  59. // for(String a:nums){
  60. // arrayList.add(a);
  61. // }
  62. //
  63. // for(int i = 0;i<100;i++ ){
  64. // String numss = "07"+getRandomNum(7);
  65. // if(!arrayList.contains(numss)){
  66. // System.out.println("07"+getRandomNum(7));
  67. // }
  68. //
  69. // }
  70. // System.out.println(generateShortUuid());
  71. }
  72. }