0

I have got a char array (size 12) that can look like this:

{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'}  

And I would like to create (in the most efficient way) a String that would be the result of taking the characters from the array and ordering them ~randomly (let's use that word), for example:

“ahbejclfkdig”

I tried solutions with StringBuffer and random placing, but there was the problem of positions repeating. Also, I tried Collections.shuffle, but I don’t quite get this one working. I also looked at linear feedback shift register, but I don’t think is appropriate here. It is quite simple case, I will not be operating on large numbers, so memory allocation and speed should not raise any major issues.

Hurdler
  • 891
  • 3
  • 15
  • 30
  • The Collections.shuffle solution is the most straightforward. Show us the code you tried, and we might help telling you what's wrong with it. – JB Nizet Aug 17 '11 at 10:48

5 Answers5

3

You can use shuffle but change it for StringBuilder. I would wouldn't use StringBuffer unless you have to use old versions of Java.

public static void main(String... args) {
    StringBuilder sb = new StringBuilder("abcdefghijkl");
    for (int i = 0; i < 5; i++) {
        shuffle(sb);
        System.out.println(sb);
    }
}


public static void shuffle(StringBuilder sb) {
    Random rand = new Random();
    for (int i = sb.length() - 1; i > 1; i--) {
        int swapWith = rand.nextInt(i);
        char tmp = sb.charAt(swapWith);
        sb.setCharAt(swapWith, sb.charAt(i));
        sb.setCharAt(i, tmp);
    }
}

prints

kbljdhieagcf
gefabkhdclij
hbkfjilcgade
eacihdkjfgbl
hbjcfegdilka
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • That works perfectly fine, thank you. Going one step futher how could I randomly choose just one of those n printed out lines? – Hurdler Aug 17 '11 at 11:00
  • 2
    just invoke shuffle(nb) not in the loop ? – mkk Aug 17 '11 at 11:11
  • I don't think we understand each other (or it's just me). This solution is all good, but I was just wondering if I could randomly choose one of those already shuffled. – Hurdler Aug 17 '11 at 11:21
  • 2
    They are all random, how would you chose one instead of another. If you want just one shuffled list, call shuffle once instead of in a loop. – Peter Lawrey Aug 17 '11 at 12:00
  • 1
    Good point, they all have same "random strength". So I will call shuffle once. Thank you for your help! – Hurdler Aug 17 '11 at 12:07
1

The following code which use Collections.shuffle works:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class Shuffle {

  public static void main(String[] args) {
    Character[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
    List<Character> shuffled = Arrays.asList(letters);
    Collections.shuffle(shuffled);
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < shuffled.size(); i++) {
      sb.append(shuffled.get(i));
    }
    System.out.println(sb);     
  }

}
Marian Galik
  • 876
  • 8
  • 21
  • Yes, I'm not that familiar with Lists, but this works perfectly fine as well, thank you Marian ;) – Hurdler Aug 17 '11 at 11:09
0

You may look up the Random class in the Java API. This will enable you to create random array indexes. So you can pick the characters from the array in random order.

Ingo
  • 36,037
  • 5
  • 53
  • 100
0
String randomize(char[] letters) {
    String newString = new String[letters.length];
    for (int i = 0; i <= letters.length; i++) {
        int randomChar = Math.floor(Math.random() * letters.length);
        newString[i] = letters[randomChar];
    }
    return newString;
}
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
0

I have tried the following piece of code... I guess you can use it in your scenario!

String[] alphabets = new String[12];
int i = 0;
char[] arrayAlpha = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'};       
for (char ch : arrayAlpha) {
   alphabets[i++] = String.valueOf(ch);
}

List<String> list = Arrays.asList(alphabets);
Collections.shuffle(list);

for (String alpha : list) {
    System.out.print(alpha + " ");
}
Taha
  • 1,086
  • 1
  • 10
  • 20