Java Algorithms How to - Generate random with strings








Question

We would like to know how to generate random with strings.

Answer

import java.util.Random;
/*from  w  ww .  java  2s  .co  m*/
public class Main {

  public static void main(String[] args) {

    String[] arr = { "A", "B", "C", "D" };
    Random random = new Random();

    int select = random.nextInt(arr.length);

    System.out.println("Random String selected: " + arr[select]);
  }
}

The code above generates the following result.