SharpShuffleBag 1.0.2

dotnet add package SharpShuffleBag --version 1.0.2
NuGet\Install-Package SharpShuffleBag -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SharpShuffleBag" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpShuffleBag --version 1.0.2
#r "nuget: SharpShuffleBag, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SharpShuffleBag as a Cake Addin
#addin nuget:?package=SharpShuffleBag&version=1.0.2

// Install SharpShuffleBag as a Cake Tool
#tool nuget:?package=SharpShuffleBag&version=1.0.2

Sharp Shuffle Bag

An efficient and convenient shuffle bag implementation in C# for .NET and Unity projects.

Usage

ShuffleBag<string> cradle = new() { "Whiskers", "Mia", "Dabbles" };

// Optionally, set a custom random source.
cradle.RandomSource = new SystemRandomSource(seed: 42);

// Retrieve 9 random cat names. Each cat is picked 3 times and no cat is picked twice in a row.
for (int i = 0; i < 9; i++)
{
    string randomCat = cradle.Next();
}

Motivation

Random number generators, as often used in games, have characteristics which can feel unnatural to humans. For example, a series of pseudo-random numbers like this:

[1, 2, 3, 4, 1, 1, 1, 1]

The shuffle bag algorithm attempts to make random feel more natural:

  • Fill a bag with numbers from 1 to 4
  • Pick a random number from the bag
  • Continue until the bag is empty
  • Refill the bag

This will produce a non-repeating series like this:

[3, 4, 1, 2, 4, 3, 2, 1]

The Sharp Shuffle Bag implements this algorithm without causing unnecessary GC allocations and with consistent, fast performance. The randomization happens in-place while calling Next() and therefore avoids the larger performance spike of reshuffling/refilling the bag when it's empty.

Support

  • .NET Standard 2.1
  • Unity 2021.3 or newer

Features

  • Use the ShuffleBag<T> class to define a collection of items of type T.
  • Use shuffleBag.Next() to get the next random item from the bag. The bag will never empty.
  • Consecutive calls of Next() will never return the same item twice.
  • Use shuffleBag.Next(markUsed: true) for a fixed-size bag.
  • Efficient implementation: The underlying collection is not resized, instead, an index is used to perform the random selection.
  • Set a custom RandomSource for fine-grained control (e.g. to make the bag deterministic or set a seed).
  • Shuffle.FisherYates(IList<T> list) utility method to shuffle a list in-place.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 93 2/25/2024
1.0.1 96 2/11/2024
1.0.0 88 2/10/2024