Here you can find the source of randomElement(T[] array)
Parameter | Description |
---|---|
array | the array to pick the element from. |
public static <T> T randomElement(T[] array)
//package com.java2s; //License from project: Open Source License import java.util.List; import java.util.Random; public class Main { /** Random instance, used to generate pseudo-random primitive types. */ public static final Random RANDOM = new Random(System.currentTimeMillis()); /**/* www. j av a 2s . c o m*/ * Picks a random element out of any array type. * * @param array * the array to pick the element from. * @return the element chosen. */ public static <T> T randomElement(T[] array) { return array[(int) (RANDOM.nextDouble() * array.length)]; } /** * Picks a random element out of any list type. * * @param list * the list to pick the element from. * @return the element chosen. */ public static <T> T randomElement(List<T> list) { return list.get((int) (RANDOM.nextDouble() * list.size())); } }