Shaitan00 Posted June 24, 2006 Posted June 24, 2006 I have a variable of type "MoveDirection" that is currently used like this: MoveDirection direction = MoveDirection.Right; However, instead of always going right (MoveDirection.Right) I wanted to make it RANDOM, so that "direction" could be any of the four possible results in the enum MoveDirection Given the following Enum: public enum MoveDirection { Right, Up, Down, Left } I need a way to generate a Random value of the enum MoveDirection, I thought of using Rand but am unable to generate anything other then just random numbers and not random enums of type MoveDirection. Any ideas, hints, and help would be greatly appreciated, thanks Quote
Leaders snarfblam Posted June 24, 2006 Leaders Posted June 24, 2006 Simply put, cast a randomly generated number to a MoveDirection value. Random rand = new Random(); // You need to crop the range to 0...3 since your enum's // integer values are 0, 1, 2, and 3. MoveDirection whichWayToGo = (MoveDirection)(rand.Next() % 4); MessageBox.Show(whichWayToGo.ToString()); Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.