Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Leaders
Posted

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());

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...