Here you can find the source of getRandom(Collection
public static <T> T getRandom(Collection<T> collection)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static final Random rnd = new Random(); public static <T> T getRandom(Collection<T> collection) { if (collection.isEmpty()) return null; int randomIndex = rnd.nextInt(collection.size()); int i = 0; for (T obj : collection) { if (i == randomIndex) return obj; i = i + 1;/* w ww . j a v a 2 s.com*/ } return null; } public static <T> T getRandom(List<T> list) { if (list.isEmpty()) return null; int randomIndex = rnd.nextInt(list.size()); return list.get(randomIndex); } }