Here you can find the source of randomElement(Collection
public static <T> T randomElement(Collection<T> coll)
//package com.java2s; //License from project: Apache License import java.util.Collection; public class Main { public static <T> T randomElement(Collection<T> coll) { int num = (int) (Math.random() * coll.size()); for (T t : coll) if (--num < 0) return t; throw new AssertionError(); }//from ww w.j a v a 2 s.c o m }