Juan Ignacio M. Juan Ignacio M. - 4 months ago 5
Android Question

Random array with obligatory letters

I am developing an android app, and I need a function which generates a random array of 16 letters, BUT, at least, some letters should be on it according to a string, and of course, the position of those "obligatory letters" should be random too. For example, letters are
T R P A O L E D H I B O T J E N,

and the word is: BOTTLE



You may add the letters into a a list together with the word´╝î and Collections.shuffle will give you the "random" you need.

    final String LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //You may add more letters if you want.
    final String WORD="BOTTLE";
    final int COUNT=16;
    List<Character> list=new ArrayList<Character>();
    for(int i=0;i<COUNT;++i){
        } else{
            int index=(int) (Math.random()*LETTERS.length());
    Collections.shuffle(list);//now the list contains characters you want, you just need to convert the characters to a String, which is quite simple.