Skip to content

Instantly share code, notes, and snippets.

@nurcinozer
Created January 9, 2019 16:58
Show Gist options
  • Save nurcinozer/c1449d9e5f806118d7cbae3923771287 to your computer and use it in GitHub Desktop.
Save nurcinozer/c1449d9e5f806118d7cbae3923771287 to your computer and use it in GitHub Desktop.
import java.util.random;
public class Password {
public static String GeneratePassword(int size) {
String password = "";
Random r = new Random();
char[] symbols = {'!', '?', '_', '#', '-'};
char[] lower = new char[26];
char[] upper = new char[26];
for (int i = 0; i < 26; i++) {
lower[i] = (char)('a' + i);
upper[i] = (char)('a' + i);
}
for(int i = 0; i < size; i++)
{
/*
* digits <-0
* symbols <-1
* lower <-2
* upper <-3
*/
switch(r.nextInt(4))
{
case 0: password += r.nextInt(10); break;
case 1: password += symbols[r.nextInt(symbols.length)]; break;
case 2: password += lower[r.nextInt(lower.length)]; break;
case 3: password += upper[r.nextInt(upper.length)]; break;
}
}
return password;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment