Here you can find the source of randomElement(T[] items)
Parameter | Description |
---|---|
items | non-null array |
public static <T> T randomElement(T[] items)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static final Random RND = new Random(); /**/* w ww.j a va2s . c o m*/ * choose a random element from an array * @param items non-null array * @return possibly null value - null only if the array is empty */ public static <T> T randomElement(T[] items) { switch (items.length) { case 0: return null; case 1: return items[0]; default: return items[RND.nextInt(items.length)]; } } }