Java Collection Random Element random(Collection coll)

Here you can find the source of random(Collection coll)

Description

random

License

Open Source License

Declaration

public static <T> T random(Collection<T> coll) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

import java.util.Random;

public class Main {
    public static Random random = new Random(System.nanoTime());

    public static <T> T random(Collection<T> coll) {
        if (coll.size() == 0)
            return null;
        int num = (int) (Math.random() * coll.size());
        for (T t : coll)
            if (--num < 0)
                return t;
        throw new AssertionError();
    }// w  w w. j ava2s .c o m

    public static int random(int min, int max) {
        return min + (int) (Math.random() * (max - min + 1));
    }
}

Related

  1. peekRandom(Collection collection, Random rnd)
  2. pickOneRandomly(Collection from)
  3. pollRandom(Collection collection, Random rnd)
  4. random(Collection collection)
  5. random(Collection coll)
  6. randomElement(Collection coll)
  7. randomElement(Collection in)
  8. randomIterable(Collection col)
  9. rndSubset(final Collection c, final double ratio)