Java Collection Random Element chooseRandomThing(Collection possibleroutes)

Here you can find the source of chooseRandomThing(Collection possibleroutes)

Description

Function chooseRandomPath -------------------------- This function takes in a set of possible routes and simply chooses a random element from the list.

License

Open Source License

Parameter

Parameter Description
possiblerebroutes a parameter

Declaration

public static Object chooseRandomThing(Collection<?> possibleroutes) 

Method Source Code

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

import java.util.Collection;

import java.util.Random;

public class Main {
    /**//  www.  j  av a2 s. com
     * Function chooseRandomPath
     * --------------------------
     * This function takes in a set of possible routes and simply 
     * chooses a random element from the list. 
     * @param possiblerebroutes
     * @return
     */
    public static Object chooseRandomThing(Collection<?> possibleroutes) {
        if (possibleroutes == null) {
            return null;
        }
        int size = possibleroutes.size();
        int item = new Random().nextInt(size);
        int i = 0;
        for (Object p : possibleroutes) {
            if (i == item)
                return p;
            i++;
        }
        // shouldn't reach here
        return null;
    }
}

Related

  1. chooseElement(Collection elements, T alternative)
  2. chooseWithoutCheck(Collection collection, long seed)
  3. generateMetricsDataWithAllWrongTypes(String metricPostfix, boolean generateRandomTenant, long collectionTime)
  4. generateMetricsDataWithPartialWrongTypes(String metricPostfix, boolean generateRandomTenant, long collectionTime)
  5. getRandom(Collection collection)