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 <T> T random(Collection<T> coll) {
        int num = (new Random().nextInt(coll.size()));
        for (T t : coll) {
            if (--num < 0) {
                return t;
            }//  ww w  .ja  v  a 2 s .c  o m
        }

        return null;
    }
}

Related

  1. oneOf(Collection array)
  2. peekRandom(Collection collection, Random rnd)
  3. pickOneRandomly(Collection from)
  4. pollRandom(Collection collection, Random rnd)
  5. random(Collection collection)
  6. random(Collection coll)
  7. randomElement(Collection coll)
  8. randomElement(Collection in)
  9. randomIterable(Collection col)