Question: Hi everyone I am trying to generate 6 different numbers on the same line in c# but the problem that i face is some of the numbers are repeating on the same line.Here is my code to

```
var rand = new Random();
List<int> listNumbers = new List<int>();
int numbers = rand.Next(1,49);
for (int i= 0 ; i < 6 ;i++)
    {
         listNumbers.Add(numbers);
         numbers = rand.Next(1,49);
    }

```

somewhere my output is

```
17 23 23 31 33 48

```

Answer: 
Check each number that you generate against the previous numbers:

```
List<int> listNumbers = new List<int>();
int number;
for (int i = 0; i < 6; i++)
{
  do {
     number = rand.Next(1, 49);
  } while (listNumbers.Contains(number));
  listNumbers.Add(number);
}

```

Another approach is to create a list of possible numbers, and remove numbers that you pick from the list:

```
List<int> possible = Enumerable.Range(1, 48).ToList();
List<int> listNumbers = new List<int>();
for (int i = 0; i < 6; i++)
{
  int index = rand.Next(0, possible.Count);
  listNumbers.Add(possible[index]);
  possible.RemoveAt(index);
}

```
What you do is to generate a random number each time in the loop. There is a chance of course that the next random number may be the same as the previous one. Just add one check that the current random number is not present in the sequence. You can use a while loop like: while (currentRandom not in listNumbers): generateNewRandomNumber