Here's the JavaScript code that shuffles an array deterministically using xorshift32 as the RNG, but instead of Fisher-Yates, it associates a random number with each element and sorts by those random values:
class XorShift32 {
constructor(seed = 1) {
// Ensure seed is a positive 32-bit integer
this.state = (seed >>> 0) || 1; // Use 1 as fallback if seed is 0
}
// Generate next random number in [0, 1)
random() {
// Xorshift32 algorithm
this.state ^= this.state << 13;
this.state ^= this.state >>> 17;
this.state ^= this.state << 5;
this.state = this.state >>> 0; // Ensure it's treated as unsigned 32-bit
// Convert to [0, 1) range
return this.state / 4294967296; // 2^32
}
}
// Deterministic shuffle by assigning random keys and sorting
function deterministicShuffle(array, seed = 1) {
if (array.length <= 1) {
return [...array]; // Return copy of array if 0 or 1 elements
}
const rng = new XorShift32(seed);
// Create array of objects with original element and random key
const indexedArray = array.map((element, index) => ({
element: element,
randomKey: rng.random(),
// Include original index as tiebreaker for stability (optional)
originalIndex: index
}));
// Sort by random key
indexedArray.sort((a, b) => {
if (a.randomKey !== b.randomKey) {
return a.randomKey - b.randomKey;
}
// If random keys are equal (very unlikely but possible),
// use original index to ensure deterministic behavior
return a.originalIndex - b.originalIndex;
});
// Extract just the elements in their new order
return indexedArray.map(item => item.element);
}
// Example usage and testing:
const originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("Original:", originalArray);
// Shuffle with seed 123
const shuffled1 = deterministicShuffle(originalArray, 123);
console.log("Shuffled with seed 123:", shuffled1);
// Shuffle again with same seed - should get same result
const shuffled2 = deterministicShuffle(originalArray, 123);
console.log("Shuffled with seed 123 again:", shuffled2);
// Shuffle with different seed
const shuffled3 = deterministicShuffle(originalArray, 456);
console.log("Shuffled with seed 456:", shuffled3);
// Verify determinism: same seed produces same output
console.log("Same seed produces same result:", JSON.stringify(shuffled1) === JSON.stringify(shuffled2));
// Test with strings
const stringArray = ["apple", "banana", "cherry", "date", "elderberry"];
console.log("\nOriginal strings:", stringArray);
console.log("Shuffled strings (seed 789):", deterministicShuffle(stringArray, 789));
// Test edge cases
console.log("\nEmpty array:", deterministicShuffle([], 123));
console.log("Single element:", deterministicShuffle([42], 123));
This implementation:
- Uses xorshift32 for deterministic pseudo-random number generation
- Associates a random key with each element in the array
- Sorts by the random keys to achieve the shuffle
- Includes a tiebreaker (original index) to ensure fully deterministic behavior even if two random keys happen to be equal
- Returns a new array without modifying the original
- Is truly deterministic - same seed always produces the same shuffled result
The approach works because:
- Each element gets assigned a random value from the deterministic RNG
- Sorting by these random values effectively randomizes the order
- Since the RNG is seeded, the same sequence of random values is generated each time
- The tiebreaker ensures that even in the extremely rare case of identical random keys, the result is still deterministic
This method has O(n log n) time complexity due to sorting, compared to O(n) for Fisher-Yates, but it's conceptually simple and still deterministic.