1

//Please, help me to fix this codes. I wanna return a String that the have same chars with the sending String but is in different order.

public static String mix(String s){
        int random;
        int n= s.length();
        int [] control = new int[n];
        String miX="";

    for(int i=0 ; i < n ; i++){
        random = (int)(1+Math.random()*(n));
            if( control[i] != random  ){
                control[i]= random;
                miX += s.charAt(random);
        }
        }
        return miX;
    }
crescent
  • 61
  • 4
  • shuffle chars in string http://stackoverflow.com/a/3316696/1737819 – Developer Marius Žilėnas Mar 02 '16 at 10:08
  • Okay thanks. But Can I write the same thing by using this codes? – crescent Mar 02 '16 at 10:12
  • Yes your codes then would look different. See `public static String mix(String s) { StringBuilder sb = new StringBuilder(s); Random r = new Random(); for (int i = 0; i < s.length(); i++) { char curr = sb.charAt(i); //current char int rix = r.nextInt(s.length()); //random index char temp = sb.charAt(rix); //memorize char at index rix sb.setCharAt(rix, curr); //put current char to rix index sb.setCharAt(i , temp); //put memorized char to i index } return sb.toString(); }` call with `System.out.println(mix("Hello"));` :) – Developer Marius Žilėnas Mar 02 '16 at 10:48

3 Answers3

1

You can make use of Collections.shuffle.

String word= "word";
ArrayList<Character> chars =newArrayList<Character>(word.length());

for(char c : word.toCharArray()){
chars.add(c); }

Collections.shuffle(chars);
char[] shuffled =newchar[chars.size()];

for(int i =0; i < shuffled.length; i++){
 shuffled[i]= chars.get(i);
}

String shuffledWord =newString(shuffled);

Another way similar to your code without using functions is:

public static void main(String[] args) {

// Create a random object
Random r = new Random();

String word = "Animals";

System.out.println("Before: " + word );
word = scramble( r, word );
System.out.println("After : " + word );
}
  public static String scramble( Random random, String inputString )
{
    // Convert your string into a simple char array:
    char a[] = inputString.toCharArray();

    // Scramble the letters

    for( int i=0 ; i<a.length-1 ; i++ )
   {
        int j = random.nextInt(a.length-1);
        // Swap letters
       char temp = a[i]; a[i] = a[j];  a[j] = temp;
  }       

    return new String( a );
 }
Aajan
  • 927
  • 1
  • 10
  • 23
0

You should select two positions within the length of the String RANDOMLY and then exchange them . Run this within a loop for any number of times depending on the amount of randomness you want in your String.

Based on your code you might miss some of the characters in your new string if random selects the same index twice

Aman Kumar Sinha
  • 407
  • 6
  • 17
0

Mix string chars with this code

import java.util.*;

class MixStringChars
{
    public static String mix(String s)
    {
        StringBuilder sb = new StringBuilder(s);
        Random r = new Random();
        for (int i = 0; i < s.length(); i++)
        {
            char curr = sb.charAt(i);          //current char
            int  rix  = r.nextInt(s.length()); //random index
            char temp = sb.charAt(rix);        //memorize char at index rix
            sb.setCharAt(rix, curr);           //put current char to rix index
            sb.setCharAt(i  , temp);           //put memorized char to i index
        }
        return sb.toString();
    }

    public static void main (String[] args)
    {
        System.out.println(mix("Hello"));
    }
}