Code: Select all
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);
}
< /code>
Ich rufe dann die Funktion hier auf (ich habe sie 5 mal angerufen, um zu überprüfen, ob die Zahlen zufällig waren): < /p>
if (thePlayer.input == "fight")
{
Console.WriteLine("you want to fight");
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
}
< /code>
Wenn ich jedoch die Ausgabe überprüfe, erhalte ich genau die gleiche Nummer für jede 3 Funktionsaufrufe. Jedes Mal, wenn ich das Programm ausführe, erhalte ich jedoch eine andere Zahl (die sich dreimal wiederholt): < /p>
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.
Meine Frage lautet: Wie kann ich sicherstellen, dass ich jedes Mal, wenn ich diese Funktion nenne, eine andere zufällige Nummer bekomme? Ich bekomme immer wieder die gleiche "zufällige" Zahl. />
Code: Select all
private int randomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}