Java Random Element randomEle(List list)

Here you can find the source of randomEle(List list)

Description

random Ele

License

Apache License

Declaration

public static <T> T randomEle(List<T> list) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.List;
import java.util.Random;

public class Main {
    private static Random random = new Random();

    public static <T> T randomEle(List<T> list) {
        return randomEle(list, list.size());
    }//from   w w  w  .  j a va 2s.c  o  m

    public static <T> T randomEle(List<T> list, int limit) {
        return list.get(randomInt(limit));
    }

    public static int randomInt(int min, int max) {
        return random.nextInt(max - min) + min;
    }

    public static int randomInt() {
        return random.nextInt();
    }

    public static int randomInt(int limit) {
        return random.nextInt(limit);
    }
}

Related

  1. randomElem(Random rand, Set partitions)
  2. randomElement(Collection coll)
  3. randomElement(E[] array)
  4. randomElement(final Set set)