6
I’m developing a password generator program. In it the user can set the percentage of numbers, letters and special characters that he wants to have in his password, as well as the size of his password. Later I concatenate the numbers, letters and symbols and need to shuffle them in order to look like a password (this was the best solution I could find to generate passwords following this concept of percentage of characters).
The problem is that I need to shuffle a character array randomly.
This array can be an array of char
, or even a String
, the important thing is that I have to print this on the screen so shuffled later.
Researching a little, I found the function shuffle();
class Collecions
and wrote the following code, which does just what I want:
public static String shuffleString(String s) {
List<String> letters = new ArrayList<String>();
String temp = "";
for (int i = 0; i < s.length(); i++) {
letters.add(String.valueOf(s.charAt(i)));
}
System.out.println("");
Collections.shuffle(letters);
for (int i = 0; i < s.length(); i++) {
temp += letters.get(i);
}
return temp;
}
The problem is that I find this code a little "heavy". I think there must be some simpler way to do this. I have also been concerned about how these items are shuffled, because I need something random, or as random as possible.
The problem here is somewhat like the syntax of Java. I find the code too extensive to do something simple. I think I’m doing this the wrong way or else in an inefficient way.
– Avelino