sé que la clase Random C# no hace "verdaderos azar" números, pero estoy subiendo con un problema con este código:Números C# azar no están siendo "aleatoria"
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);
}
I a continuación, llamar a la función aquí (me llamó 5 veces en aras de comprobar si los números eran al azar):
if (thePlayer.input == "fight")
{
Console.WriteLine("you want to fight");
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
thePlayer.autoAttack(enemy1);
}
Sin embargo, al comprobar la salida, me da exactamente el mismo número por cada 3 llamadas a funciones. Sin embargo, cada vez que ejecute el programa, aparece un número diferente (que se repite 3 veces) así:
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.
haré luego reconstruir/debug/a ejecutar el programa, y obtener un número diferente en lugar de 30 , pero repetirá las 3 veces.
Mi pregunta es: ¿cómo puedo asegurarme de obtener un número aleatorio diferente cada vez que llamo a esta función? Solo estoy obteniendo el mismo número "aleatorio" una y otra vez.
Aquí es la llamada clase al azar que he utilizado:
private int randomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
¿Cómo es tu función 'randomNumber'? – Nija
Es posible que desee consultar [esta publicación] (http://www.codeducky.org/random-numbers-c-net-primer/), que trata este problema, así como otros problemas con la clase .NET Random – ChaseMedallion