Skip to content

5.4.0

Latest

Choose a tag to compare

@BenMakesGames BenMakesGames released this 10 Jul 23:49
· 8 commits to main since this release

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 RandomHelpers class to RandomExtensions
  • Added CollectionExtensions, and moved all collection extension methods into that
  • Updated all the third-party packages...
  • ... except FluentAssertions, which was replaced with Shoudly
  • Replaced .sln file with .slnx file
  • Updated year in LICENSE.md file