Java SecureRandom generate random String
import java.security.SecureRandom; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { System.out.println(generatePassword(10)); System.out.println(generatePassword(20)); }/*from ww w.j a v a2s.c om*/ public static String generatePassword(int passwordLength) { int asciiFirst = 33; int asciiLast = 126; Integer[] exceptions = { 34, 39, 96 }; List<Integer> exceptionsList = Arrays.asList(exceptions); SecureRandom random = new SecureRandom(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < passwordLength; i++) { int charIndex; do { charIndex = random.nextInt(asciiLast - asciiFirst + 1) + asciiFirst; } while (exceptionsList.contains(charIndex)); builder.append((char) charIndex); } return builder.toString(); } }