My problem is that I am trying to make (for example) String "asdf" changed to "sdfa" by using Random() and .toCharArray().
How do I not get duplicate random numbers?
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.
I didn't do it here in this code, but that might be an alternative???
edit: I have made it into a main class, which should make it easier. Thank You.
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 < lengthOfStr; i++)
{
int n = randomGenerator.nextInt(lengthOfStr);
chars[i] = chars[n];
String newStr = new String(chars);
str = newStr;
}
System.out.println(str);
}
}