Я знаю, что класс C # Random не создает "истинно случайные" числа, но у меня возникает проблема с этим кодом:
public void autoAttack(enemy theEnemy)
{
//Gets the random number
float damage = randomNumber((int)(strength * 1.5), (int)(strength * 2.5));
//Reduces the damage by the enemy's armor
damage *= (100 / (100 + theEnemy.armor));
//Tells the user how much damage they did
Console.WriteLine("You attack the enemy for {0} damage", (int)damage);
//Deals the actual damage
theEnemy.health -= (int)damage;
//Tells the user how much health the enemy has left
Console.WriteLine("The enemy has {0} health left", theEnemy.health);
}
Затем я вызываю функцию здесь (я вызывал ее 5 раз для проверки случайности чисел):
if (thePlayer.input == "fight")
{
Console.WriteLine("you want to fight");
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
}
Однако, когда я проверяю вывод, я получаю одно и то же число для каждых 3 вызовов функций. Однако каждый раз, когда я запускаю программу, я получаю другое число (которое повторяется 3 раза), например:
You attack the enemy for 30 damage.
The enemy has 70 health left.
You attack the enemy for 30 damage.
The enemy has 40 health left.
You attack the enemy for 30 damage.
The enemy has 10 health left.
Затем я пересоберу / отладлю / снова запустите программу и получу другое число вместо 30, но оно будет повторяться все 3 раза.
У меня вопрос: как я могу получать разные случайные числа каждый раз, когда вызываю эту функцию? Я просто получаю одно и то же «случайное» число снова и снова.
Вот случайный вызов класса, который я использовал:
private int randomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}