// Created on savesnippets.com ยท https://savesnippets.com/crg9QFMDJA23H8 export function shuffle(arr: T[]): T[] { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arr[i], arr[j]] = [arr[j]!, arr[i]!]; } return arr; } // Cryptographically secure variant: export function shuffleCrypto(arr: T[]): T[] { const buf = new Uint32Array(1); for (let i = arr.length - 1; i > 0; i--) { crypto.getRandomValues(buf); const j = buf[0]! % (i + 1); [arr[i], arr[j]] = [arr[j]!, arr[i]!]; } return arr; } shuffle([1, 2, 3, 4, 5]); // e.g. [4, 1, 5, 3, 2]