1 package com.zuidaima.core.util; 2 3 import java.util.Random; 4 5 public class RandomUtil { 6 public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 7 public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 8 public static final String NUMBERCHAR = "0123456789"; 9 10 /** 11 * 返回一个定长的随机字符串(只包含大小写字母、数字) 12 * 13 * @param length 14 * 随机字符串长度 15 * @return 随机字符串 16 */ 17 public static String generateString(int length) { 18 StringBuffer sb = new StringBuffer(); 19 Random random = new Random(); 20 for (int i = 0; i < length; i++) { 21 sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); 22 } 23 return sb.toString(); 24 } 25 26 /** 27 * 返回一个定长的随机纯字母字符串(只包含大小写字母) 28 * 29 * @param length 30 * 随机字符串长度 31 * @return 随机字符串 32 */ 33 public static String generateMixString(int length) { 34 StringBuffer sb = new StringBuffer(); 35 Random random = new Random(); 36 for (int i = 0; i < length; i++) { 37 sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length()))); 38 } 39 return sb.toString(); 40 } 41 42 /** 43 * 返回一个定长的随机纯大写字母字符串(只包含大小写字母) 44 * 45 * @param length 46 * 随机字符串长度 47 * @return 随机字符串 48 */ 49 public static String generateLowerString(int length) { 50 return generateMixString(length).toLowerCase(); 51 } 52 53 /** 54 * 返回一个定长的随机纯小写字母字符串(只包含大小写字母) 55 * 56 * @param length 57 * 随机字符串长度 58 * @return 随机字符串 59 */ 60 public static String generateUpperString(int length) { 61 return generateMixString(length).toUpperCase(); 62 } 63 64 /** 65 * 生成一个定长的纯0字符串 66 * 67 * @param length 68 * 字符串长度 69 * @return 纯0字符串 70 */ 71 public static String generateZeroString(int length) { 72 StringBuffer sb = new StringBuffer(); 73 for (int i = 0; i < length; i++) { 74 sb.append('0'); 75 } 76 return sb.toString(); 77 } 78 79 /** 80 * 根据数字生成一个定长的字符串,长度不够前面补0 81 * 82 * @param num 83 * 数字 84 * @param fixdlenth 85 * 字符串长度 86 * @return 定长的字符串 87 */ 88 public static String toFixdLengthString(long num, int fixdlenth) { 89 StringBuffer sb = new StringBuffer(); 90 String strNum = String.valueOf(num); 91 if (fixdlenth - strNum.length() >= 0) { 92 sb.append(generateZeroString(fixdlenth - strNum.length())); 93 } else { 94 throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth 95 + "的字符串发生异常!"); 96 } 97 sb.append(strNum); 98 return sb.toString(); 99 } 100 101 /** 102 * 每次生成的len位数都不相同 103 * 104 * @param param 105 * @return 定长的数字 106 */ 107 public static int getNotSimple(int[] param, int len) { 108 Random rand = new Random(); 109 for (int i = param.length; i > 1; i--) { 110 int index = rand.nextInt(i); 111 int tmp = param[index]; 112 param[index] = param[i - 1]; 113 param[i - 1] = tmp; 114 } 115 int result = 0; 116 for (int i = 0; i < len; i++) { 117 result = result * 10 + param[i]; 118 } 119 return result; 120 } 121 122 public static void main(String[] args) { 123 System.out.println("返回一个定长的随机字符串(只包含大小写字母、数字):" + generateString(10)); 124 System.out 125 .println("返回一个定长的随机纯字母字符串(只包含大小写字母):" + generateMixString(10)); 126 System.out.println("返回一个定长的随机纯大写字母字符串(只包含大小写字母):" 127 + generateLowerString(10)); 128 System.out.println("返回一个定长的随机纯小写字母字符串(只包含大小写字母):" 129 + generateUpperString(10)); 130 System.out.println("生成一个定长的纯0字符串:" + generateZeroString(10)); 131 System.out.println("根据数字生成一个定长的字符串,长度不够前面补0:" 132 + toFixdLengthString(123, 10)); 133 int[] in = { 1, 2, 3, 4, 5, 6, 7 }; 134 System.out.println("每次生成的len位数都不相同:" + getNotSimple(in, 3)); 135 } 136 }