@Oleg is right, but that is why this is a problem.
Where scans a list looking for items matching specified criteria. In this case, the criteria are changed for each item. If you made the problem smaller, say an array of 5 elements:
List<Int32> (5 items) 1 2 3 4 5
And then it goes into cycles to find a value that corresponds to some random number (x is Item[i] and r is a random number):
Item 1: x = 1, r = 2 // fail Item 2: x = 2, r = 3 // fail Item 3: x = 3, r = 2 // fail Item 4: x = 4, r = 3 // fail Item 5: x = 5, r = 2 // fail
Note that no element matches this random number, so no element matches the criteria, and First throws an exception!
The fix, as you discovered, is to generate a random number before listing:
int temp=NewMethod(minValue, maxValue, random);
Side note:
It is slightly wrong to use maxValue here:
Enumerable.Range(minValue, maxValue)
Since the second parameter of Enumerable.Range is the length of the resulting set, and not the maximum value. In this case, this works because you start at 1, but the results will be unexpected if you use, say, 99 and 100 - you will get a range from 99 to 198.