scrambling a string using .toCharArray()
<p>My problem is that I am trying to make (for example) String "asdf" changed to "sdfa" by using Random() and .toCharArray().</p>

<p>How do I not get duplicate random numbers?</p>

<p>I figure that I am supposed to create a new Char array in order to randomly store the char's without changing my original array because if I do, then the new string will be messed up, if that makes any sense.</p>

<p>I didn't do it here in this code, but that might be an alternative???</p>

<p>edit: I have made it into a main class, which should make it easier. Thank You.</p>

<pre><code>import java.util.Random;


public class Scramble {

public static void main(String[] args) {

            String str = "asdf";

            Random randomGenerator = new Random();

            int lengthOfStr = str.length();

            char[] chars = str.toCharArray();



            // do for length of str
            for (int i=0; i &lt; lengthOfStr; i++)
            {

            int n = randomGenerator.nextInt(lengthOfStr);

            chars[i] = chars[n];

            String newStr = new String(chars);
            str = newStr;
            }

            System.out.println(str);            
}
</code></pre>

<p>}</p>
