내가 C#을 임의의 클래스는 "진정한 무작위로"숫자를하지 않는 것을 알고있다 "랜덤"되는 것이 아니라,이 코드에 문제가 함께 간다 :C# 임의의 숫자는
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 여기에 함수를 호출 (숫자가 무작위로한다면 나는 확인을 위해 그것을 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);
}
'randomNumber' 함수는 어떻게 생겼습니까? – Nija
[이 게시물] (http://www.codeducky.org/random-numbers-c-net-primer/)에서이 문제와 .NET Random 클래스의 다른 문제를 설명 할 수 있습니다. – ChaseMedallion