A shuffle can pass once and fail the next time if its test leaves the random input uncontrolled. Give the shuffling method a Random parameter so the caller chooses the generator. Then create two fresh generators with the same seed and compare their complete deck orders inside one test run.
This tutorial uses a single-file .NET 10 console app with no packages. Its self-checking harness was compiled and run on .NET 10.0.13. It checks replay and card preservation, plus an observed difference between two chosen seeds. It does not hard-code a particular shuffled order.
Put the generator under the caller’s control
ShuffleDeck(Random rng) receives its random input source as an ordinary method argument. The method creates a fresh deck of integer card IDs from 0 through 51 and swaps cards using that generator. There is no static generator or hidden construction inside the shuffle.
Microsoft documents that separate Random objects initialized with the same seed produce the same series of numbers. For this test, both objects also receive the same sequence of calls with the same bounds. That makes their shuffle results comparable within the running implementation. See the Random remarks on seeding and repeated sequences.
The loop works backward. At position i, it chooses an index from 0 through i, swaps those two cards, and leaves position i alone for the remaining iterations. The Next(Int32) contract makes the upper bound exclusive, so the argument must be i + 1. Choosing the current position is allowed; that swap simply leaves the card in place.
Run the complete self-check
With the .NET 10 SDK installed, create a console project using these commands. Replace the generated Program.cs with the complete program below before running the final command.
dotnet new console --framework net10.0 --name SeededDeck
cd SeededDeck
# Replace Program.cs with the program below.
dotnet run
using System;
using System.Linq;
using System.Runtime.InteropServices;
const int seed = 20261116;
int[] first = ShuffleDeck(new Random(seed));
int[] replay = ShuffleDeck(new Random(seed));
int[] other = ShuffleDeck(new Random(20261117));
foreach (int[] deck in new[] { first, replay, other })
{
Check(deck.Length == 52 &&
deck.OrderBy(card => card).SequenceEqual(Enumerable.Range(0, 52)),
"A deck lost or duplicated a card.");
}
Console.WriteLine("PASS: all three decks contain cards 0..51 exactly once");
Check(first.SequenceEqual(replay), "The same seed did not replay the order.");
Console.WriteLine("PASS: seed 20261116 replays the same order");
Check(!first.SequenceEqual(other), "These two seeds produced the same order.");
Console.WriteLine("PASS: seeds 20261116 and 20261117 differ in this run");
Console.Error.WriteLine(RuntimeInformation.FrameworkDescription);
static int[] ShuffleDeck(Random rng)
{
int[] deck = Enumerable.Range(0, 52).ToArray();
for (int i = deck.Length - 1; i > 0; i--)
{
int j = rng.Next(i + 1);
(deck[i], deck[j]) = (deck[j], deck[i]);
}
return deck;
}
static void Check(bool condition, string message)
{
if (!condition)
throw new InvalidOperationException(message);
}
The asserted standard output is:
PASS: all three decks contain cards 0..51 exactly once
PASS: seed 20261116 replays the same order
PASS: seeds 20261116 and 20261117 differ in this run
The program also writes its runtime description to standard error; the verification run reported .NET 10.0.13. A failed check throws an exception and causes the console process to exit unsuccessfully. No interactive input is required.
Read each assertion as a separate contract
- Preservation: each result has 52 elements, and sorting it gives exactly 0 through 51. This catches a missing or duplicated card even if the replay comparison passes.
- Replay: two fresh generators seeded with 20261116 produce identical card orders.
SequenceEqualcompares the elements in order; sorting before this comparison would hide a shuffle difference. - A second sample: seed 20261117 produced a different order in the verified run. This is an assertion about these inputs on the tested runtime, not a guarantee that any two distinct seeds must produce distinct shuffles. Different seeds can, in principle, yield the same sequence of chosen indices.
Replay alone cannot prove that a shuffle works: a method that always returns the original deck would pass the same-seed check and the preservation check. The different-seed check catches that particular mistake here. These three checks still do not establish statistical quality or cover every possible shuffle bug.
Reset the generator, not just the deck
Each replay starts with a new Random(seed). Calling the method twice with the same generator object continues consuming its state, even though the method creates a fresh deck each time. It does not restart the sequence. An unrelated draw made before the shuffle also changes which draws the shuffle receives. Keep generator creation at the test boundary and make the call order part of the reproduction.
Passing an object into a method is enough for this small test; no container is needed. For a broader example of supplying behavior from the caller, see dependency inversion in plain C#. For choosing assertions around a caller’s contract, see testing observable behavior.
Keep replay expectations local to the runtime
The Random documentation’s Notes to Callers, following the remarks, states:
The implementation of the random number generator in the Random class isn’t guaranteed to remain the same across major versions of .NET.
Do not persist a specific expected shuffle as a portable contract across .NET versions. This harness derives both replay results in the same process instead. When recording a failure, save the seed, runtime version, inputs and call order. A seed by itself is insufficient for durable replay across runtime upgrades or changes to the shuffle’s calls. If long-term replay is a requirement, record the resulting card order or specify and version an algorithm you control.
Use the same-seed comparison to make a failure reproducible in your current environment, and retain the preservation assertion to check what the shuffle actually promises about its output.