Here you can find the source of nextRandomElement(final List
private static <T> T nextRandomElement(final List<T> list)
//package com.java2s; /**//from ww w.ja v a2 s .c o m * ShortPasta Foundation * http://www.shortpasta.org * Copyright 2009 and beyond, Sal Ingrilli at the ShortPasta Software Foundation * <p/> * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version 3 * as published by the Free Software Foundation as long as: * 1. You credit the original author somewhere within your product or website * 2. The credit is easily reachable and not burried deep * 3. Your end-user can easily see it * 4. You register your name (optional) and company/group/org name (required) * at http://www.shortpasta.org * 5. You do all of the above within 4 weeks of integrating this software * 6. You contribute feedback, fixes, and requests for features * <p/> * If/when you derive a commercial gain from using this software * please donate at http://www.shortpasta.org * <p/> * If prefer or require, contact the author specified above to: * 1. Release you from the above requirements * 2. Acquire a commercial license * 3. Purchase a support contract * 4. Request a different license * 5. Anything else * <p/> * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, similarly * to how this is described in the GNU Lesser General Public License. * <p/> * User: Sal Ingrilli * Date: Oct 9, 2014 * Time: 6:58:48 PM */ import java.util.List; import java.util.Random; public class Main { private static final Random random = new Random(); /** * Returns the next random element from the given Collection * @return list */ private static <T> T nextRandomElement(final List<T> list) { // preconditions: array cannot be empty, otherwise we would have to return null, in which // case the caller would not be able to tell whether the element returned is a valid element or not if (list.size() == 0) { throw new RuntimeException("list.size () must be > 0"); } // randomize the next final int lowerBound = 0; final int upperBound = list.size(); final int index = lowerBound + random.nextInt(upperBound - lowerBound); return list.get(index); } }