Which one is faster?
Im doing tests on a ConcurrentDictionary and want to randomize it and just return 1 result. Essentially pick a random result from it.
Which method is faster/more efficient? Efficiency as in, as little cpu/memory while having low possible errors like conflicts and such.
Method A.
Random rand = new Random();
var result = concDict.ElementAt(rand.Next(concDict.Count() - 1));
Method B.
var result = concDict.OrderBy(x=>Guid.NewGuid()).First();
In my "vague" testing I dont see much difference apart from Method B being more efficient. Method A can succum to the concDict's Count being out of sync with the concDict.ElementAt causing ArgumentOutOfRangeException: 'maxValue' must be greater than zero.
while Method B literally cant cause that.