Unclaimed Package Is this your package? Claim it to unlock full analytics and manage your listing.
Claim This Package

Install via UPM

Add to Unity Package Manager using this URL

https://www.pkglnk.dev/sharpshufflebag.git?path=SharpShuffleBag.Unity/Packages/com.chrisyarbrough.sharpshufflebag

README Markdown

Copy this to your project's README.md

Style
Preview
pkglnk installs badge
## Installation

Add **Sharp Shuffle Bag** to your Unity project via Package Manager:

1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/sharpshufflebag.git?path=SharpShuffleBag.Unity%2FPackages%2Fcom.chrisyarbrough.sharpshufflebag
```

[![pkglnk](https://www.pkglnk.dev/badge/sharpshufflebag.svg?style=pkglnk)](https://www.pkglnk.dev/pkg/sharpshufflebag)

README

NuGet Version openupm

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.

Comments

No comments yet. Be the first!