aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/utils/shuffle.ts
blob: a2b8fecd745e5d4e8b2207cd4e7c1f702db64528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * Fisher-Yates shuffle algorithm.
 * Returns a new shuffled array (does not mutate the original).
 */
export function shuffle<T>(array: T[]): T[] {
	const result = [...array];
	for (let i = result.length - 1; i > 0; i--) {
		const j = Math.floor(Math.random() * (i + 1));
		const temp = result[i] as T;
		result[i] = result[j] as T;
		result[j] = temp;
	}
	return result;
}