Since you sort and reverse the array inside the loop you basically force the biggest number to be last.
For example in 5 sized array:
i = 0
arr[i] = rand:
arr -> 5 0 0 0 0
sort and reverse
arr -> 5 0 0 0 0
output = 5
i = 1
arr[i] = rand:
arr -> 5 8 0 0 0
sort and reverse
arr -> 8 5 0 0 0
output = 5
i = 2
arr[i] = rand:
arr -> 8 5 7 0 0
sort and reverse
arr -> 8 7 5 0 0
output = 5
i = 3
arr[i] = rand:
arr -> 8 5 7 2 0
sort and reverse
arr -> 8 7 5 2 0
output = 2
i = 4
arr[i] = rand:
arr -> 8 7 5 2 0
sort and reverse
arr -> 8 7 5 2 0
output = 2
As you can see, your output will always be the smallest number and as you go on, less and less number generated by the Random.Next
can be smaller than the current smallest so the current smallest will most likely appear again as the output.
If you want to make a list of 100 numbers and print them you can use this Linq
:
(new int[100])
.Select(x => r.Next(100))
.ToList()
.ForEach(x => Console.WriteLine(x));
If you want the numbers to be distinct (all the numbers between 0 and 99 but you want them to appear only once) you can use the following code:
private static Random r = new Random();
public static int[] GetRandomArray(int size)
{
SortedDictionary<double, int> sortedSet = new SortedDictionary<double, int>();
for (int index = 0; index < size; index++)
{
sortedSet.Add(r.NextDouble(), index);
}
return sortedSet.Select(x => x.Value).ToArray();
}