Using rand() to assign enum

Ok so I have a couple enums right now, and I need a way to randomly assign one of a few different options to an object. Can I just use rand() to assign this, since enums are really just an int value?

1
2
enum color {White = 0, Brown, Black};
color = rand()%3;


Not sure if this works, or if there is a better way
Last edited on
Well, sort of.
1
2
enum Color {Red, Green, Blue};
Color color = Color(rand()%3);
You have to cast.
Last edited on
Great! That worked, thanks :D
Topic archived. No new replies allowed.