Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Digits {
- private static final String CAPITAL_LETTERS =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- private static final String SMALL_LETTERS =
- "abcdefghijklmnopqrstuvwxyz";
- private static final String DIGITS = "0123456789";
- private static final String SPECIAL_CHARS =
- "~!@#$%^&*()_+=`{}[]\\|':;.,/?<>";
- private static final String ALL_CHARS =
- CAPITAL_LETTERS + SMALL_LETTERS + DIGITS + SPECIAL_CHARS;
- private static Random rnd = new Random();
- public static void main(String[] args) {
- StringBuilder password = new StringBuilder();
- // Генерира 2 произволни главни букви
- for (int i=1; i<=2; i++) {
- char capitalLetter = generateChar(CAPITAL_LETTERS);
- insertAtRandomPosition(password, capitalLetter);
- }
- // Генерира 2 произволни малки букви
- for (int i=1; i<=2; i++) {
- char smallLetter = generateChar(SMALL_LETTERS);
- insertAtRandomPosition(password, smallLetter);
- }
- //Генерира се 1 произвона цифра
- char digit = generateChar(DIGITS);
- insertAtRandomPosition(password, digit);
- // Генерира 3 символа
- for (int i=1; i<=3; i++) {
- char specialChar = generateChar(SPECIAL_CHARS);
- insertAtRandomPosition(password, specialChar);
- }
- // Генерира няколко произвилни символа (между 0 и 7)
- int count = rnd.nextInt(8);
- for (int i=1; i<=count; i++) {
- char specialChar = generateChar(ALL_CHARS);
- insertAtRandomPosition(password, specialChar);
- }
- System.out.println(password);
- }
- private static void insertAtRandomPosition(
- StringBuilder password, char character) {
- int randomPosition = rnd.nextInt(password.length()+1);
- password.insert(randomPosition, character);
- }
- private static char generateChar(String availableChars) {
- int randomIndex = rnd.nextInt(availableChars.length());
- char randomChar = availableChars.charAt(randomIndex);
- return randomChar;
- }
- }
Add Comment
Please, Sign In to add comment