mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-19 17:54:42 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@ -1,23 +1,25 @@
|
||||
package Others;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
Creates a random password from ASCII letters
|
||||
Given password length bounds
|
||||
|
||||
author: AKS1996
|
||||
date: 2017-10-25
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a random password from ASCII letters
|
||||
* Given password length bounds
|
||||
*
|
||||
* @author AKS1996
|
||||
* @date 2017.10.25
|
||||
*/
|
||||
class PasswordGen {
|
||||
public static void main(String args[]){
|
||||
String password = generatePassword(8,16);
|
||||
System.out.print("Password: " + password);
|
||||
public static void main(String args[]) {
|
||||
String password = generatePassword(8, 16);
|
||||
System.out.print("Password: " + password);
|
||||
}
|
||||
|
||||
static String generatePassword(int min_length, int max_length){
|
||||
|
||||
static String generatePassword(int min_length, int max_length) {
|
||||
Random random = new Random();
|
||||
|
||||
String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
@ -25,10 +27,10 @@ class PasswordGen {
|
||||
String numbers = "0123456789";
|
||||
String specialChars = "!@#$%^&*(){}?";
|
||||
|
||||
String allChars = upper+lower+numbers+specialChars;
|
||||
String allChars = upper + lower + numbers + specialChars;
|
||||
|
||||
List<Character> letters = new ArrayList<Character>();
|
||||
for(char c:allChars.toCharArray())
|
||||
for (char c : allChars.toCharArray())
|
||||
letters.add(c);
|
||||
|
||||
// Inbuilt method to randomly shuffle a elements of a list
|
||||
@ -36,7 +38,7 @@ class PasswordGen {
|
||||
String password = "";
|
||||
|
||||
// Note that size of the password is also random
|
||||
for(int i = random.nextInt(max_length-min_length) + min_length; i>0; --i) {
|
||||
for (int i = random.nextInt(max_length - min_length) + min_length; i > 0; --i) {
|
||||
password += letters.get(random.nextInt(letters.size()));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user