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: 
```
listNumbers.AddRange(Enumerable.Range(1, 48)
                               .OrderBy(i => rand.Next())
                               .Take(6))

```
The best approach (CPU time-wise) for such tasks is creating an array of all possible numbers and taking 6 items from it while removing the item you just took from the array.
Example:

```
const int min = 1, max = 49;
List<int> listNumbers = new List<int>();
int[] numbers = new int[max - min + 1];
int i, len = max - min + 1, number;

for (i = min; i < max; i++) numbers[i - min] = i;
for (i = 0; i < 6; i++) {
    number = rand.Next(0, len - 1);
    listNumbers.Add(numbers[number]);
    if (number != (len - 1)) numbers[number] = numbers[len - 1];
    len--;
}

```