Skip to content

Instantly share code, notes, and snippets.

@ghsatpute
Created December 6, 2018 11:55
Show Gist options
  • Save ghsatpute/462d92a8dc848240cc495a789bf6705f to your computer and use it in GitHub Desktop.
Save ghsatpute/462d92a8dc848240cc495a789bf6705f to your computer and use it in GitHub Desktop.
Generate random password
public class RandomPassword {
private static String generateRandomPassword() {
Random random = new Random();
char start = characters.charAt(Math.floorMod(random.nextInt(), characters.length()));
List<Character> chars = new ArrayList<>();
for (int i = 0; i < 5; i++) {
chars.add(characters.charAt(Math.floorMod(random.nextInt(), characters.length())));
}
chars.add(numeric.charAt(Math.floorMod(random.nextInt(), numeric.length())));
chars.add(specialCharacters.charAt(Math.floorMod(random.nextInt(), specialCharacters.length())));
Collections.shuffle(chars);
String lastChars = StringUtils.join(chars, "");
return start + lastChars;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment