Here you can find the source of getRandomElement(T[] items)
Parameter | Description |
---|---|
items | list |
public static <T> T getRandomElement(T[] items)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static final Random RND = new Random(); /**//from w w w . j a va2 s .co m * choose a random element * * @param items list * @return some element - null */ public static <T> T getRandomElement(T[] items) { if (items == null) return null; int len = items.length; switch (len) { case 0: return null; case 1: return items[0]; default: return items[RND.nextInt(len)]; } } }