everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
26 lines (25 loc) • 722 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.shuffleStable = void 0;
/**
* Deterministic shuffle based on a seed.
* @author @dailker
* @template T
* @param {T[]} array
* @param {number} [seed=1]
* @returns {T[]}
*/
function shuffleStable(array, seed = 1) {
// Simple LCG for deterministic pseudo-random
function random() {
seed = (seed * 9301 + 49297) % 233280;
return seed / 233280;
}
const arr = array.slice();
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
exports.shuffleStable = shuffleStable;