Skip to content

Thread-safe list, array and span shuffle extension library, using Fisher-Yates shuffle and optional cryptographically-strong random.

License

Notifications You must be signed in to change notification settings

MarkCiliaVincenti/ListShuffle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

22427b4 · Aug 20, 2024

History

57 Commits
Dec 7, 2023
Aug 20, 2024
Aug 20, 2024
Mar 13, 2022
Mar 13, 2022
Aug 20, 2024
Aug 20, 2024
Aug 20, 2024
Aug 20, 2024
Dec 7, 2023
Mar 13, 2022
Mar 13, 2022

Repository files navigation

ListShuffle ListShuffle

GitHub Workflow Status Nuget Nuget Codacy Grade

Thread-safe list, array and span shuffle extension library, using Fisher-Yates shuffle and optional cryptographically-strong random.

Installation

The recommended means is to use NuGet, but you could also download the source code from here.

Usage (using Random)

var myList = new List<string>();
myList.Add("Item A");
myList.Add("Item B");
myList.Add("Item C");

myList.Shuffle();
var myArray = new string[3];
myArray[0] = "Item A";
myArray[1] = "Item B";
myArray[2] = "Item C";

myArray.Shuffle();
Span<string> mySpan = stackalloc string[3];
mySpan[0] = "Item A";
mySpan[1] = "Item B";
mySpan[2] = "Item C";

mySpan.Shuffle();

Usage (using RandomNumberGenerator)

If you need cryptographically-strong random in order to shuffle the list, array or span, you can use the CryptoStrongShuffle method instead. This method is less performant and only use it if you absolutely need to.

var myList = new List<string>();
myList.Add("Item A");
myList.Add("Item B");
myList.Add("Item C");

myList.CryptoStrongShuffle();
var myArray = new string[3];
myArray[0] = "Item A";
myArray[1] = "Item B";
myArray[2] = "Item C";

myArray.CryptoStrongShuffle();
Span<string> mySpan = stackalloc string[3];
mySpan[0] = "Item A";
mySpan[1] = "Item B";
mySpan[2] = "Item C";

mySpan.CryptoStrongShuffle();