A deterministic shuffle tool for generating reproducible index mappings and shuffling Spotify playlists.
- Deterministic shuffling: Given the same key/seed and size, always produces the same shuffle order
- Index mapping: Generate shuffled index arrays for any size
- Spotify integration: Shuffle Spotify playlists with a new deterministic order
- Deduplication: Optionally remove duplicate items when shuffling playlists
- Dry-run mode: Preview shuffle results without creating a new playlist
cargo install --path .Or build from source:
cargo build --releaseGenerate a shuffled index mapping for a given size and key:
# Basic usage
shfl ind-map <SIZE> <SHUFFLE_KEY>
# Example: Generate shuffled indices for 10 items
shfl ind-map 10 my-secret-key
# Output: [3, 7, 1, 9, 0, 5, 2, 8, 4, 6]
# With timing information
shfl ind-map 1000 my-key -tArguments:
SIZE- Number of indices to shuffle (0 to N-1)SHUFFLE_KEY- Key for deterministic shuffle (same key = same result)-t- Show execution time
Shuffle a Spotify playlist into a new playlist with deterministic ordering:
shfl spotify <ACCESS_TOKEN> <SOURCE_NAME> <DEST_NAME> <SEED> [OPTIONS]Arguments:
ACCESS_TOKEN- Spotify OAuth access tokenSOURCE_NAME- Name of the source playlist to shuffleDEST_NAME- Name for the new shuffled playlistSEED- Seed for deterministic shuffle
Options:
-d- Remove duplicate tracks-n, --dry-run- Preview what would happen without creating the playlist
Example:
# Shuffle a playlist
shfl spotify "your-token" "My Playlist" "My Playlist (Shuffled)" "2024-shuffle"
# Shuffle with deduplication
shfl spotify "your-token" "My Playlist" "My Playlist (Shuffled)" "2024-shuffle" -d
# Preview shuffle without creating playlist
shfl spotify "your-token" "My Playlist" "My Playlist (Shuffled)" "2024-shuffle" -nNote: Only tracks are supported; episodes in playlists will be skipped with a warning. The tool uses Spotify's /items API endpoints.
The shuffle algorithm provides the following guarantees:
-
Reproducibility: The same
shuffle_key/seedandsizewill always produce the identical shuffle order, regardless of when or where the program is run. -
Algorithm: Uses ChaCha8 CSPRNG seeded with a 64-bit hash (xxHash) of the shuffle key.
-
Stability: The shuffle is a valid permutation - every index from 0 to size-1 appears exactly once.
-
Cross-platform: Results are consistent across different operating systems and architectures.
shuffle_key → xxHash64 → 64-bit seed → ChaCha8 RNG → Fisher-Yates shuffle
- The shuffle key is hashed using xxHash64 to produce a 64-bit seed
- The seed initializes a ChaCha8 random number generator
- A Fisher-Yates shuffle is performed on the index array
- The result is a deterministic permutation of indices
To use the Spotify shuffle feature, you need an OAuth access token with the following scopes:
playlist-read-privateplaylist-modify-privateplaylist-modify-public
You can obtain a token through:
- Spotify Developer Dashboard
- OAuth authorization flow
- Third-party tools like spotify-token-gen
Use the same seed to recreate the exact same shuffle:
# First run
shfl spotify "$TOKEN" "Favorites" "Favorites Jan 2024" "january-2024"
# Running again with same seed produces identical order
shfl spotify "$TOKEN" "Favorites" "Favorites Jan 2024 Copy" "january-2024"Create consistently shuffled playlists using date-based seeds:
shfl spotify "$TOKEN" "Workout Mix" "Workout Jan" "2024-01"
shfl spotify "$TOKEN" "Workout Mix" "Workout Feb" "2024-02"MIT