Randomly select in an array


License: This work is licensed under a Attribution 4.0 International.

import java.util.Random;

public class Testing {

public static void main(String[] args) {

// TODO Auto-generated method stub

String arr[] = {“A”,“B”,“C”,“D”,“E”}; //To Define an one-dementional array

    int len = arr.length; //Get the length of that array

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

    int arrIdx = random.nextInt(len-1); //Index randomly,nextInt(len-1) means any number between [0,(len-1)]

    String num = arr[arrIdx];