Here you can find the source of random(E[] elements)
public static <E> E random(E[] elements)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static <E> E random(E[] elements) { return first(randomList(elements)); }/* w w w.j av a 2 s . com*/ public static <E> E random(Collection<E> collection) { return first(randomList(collection)); } public static <E> E random(E[] values, E except) { return random(Arrays.asList(values), except); } public static <E> E random(Collection<E> collection, E except) { Collection<E> randomCollection = randomList(collection); Iterator<E> iterator = randomCollection.iterator(); if (iterator.hasNext()) { E entry = iterator.next(); if (!Objects.equals(entry, except)) { return entry; } } return null; } public static <E> E first(Collection<E> collection) { if (isEmpty(collection)) { return null; } Iterator<E> iterator = collection.iterator(); if (iterator.hasNext()) { return iterator.next(); } throw new IllegalStateException("Should not happen"); } public static <E> List<E> randomList(Collection<E> collection) { List<E> list = new ArrayList<>(collection); Collections.shuffle(list); return list; } public static <E> List<E> randomList(E[] elements) { return randomList(Arrays.asList(elements)); } public static <E> List<E> asList(E[] array) { if (isEmpty(array)) { return new ArrayList<>(); } return Arrays.asList(array); } private static <E> boolean isEmpty(Collection<E> collection) { return (collection == null || collection.isEmpty()); } private static <E> boolean isEmpty(E[] array) { return (array == null || array.length == 0); } }