How to shuffle characters from a String in Java at random?

6

I am developing a password generator program. Here you can set the percentage of numbers, letters and special characters you want to have in your password, as well as the size of your password. Later I concatenate the numbers, letters and symbols and I need to shuffle them so that it looks like a password (this was the best solution I found to generate passwords following this percentage character concept).

The problem is that I need to shuffle an array of characters at random.

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 in a scrambled form later.

Searching a little, I found the function shuffle(); of class Collecions and wrote the following code, which does exactly 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 bit "heavy". I think there must be some simpler way of doing this. I've also been worried about how these items are shuffled, as I need something random, or as random as possible.

    
asked by anonymous 12.11.2015 / 20:43

3 answers

5
___ erkimt ___ How to shuffle characters from a String in Java at random? ______ qstntxt ___

I am developing a password generator program. Here you can set the percentage of numbers, letters and special characters you want to have in your password, as well as the size of your password. Later I concatenate the numbers, letters and symbols and I need to shuffle them so that it looks like a password (this was the best solution I found to generate passwords following this percentage character concept).

The problem is that I need to shuffle an array of characters at random.

This array can be an array of %code% , or even a %code% , the important thing is that I have to print this on the screen in a scrambled form later.

Searching a little, I found the function %code% of class %code% and wrote the following code, which does exactly what I want:

public static String shuffle(String s) {
    List<String> letters = Arrays.asList(s.split(""));
    Collections.shuffle(letters);
    StringBuilder t = new StringBuilder(s.length());
    for (String k : letters) {
        t.append(k);
    }
    return t.toString();
}

The problem is that I find this code a bit "heavy". I think there must be some simpler way of doing this. I've also been worried about how these items are shuffled, as I need something random, or as random as possible.

    
______ ___ azszpr97949

Abusing a little regex:

public static String shuffle(String s) {
    List<String> letters = Arrays.asList(s.split(""));
    Collections.shuffle(letters);
    StringBuilder t = new StringBuilder(s.length());
    for (String k : letters) {
        t.append(k);
    }
    return t.toString();
}

View running on ideone.

    
______ azszpr97946 ___

How about using Java 8?

%pre%     
______ azszpr97945 ___

Let's face it:

%pre%     
___
12.11.2015 / 21:20
5

How about using Java 8?

public static String shuffle(String s) {
    List<Character> letters = s.chars().boxed().map(c -> (char) c.intValue()).collect(Collectors.toList());
    Collections.shuffle(letters);
    StringBuilder t = new StringBuilder(s.length());
    letters.forEach(t::append);
    return t.toString();
}
    
12.11.2015 / 21:00
4

Let's face it:

public static String shuffleString(String s) {
    char[] caracteres = s.toCharArray();
    ArrayList<String> lista = new ArrayList<String>(Arrays.asList(caracteres));
    Collections.shuffle(lista);
    return lista;

}
    
12.11.2015 / 20:58