Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Random; public class Main { private static final Random random = new Random(); public static <O> O getRandom(final Collection<O> pObjects) { if (pObjects != null && !pObjects.isEmpty()) { int r = Math.abs(random.nextInt() % pObjects.size()); List<O> objects = !(pObjects instanceof List) ? new ArrayList<O>(pObjects) : (List<O>) pObjects; return objects.get(r); } return null; } }