Randomly selecting from an enumurated list

Started by
2 comments, last by UnknownPlayer 20 years, 10 months ago
Can this be done safely? The way I imagine doing it is randomly generating an integer in the range of the list and then using that as my value, but MSDN says the results of explicitely casting an int back to an enum type is undefined - this yells ''unsafe!'' at me, but I''d like to get some other opinions and or methods of doing it. Any ideas? ##UnknownPlayer##
##UnknownPlayer##
Advertisement
Why don''t you just try it and see if it works out with what your doing. Last time i used a random integer with an enum it worked perfectly.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
You could load up an array of the enum type, and the random integer range could be the index that you look up.

Likely you'll just populate [1]=enum1, [2]=enum2, [3]=enum3, etc. but at least you aren't casting an integer to an enum to get the result. You are instead looking it up in a table.

[edited by - Waverider on May 28, 2003 9:48:22 AM]
It's not what you're taught, it's what you learn.
std::map<int, myEnum>

This topic is closed to new replies.

Advertisement