Home > Backend Development > C++ > Is `OrderBy(x => r.Next())` a Reliable Way to Shuffle a Collection?

Is `OrderBy(x => r.Next())` a Reliable Way to Shuffle a Collection?

Barbara Streisand
Release: 2025-01-31 19:11:11
Original
444 people have browsed it

<.> R.next ()) `A Roths to shuffle a collection? " /> Is `OrderBy(x =>
</p> <<> Is it reliable with <p> random sorting? <strong> <<>
<code>OrderBy(x => r.Next())</code> Some developers try to use the following methods to randomly sort out the collection: </strong>
</p>
<p> However, the effectiveness of this method and the underlying mechanism are worth exploring. Let's analyze it in depth. </p>
<div class=

var r = new Random();
var shuffled = ordered.OrderBy(x => r.Next());
Copy after login
<法> The effectiveness of the algorithm

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 implementation of the expansion method of the shuffle

To use the Fisher-Yates algorithm to implement a basic SHuffle extension method, please follow the steps below:

The input collection is converted to array. Use the predetermined function (for example,

) the element in the random exchange list.

Return to exchanges.
  1. Using this method, developers can quickly and effectively sort the collection at the time complexity of O (N).
  2. Next <能> Performance optimization
  3. In order to further improve the performance, the expansion method can effectively exchange elements and return them in the random sorting process, thereby minimizing the time consumed by unnecessary operation.
">

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template