New Method: Queue<T> ToShuffledQueue<T>(this IEnumerable<T> source, Random rng)
(And a version for Span<T>s. Just in case you love spans that much.)
Use this to create a shuffled queue from a collection; for example:
Queue<int> shuffledNumbers = Enumerable.Range(1, 100).ToShuffledQueue(Random.Shared);When is this useful? It's not uncommon to pull some random subset of items out of a collection as follows:
List<SomeItem> myItems = [ ... ];
myItems.Shuffle(Random.Shared);
var theItemsIWant = myItems.Take(10).ToList();However, in some scenarios, such as generating items in a complex .Select, you'd might like to pull items out one at a time, as needed (and simply indexing a shuffled array won't do). In these scenarios, turning the collection into a Queue and .Dequeue-ing items on-the-fly may simplify your code.
Behind-the-scenes Updates
- Renamed
RandomHelpersclass toRandomExtensions - Added
CollectionExtensions, and moved all collection extension methods into that - Updated all the third-party packages...
- ... except FluentAssertions, which was replaced with Shoudly
- Replaced
.slnfile with.slnxfile - Updated year in
LICENSE.mdfile