/** * 获取4位包括字母+数字的随机码 * @return */ public static String getRandomValidStr(){ String letters[] = {"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", "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"}; String numbers[] = {"0","1","2","3","4","5","6","7","8","9"}; int r1 = (int) Math.round(Math.random()*(letters.length-1)); int r2 = (int) Math.round(Math.random()*(letters.length-1)); int r3 = (int) Math.round(Math.random()*(numbers.length-1)); int r4 = (int) Math.round(Math.random()*(numbers.length-1)); String str[] = {letters[r1],letters[r2],numbers[r3],numbers[r4]}; List<String> list = Arrays.asList(str); Collections.shuffle(list); return list.get(0)+list.get(1)+list.get(2)+list.get(3); }