Random password generator in Java
Source code:
mport java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("How many pw do you need");
int total=in.nextInt();
System.out.println("How long");
int len=in.nextInt();
// '0'-'9' --> 48 - 57
// 'A' - 'Z' --> 65-90
// 'a' - 'z' --> 97-122
ArrayList<String> listOfPasswords=new ArrayList<>();
// for looping total number of passwords
for(int i=0;i<total;i++){
// generate one random password
String randomPassword="";
for(int j=0;j<len;j++){
//generate one random character
randomPassword=randomPassword+randomCharacter();
}
listOfPasswords.add(randomPassword);
}
for(String i: listOfPasswords)
System.out.println(i+"");
}
public static char randomCharacter(){
// generate a random number represents all possible character
// in out psword
// 10 digits+26 uppercase+ 26 lower case = 62 possible values
int rand=(int)(Math.random()*62);
if(rand<=9){
rand=rand + 48;
return (char)(rand);
}else if(rand<=35){
rand = rand + 55;
return (char)(rand);
}else{
rand = rand + 61;
return (char)(rand);
}
}
}