-1
int Min = 0;
int Max = 20;
int[] test2 = new int[5]; 

Random randNum = new Random();
for (int i = 0; i < test2.Length; i++)
{
    test2[i] = randNum.Next(Min, Max);
}

How can I make sure that the numbers between 0 and 20 will not be the same in the array ? For example I don't want to have in the array twice the number 5.

And how to do it with a List ? or array is better ?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
TheLost Lostit
  • 505
  • 6
  • 28
  • Did you tried? Does it repeats? – Rahul Oct 09 '16 at 13:48
  • Fill a `List` with `Min` to `Max`. Then [Randomize the List](http://stackoverflow.com/questions/273313/randomize-a-listt). Then `Take()` some items. – Reza Aghaei Oct 09 '16 at 14:43
  • 1
    Why not [try this??](https://www.google.com/search?q=c%23+How+can+I+create+a+List+of+random+int+numbers+without+repeating&ie=utf-8&oe=utf-8) – TaW Oct 09 '16 at 15:19

5 Answers5

3

Create an array of Max length and insert numbers from 0 to Max. Then use a random algorithm to choose one element of the array (possibly mod(Max-chosenElementsNumber)). After delete element from array. Done.

2

You can use an HashSet<int>, it doesn't allow duplicates.

int Min = 0;
int Max = 20;
var test2 = new HashSet<int>();

Random randNum = new Random();
while(test2.Count < 5)
{
    test2.Add(randNum.Next(Min, Max));
}

You can also randomize with LINQ:

int Min = 0;
int Max = 20;

Random randNum = new Random();

var test2 = Enumerable.Range(Min, Max - Min + 1)
                      .OrderBy(x => randNum.Next())
                      .Take(5)
                      .ToArray();
Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45
2

Or use LINQ. (By generating a sequence between Min and Max with Enumerable.Range):

var rnd = new Random();
var res = Enumerable.Range(Min, Max - Min + 1).OrderBy(x => rnd.Next()).ToList();

And if you want to pick specific number of the sequence you can use Take method. Like this:

var res = Enumerable.Range(Min, Max - Min + 1).OrderBy(x => rnd.Next()).Take(5).ToList();
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 1
    This method is probably best, it doesn't require 256MB of memory (like stroing an array of all possible integers), and it is guaranteed to terminate unlike any meothd which involves ignoring generated duplicates. – starlight54 Oct 09 '16 at 13:54
0

Just continue to get another random number if you get a duplicated one.

int Min = 0;
int Max = 20;
int[] test2 = new int[5];

Random randNum = new Random();
for (int i = 0; i < test2.Length; i++)
{
    int r;
    do
    {
        r = randNum.Next(Min, Max);
    } while (test2.Contains(r));
    test2[i] = r;
}
zwcloud
  • 4,546
  • 3
  • 40
  • 69
0

This is normal method of sorting integers 0 to 19

            List<KeyValuePair<int, int>> numbers = new List<KeyValuePair<int, int>>();

            Random randNum = new Random();
            for (int i = 0; i < 20; i++)
            {
                numbers.Add(new KeyValuePair<int,int>(i, randNum.Next()) );
            }
            numbers = numbers.OrderBy(x => x.Value).ToList();
            Console.WriteLine(string.Join(",", numbers.Select(x => x.Key).ToArray()));
            Console.ReadLine();
jdweng
  • 33,250
  • 2
  • 15
  • 20