Here's a function that will generate a list in ascending order of randomly chosen integers in a certain range; every value in the list will be unique(no duplicates):
using Linq;
public List<int> generateRandomList(int sizeOfList, int min, int max)
{
max++;
Random RandomGenerator = new Random(Guid.NewGuid().GetHashCode());
List<int> randomIntResults = new List<int>();
int currentInt = RandomGenerator.Next(min, max);
for(int x = 0; x < sizeOfList; x++)
{
while (randomIntResults.Contains(currentInt))
{
currentInt = RandomGenerator.Next(min, max);
}
randomIntResults.Add(currentInt);
}
return randomIntResults.OrderBy(item => item).ToList();
}
If I wanted a List of 100 integers in the range 1~1000 chosen randomly:
List<int> randomInt100 = generateRandomList(100, 1, 1000);
Note there's no argument checking done above: you could do something stupid that would create an endless loop, like:
List<int> randomInt100 = generateRandomList(100, 1, 10);