I am playin around with generating random numbers, and I can get them to generate, although my console.writeline(randomList[i]); seems to loop twice and then displays both results, it should only display one value per loop, and the user should have to hit any-key to get the next result. Can someone give a little insight on this? Thanks.
class Program
{
static void Main(string[] args)
{
GenerateRandomNumbers();
}
private static void GenerateRandomNumbers()
{
//Initialize an array
int[] randomList = new int[1000];
//Initialize an instance of random class
Random rnd = new Random();
// integer variable
int counter = 0;
while (counter < 1000)
{
//store random num
int random = rnd.Next(1, 1001);
if (Array.IndexOf(randomList, random) <= 0)
{
//store random number into Array
randomList[counter] = random;
counter++;
}
}
//output elements in Array
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(randomList[i]);
Console.Read();
}
//output number of elements in Array
// Console.WriteLine(counter);
Console.Read();
}
The solution is to use the Console.ReadLine() in the loop instead of the Console.Read()