Java Algorithms How to - Get random letters








Question

We would like to know how to get random letters.

Answer

/*from   w  w w. ja  v a2 s. c o m*/
import java.util.Random;

public class Main {
  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()));
    }
  }
}

The code above generates the following result.