Here you can find the source of randomElement(List
public static <T> T randomElement(List<T> list)
//package com.java2s; //License from project: Apache License import java.util.List; import java.util.Random; public class Main { private static final ThreadLocal<Random> random = new ThreadLocal<Random>() { @Override//from ww w. j a v a 2 s.c o m protected Random initialValue() { return new Random(); } }; /** Selects a random element from a list. */ public static <T> T randomElement(List<T> list) { if (list.isEmpty()) throw new IllegalArgumentException("Can't select random element from an empty list."); return list.get(random.get().nextInt(list.size())); } }