+ 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?

30th Dec 2024, 11:11 PM
Кирилл Садеков
Кирилл Садеков - avatar
1 Answer
+ 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.
31st Dec 2024, 12:00 AM
Mateo Bernald
Mateo Bernald - avatar