How to exclude int values from Random.Range?

Hey,

I’m trying to have an Enemy move in random directions.

What I’ve done is set up 8 arrays of ints and for each value from the array I have a movement direction put in place.

My enemy will choose a random direction each time it hits one of the 4 walls (left,right,top,bottom). The problem is that if for the top wall I need for example ints 1,3,5 (which would be the accepted directions), how could I use Random.Range to make it select a value of either 1, 3 or 5 and never 2?

Regards,
Eugen

var validChoices = [1,3,5];

``````   function GetRandom() : int
{
return validChoices[Random.Range(0, validChoices.Length)];
}
``````

I think the simplest way is to create a function where you pass the direction that you don’t want chosen and it then re-tries the random number if that is chosen.

eg.,

``````int GetNewDir(int NotThisDirection)
{
int newDir;

newDir = Random.Range(1,8);
while (newDir == NotThisDirection)
{
newDir = Random.Range(1,8);
}

return newDir;

}
``````