I know that the C # Random class does not do “true random” numbers, but I am facing a problem with this code:
public void autoAttack(enemy theEnemy)
{
float damage = randomNumber((int)(strength * 1.5), (int)(strength * 2.5));
damage *= (100 / (100 + theEnemy.armor));
Console.WriteLine("You attack the enemy for {0} damage", (int)damage);
theEnemy.health -= (int)damage;
Console.WriteLine("The enemy has {0} health left", theEnemy.health);
}
Then I call the function here (I called it 5 times to check if the numbers were random):
if (thePlayer.input == "fight")
{
Console.WriteLine("you want to fight");
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
}
However, when I check the output, I get the exact number for every 3 function calls. However, every time I run the program, I get a different number (which is repeated 3 times):
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.
Then I rebuild / debug / run the program again and get a different number instead of 30, but it will be repeated all three times.
My question is: how can I get different random numbers every time I call this function? I just get the exact same random number over and over again.
Here is a random call to the class that I used:
private int randomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}