views:

214

answers:

3

I've started writing some JAVA code and now I want to get a random letter from list of letters given so what should I use for this.

+4  A: 

I'll take your question literally:

Random r = new Random(); // Keep this stored as a field
List<Character> l = ...; // initialize this somewhere
char c = l.get(r.nextInt(l.size()));

Depending on several factors (are the letters contiguous, are you resizing the list dynamically), you may be able to use an array, or may not need a collection. See the Random class.

Example:

Random r = new Random(); // Keep this stored as a field
List<Character> l = Arrays.asList('A', 'F', 'O', 'W', 'M', 'I', 'C', 'E');
char c = l.get(r.nextInt(l.size()));   
Matthew Flaschen
I can't compile the code above can you give me a sample, for example random letter from A F O W M I C E
Ivan
@Ivan You can't compile it because you need to initialize the list. After that, it will compile, so just add the characters that you want to the list.
Kevin Crowell
+1  A: 

This snippet should be instructive:

import java.util.Random;

public class RandomLetter {
   static Random r = new Random();  
   static char pickRandom(char... letters) {
      return letters[r.nextInt(letters.length)];
   }
   public static void main(String args[]) {
      for (int i = 0; i < 10; i++) {
         System.out.print(pickRandom('A', 'F', 'O', 'W', 'M', 'I', 'C', 'E'));
      }
   }   
}

See also:


If you want to do 3 letters at a time, then you can do something like this instead:

import java.util.Random;

public class RandomLetter {
   static Random r = new Random();  
   static char pickRandom(char... letters) {
      return letters[r.nextInt(letters.length)];
   }

   public static void main(String args[]) {
      for (int i = 0; i < 10; i++) {
         System.out.println("" +
            pickRandom("ACEGIKMOQSUWY".toCharArray()) +
            pickRandom("BDFHJLNPRVXZ".toCharArray()) +
            pickRandom("ABCDEFGHJKLMOPQRSTVWXYZ".toCharArray())
         );
      }
   }   
}
polygenelubricants
A: 

Thanks you guy for the answers, now I want to generate 3 letter at the same time so I write the code below but it can't be compiled.

import java.util.*;

public class RL{

    public static void main(String[] args) {

        Random random = new Random();
        List<Character> l = Arrays.asList(
            'A', 'C', 'E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y');
        char l1 = l.get(random.nextInt(l.size()));
        List<Character> 3 = Arrays.asList(
            'B', 'D', 'F', 'H', 'J', 'L', 'N', 'P', 'R', 'T', 'V', 'X', 'Z');
        char l2 = 2.get(random.nextInt(2.size()));
        List<Character> 3 = Arrays.asList(
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        char l3 = 3.get(random.nextInt(3.size()));
        System.out.println(l1""+""l2""+""l3);
    }
}
Ivan
`List<Character> l` this is actually the lowercase letter `l`, not the number `1`. Using `l` as a variable name is a _terrible practice_ exactly for this reason.
polygenelubricants
When something doesn't compile, it will tell you why. Please read the errors and fix the problems.
Kevin Crowell
Also, `2` and `3` are not variable names at all.
trashgod