I have a list of a custom class, (called enemy
), named enemyList
. I use a foreach loop. Inside, I generate a random number between 1 and 200,000. Then I save it to a list outside of the foreach loop:
try
{
List<int> randoms = new List<int>();
foreach (enemy m in enemyList)
{
Random r = new Random();
int i = r.Next(1, 2000);
randoms.Add(i);
}
Then, when that is done, under the foreach loop, I do this:
int highest = randoms.Max();
int rate = -1;
Next, I use another foreach loop to figure out which value in randoms
equals the highest number.
foreach (int number in randoms)
{
rate++;
if (highest == number)
{
return rate;
}
}
}
catch (Exception)
{
return 0;
}
When I run it, it displays which item is the highest, (example: if it is the first in the randoms
list, then display 0
). But for some reason, it always returns 0. But if I walk through the code, it works properly. Can anyone explain why this happens so I can fix it?