+ 2
How can I get a random element from massive in C#?
I just take a problem. I will make a console rpg. I need to take a random enemy from the massive with enemies. How can I do it?
1 Respuesta
+ 4
string[] enemies = { "Skeleton", "Zombie", "Goblin", "Etc" };
Random random = new Random();
string randomEnemy = enemies[random.Next(enemies.Length)];
Console.WriteLine(randomEnemy);
"random.Next(enemies.Length)" generates a random number between 0 and... whatever you put, and then uses that index to get a random enemy from the array.