<.> R.next ()) `A Roths to shuffle a collection?
" />
var r = new Random(); var shuffled = ordered.OrderBy(x => r.Next());
This algorithm is not the best random sorting method. It sorted the elements based on random numbers assigned to each element, resulting in time complexity is O (N Log N). This is much lower than the Fisher-Yates algorithm (time complexity is O (n)). <底> The underlying mechanism
The algorithm assigns a random number for each element in the collection, and then sorts the elements based on these numbers. This process effectively random the order of elements, but this is not a real random sorting operation.
<合> More suitable random sorting method
Although this method can produce random sorting results, it is not the first method because of its low efficiency. The Fisher-Yates random sorting algorithm has lower calculation complexity and higher efficiency. In addition, a special SHUFFLE expansion method can provide a clear and concise random sorting operation expression.
The input collection is converted to array. Use the predetermined function (for example,
) the element in the random exchange list.Return to exchanges.
Next
<能> Performance optimization The above is the detailed content of Is `OrderBy(x => r.Next())` a Reliable Way to Shuffle a Collection?. For more information, please follow other related articles on the PHP Chinese website!