Java Random Element randomElement(int[] anArray)

Here you can find the source of randomElement(int[] anArray)

Description

random Element

License

Apache License

Declaration

public static int randomElement(int[] anArray) 

Method Source Code


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

import java.util.Random;

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

    public static int randomElement(int[] anArray) {
        if (anArray.length == 0) {
            return 0;
        }/*from www. j a  va2  s .c o  m*/
        return anArray[nextInt(anArray.length)];
    }

    public static String randomElement(String[] anArray) {
        if (anArray.length == 0) {
            return "";
        }
        return anArray[nextInt(anArray.length)];
    }

    public static int nextInt() {
        return rand.nextInt();
    }

    public static int nextInt(int n) {
        return rand.nextInt(n);
    }
}

Related

  1. randomEle(List list)
  2. randomElem(Random rand, Set partitions)
  3. randomElement(Collection coll)
  4. randomElement(E[] array)
  5. randomElement(final Set set)
  6. randomElement(List list)
  7. randomElement(T... values)
  8. randomElement(T[] array)
  9. randomElementOf(final List list)