Skip to content

Instantly share code, notes, and snippets.

@cleydyr
Created April 25, 2021 22:31
Show Gist options
  • Save cleydyr/69b68996d836d004058a46132c7cc6fb to your computer and use it in GitHub Desktop.
Save cleydyr/69b68996d836d004058a46132c7cc6fb to your computer and use it in GitHub Desktop.
A class with a static method to choose randomly among multiple values of a given enum
import java.util.Random;
public class RandomEnumDrawer {
private static Random _rand = new Random(System.currentTimeMillis());
public static <E extends Enum<E>> E randomValue(Class<E> enumClass) {
E[] values = enumClass.getEnumConstants();
E randomValue = values[_rand.nextInt(values.length)];
return randomValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment