How to shuffle characters in a string without using Collections.shuffle(…)?

后端 未结 14 2180
南旧
南旧 2020-11-27 07:19

How do I shuffle the characters in a string (e.g. hello could be ehlol or lleoh or ...). I don\'t want to use the Collections.shuffle(...) method, is there anyt

相关标签:
14条回答
  • 2020-11-27 07:58

    How about this:

    public static String shuffle(String text) {
        char[] characters = text.toCharArray();
        for (int i = 0; i < characters.length; i++) {
            int randomIndex = (int)(Math.random() * characters.length);
            char temp = characters[i];
            characters[i] = characters[randomIndex];
            characters[randomIndex] = temp;
        }
        return new String(characters);
    }
    
    0 讨论(0)
  • 2020-11-27 08:01

    What an annoying problem. I finally ended up with this:

    import java.util.Collections;
    import com.google.common.primitives.Chars;
    import org.apache.commons.lang3.StringUtils;
    
    String shuffle(String s) {
        List<Character> chars = Chars.asList(s.toCharArray());
        Collections.shuffle(chars);
        return StringUtils.join(chars.stream().toArray());
    }
    

    Yes, two libraries :)

    0 讨论(0)
  • 2020-11-27 08:02
    class ShuffleString
    {
    
        public static String shuffle(String s)
        {
    
            String shuffledString = ""; 
    
            while (s.length() != 0)
            {
                int index = (int) Math.floor(Math.random() * s.length());
                char c = s.charAt(index);
                s = s.substring(0,index)+s.substring(index+1);
                shuffledString += c;
            }
    
            return shuffledString;
    
        }
    
    }
    
    
    public class foo{
        static public void main(String[] args)
        {
    
            String test = "hallo";
            test = ShuffleString.shuffle(test);
            System.out.println(test);
        }
    }
    

    Output: ahlol

    0 讨论(0)
  • 2020-11-27 08:03
            String shuffled;
            do {
                shuffled = Stream.of(text.split("")).sorted((o1, o2) -> ThreadLocalRandom.current().nextInt(3) - 1).collect(Collectors.joining());
            }while(shuffled.equals(text));
    
    0 讨论(0)
  • 2020-11-27 08:06

    Not great performance, but quite readable in my opinion:

    public static String shuffleString(String string)
    {
      List<String> letters = Arrays.asList(string.split(""));
      Collections.shuffle(letters);
      String shuffled = "";
      for (String letter : letters) {
        shuffled += letter;
      }
      return shuffled;
    }
    
    0 讨论(0)
  • 2020-11-27 08:07

    I dont know anything simpler. But you can use the Math.rand() functionality to generate a random number within the range of the character's length without replace and that would give you a shuffled output

    public class Shuffle {
        public static void main(String[] args) {
            Shuffle s = new Shuffle();
            s.shuffle("hello");
    
        }
        public void shuffle(String input){
            List<Character> characters = new ArrayList<Character>();
            for(char c:input.toCharArray()){
                characters.add(c);
            }
            StringBuilder output = new StringBuilder(input.length());
            while(characters.size()!=0){
                int randPicker = (int)(Math.random()*characters.size());
                output.append(characters.remove(randPicker));
            }
            System.out.println(output.toString());
        }
    }
    /*
    Sample outputs
    hlleo
    llheo
    leohl
    lleho
    */
    
    0 讨论(0)
提交回复
热议问题