I tried the following code to generate random numbers and store them in a HashSet in c#:
class program
{
static void Main(String[] args)
{
Random r=new Random();
HashSet <int> h = new HashSet<int>();
for (int j=0;j<9;++j)
{
h.Add(r.Next(9));
}
int [] array = h.ToArray();
foreach(int i in array)
Console.WriteLine(i);
Console.ReadLine();
}
}
Each time I execute the program, the number of elements being displayed differs. Since I'm using a loop to store 9 values, I expect 9 values to be displayed, which is not happening. What could be the possible error?