Hey frenz ,
I just made a console application in C#.NET as:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<int> rand = new List<int>();
rand = abc();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(rand[i]);
}
Console.ReadLine();
}
public static List<int> abc()
{
Random random = new Random();
List<int> nums = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<int> result = new List<int>();
while (nums.Count > 0)
{
int idx = random.Next(0, nums.Count);
result.Add(nums[idx]);
nums.RemoveAt(idx);
}
return result;
}
}
}
Just look at the line => int idx = random.Next(0, nums.Count);
For 1st time iteration of while loop , nums.count will be equal to 10 and so the value of idx can be 10 also . Now, if idx =10 ,
and then there is no index 10 in List nums and it should give an error . But still , my anser comes without any error .
Sample of the answer is:
2
4
6
8
10
9
1
3
7
5
Why is it so??