Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
Working with randomized collections in .NET just got easier. Whether you’re displaying rotating messages, selecting ads, or building dynamic UI components, having a reliable way to retrieve randomized items—and optionally loop through them endlessly—can streamline your workflow. Enter CollectionRandomizer<T>, a new utility class I created while improving my dotNetTips Utility Dev App. CollectionRandomizer is in the DotNetTips.Spargine.Core assembly and NuGet package.
This lightweight, reusable type simplifies collection shuffling, supports infinite looping with automatic reshuffling, and is production-tested. If you’re tired of manually tracking shuffled indexes or reimplementing random logic, this tool can save you time and headaches.
Why I Built It
While updating my dotNetTips Utility Dev App to pull speaker and event messages from an Azure-hosted project, I ran into a familiar need:
Rotate messages randomly — and keep doing it forever, without repeating the same order until the full list is exhausted.
So instead of reinventing the wheel again, I abstracted the logic into a dedicated, reusable class:
CollectionRandomizer<T>
Now it’s baked right into my app — and it works flawlessly.
What is CollectionRandomizer<T>
CollectionRandomizer<T> simplifies the process of retrieving randomized items from a collection. It supports one-time randomization or infinite looping, reshuffling automatically when needed.
It’s fast, thread-safe, generic, and production-tested.
Key Features
- Randomizes the collection on initialization
- Optional infinite looping with automatic reshuffling
- Eliminates the need to track indexes manually
- Thread-safe
- Lightweight, generic, and reusable
- Fully tested and production-ready
Constructors
CollectionRandomizer<T>(IEnumerable<T> collection, bool repeat = false)
- CollectionRandomizer<T>(IEnumerable<T> collection, bool repeat = false)
Creates a randomized sequence from a collection with optional looping behavior.
Methods
- GetEnumerator()
Iterates through the randomized collection. - GetNext()
Returns the next item. If repeating and the end is reached, the list reshuffles. - PeekNext()
Returns the next item without advancing. - Reset()
Reshuffles and restarts the sequence. - SkipNext()
Skips the next item. - ToArray()
Returns the current shuffled snapshot.
Properties
- Count
Total items in the collection. - CurrentIndex
Current position in the randomized sequence. - HasRemainingItems
Indicates whether more items remain. - PercentComplete
Progress through the randomized cycle.
How To Use
Declaration & Initialization:
private CollectionRandomizer<Ad> _adRandomizer;
this._adRandomizer = new CollectionRandomizer<Ad>(ads, repeat: true);
Retrieve an Item:
var ad = this._adRandomizer.GetNext();
Check Remaining Items:
if (_adRandomizer.HasRemainingItems)
{
// Continue processing...
}
Why You’ll Like Using It
CollectionRandomizer<T> has already simplified how I rotate ads, notifications, speaker messages, quotes, and playlist items. You just drop it into your project and stop worrying about random-order logic.
I’ll also be integrating this into the Spargine Dev Tool using .NET Aspire.
Summary
Randomizing collections shouldn’t require custom index tracking, brittle logic, or repeated reinvention. CollectionRandomizer<T> delivers a clean, reusable, and production-ready solution for handling randomized sequences in .NET—whether you need a single shuffled pass or an endlessly looping, automatically reshuffled stream of items.
Built from real-world needs and battle-tested in production, this lightweight utility removes complexity while giving you full control over randomized iteration. If your application rotates messages, ads, UI elements, playlists, or any data that benefits from controlled randomness, CollectionRandomizer<T> lets you focus on building features instead of managing shuffle logic.
Special thanks to Kristine Tran for reviewing and rigorously testing the implementation—you’re a CodeStar, as always.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

