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