Archived

This topic is now archived and is closed to further replies.

Randomly selecting from an enumurated list

This topic is 5313 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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##

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites