-
-
Save parzibyte/e5c74f9811e2a09369acba11950f2876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static String obtenerPasswordAleatoria(boolean conRepetidos, int longitud) { | |
String muestra = "0123456789"; | |
Random random = new Random(System.currentTimeMillis()); | |
String pass = ""; | |
for (int x = 0; x < longitud; x++) { | |
// Tomamos un dígito aleatorio | |
int indiceAleatorio = random.nextInt(muestra.length()); | |
char digitoAleatorio = muestra.charAt(indiceAleatorio); | |
// Si no quieren repetidos... | |
if (!conRepetidos) { | |
// Mientras que en la cadena ya exista el carácter que acabamos de generar | |
while (pass.indexOf(digitoAleatorio) != -1) { | |
// Generamos otro | |
indiceAleatorio = random.nextInt(muestra.length()); | |
digitoAleatorio = muestra.charAt(indiceAleatorio); | |
} | |
} | |
// De cualquier forma, agregamos el dígito a la cadena | |
pass += digitoAleatorio; | |
} | |
return pass; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment