Here you can find the source of random(T[] array)
Parameter | Description |
---|---|
array | The array to retrieve an element from. |
public static <T> T random(T[] array)
//package com.java2s; //License from project: Open Source License import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class Main { /**/*from w w w.j a va2s . c om*/ * Pseudo-randomly retrieves a element from {@code array}. * * @param array The array to retrieve an element from. * @return The element retrieved from the array. */ public static <T> T random(T[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code int} from this {@code array}. * * @param array The array to retrieve an {@code int} from. * @return The {@code int} retrieved from the array. */ public static int random(int[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code long} from this {@code array}. * * @param array The array to retrieve an {@code long} from. * @return The {@code long} retrieved from the array. */ public static long random(long[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code double} from this {@code array}. * * @param array The array to retrieve an {@code double} from. * @return The {@code double} retrieved from the array. */ public static double random(double[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code short} from this {@code array}. * * @param array The array to retrieve an {@code short} from. * @return The {@code short} retrieved from the array. */ public static short random(short[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code byte} from this {@code array}. * * @param array The array to retrieve an {@code byte} from. * @return The {@code byte} retrieved from the array. */ public static byte random(byte[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code float} from this {@code array}. * * @param array The array to retrieve an {@code float} from. * @return The {@code float} retrieved from the array. */ public static float random(float[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code boolean} from this {@code array}. * * @param array The array to retrieve an {@code boolean} from. * @return The {@code boolean} retrieved from the array. */ public static boolean random(boolean[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves an {@code char} from this {@code array}. * * @param array The array to retrieve an {@code char} from. * @return The {@code char} retrieved from the array. */ public static char random(char[] array) { return array[(int) (ThreadLocalRandom.current().nextDouble() * array.length)]; } /** * Pseudo-randomly retrieves a element from {@code list}. * * @param list The list to retrieve an element from. * @return The element retrieved from the list. */ public static <T> T random(List<T> list) { return list.get((int) (ThreadLocalRandom.current().nextDouble() * list.size())); } }