- Home /
Randoim values in a dynamic array C#
I have a relatively simple issue, I'm working on a game where you get shown a sequence of arrows and then you have to remember the order and press the keys accordingly, as the game gets more difficult more arrows are added to the sequence.
my plan to solve that is to have a sort of dynamic array store a random number between 1 and 4, and then add more to the array over time. How would you do something like this, if its not possible how would you create and store random values for a memory based game?
Answer by UnityCoach · Oct 20, 2018 at 10:09 PM
I don't mean to promote my courses here, but I touch on this quite in depth in this course on Model View Controller design pattern. and this one touching on OOP and SOLID design.
That said, you can do something like :
int [] RandomValues (int length, int range)
{
int[] rv = new rv[length];
for (int i = 0 ; i < length ; i++)
rv[i] = Random.Range (0, range);
return rv;
}
Random.Range(int, int)
never returns the max value, so if range = 4, Random.Range(0, 4) will return any value between 0 and 3, thus 4 possibilities 0, 1, 2 and 3