Java Collection Random Element randomElement(Collection coll)

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

Description

random Element

License

Apache License

Declaration

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

Method Source Code


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

import java.util.Collection;

public class Main {
    public static <T> T randomElement(Collection<T> coll) {
        int num = (int) (Math.random() * coll.size());
        for (T t : coll)
            if (--num < 0)
                return t;
        throw new AssertionError();
    }//from  ww w.j a  v  a  2  s.c  o  m
}

Related

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