11

Say you have an enum with some elements

public enum LightColor {
   RED, YELLOW, GREEN
}

And would like to randomly pick any color from it.

I put colors into a

public List<LightColor> lightColorChoices = new ArrayList<LightColor>();

lightColorChoices.add(LightColor.GREEN);
lightColorChoices.add(LightColor.YELLOW);
lightColorChoices.add(LightColor.RED);

And then picked a random color like:

this.lightColor = lightColorChoices.get((int) (Math.random() * 3));

All of this (while working fine) seems needlessly complicated. Is there a simplier way to pick a random enum element?

James Raitsev
  • 82,013
  • 132
  • 311
  • 454
  • as your fellow developer for a random number ? ... sorry, no. – ic3 Nov 13 '11 at 19:50
  • related question: http://stackoverflow.com/questions/5269250/random-value-from-enum-with-probability – Ray Tayek Nov 17 '11 at 17:43
  • I think this one is more akin to the problem at hand (hint: look for trashgod's answer): http://stackoverflow.com/questions/1972392/java-pick-a-random-value-from-an-enum – DPM Feb 12 '12 at 22:55

6 Answers6

24

Java's enums are actually fully capable Objects. You can add a method to the enum declaration

public enum LightColor {
    Green,
    Yellow,
    Red;

    public static LightColor getRandom() {
        return values()[(int) (Math.random() * values().length)];
    }
}

Which would allow you to use it like this:

LightColor randomLightColor = LightColor.getRandom();
Lucas
  • 11,997
  • 7
  • 62
  • 114
8
LightColor random = LightColor.values()[(int)(Math.random()*(LightColor.values().length))];
Nightfirecat
  • 10,836
  • 6
  • 32
  • 50
b_erb
  • 19,644
  • 8
  • 51
  • 63
5

Use Enum.values() to get all available options and use the Random.nextInt() method specifying the max value. eg:

private static Random numberGenerator = new Random();
public <T> T randomElement(T[] elements)
  return elements[numberGenerator.nextInt(elements.length)];
}

This can then be called as such:

LightColor randomColor = randomElement(LightColor.values());
Rich O'Kelly
  • 38,811
  • 8
  • 78
  • 108
3

This should be just easy as shown below

LightColor[] values = LightColor.values();
LightColor value = values[(int) (Math.random() * 3)];
Kowser
  • 7,713
  • 6
  • 39
  • 60
0

So reading Kowser's answer, I came up with something here. Given an enum ChatColor containing different colors, you could do the following:

private ChatColor getRandomColor() {
    ChatColor randomColor = ChatColor.values()[random.nextInt(ChatColor
            .values().length - 1)];
    ChatColor[] blacklist = { ChatColor.BOLD, ChatColor.ITALIC,
            ChatColor.MAGIC, ChatColor.RESET, ChatColor.STRIKETHROUGH,
            ChatColor.UNDERLINE };
    while (Arrays.asList(blacklist).contains(randomColor)) {
        randomColor = ChatColor.values()[random
                .nextInt(ChatColor.values().length)];
    }
    return randomColor;
}

and even have a blacklist.

ionree
  • 45
  • 1
  • 1
  • 5
0

You could associate an integer id to each enum color, and have a valueOf(int id) method that returns the corresponding color. This will help you get rid of the list..

Tiberiu

jumping-jack
  • 237
  • 1
  • 3