From 9f3d7b3965803a6e7af383d8267609f8e5923c1b Mon Sep 17 00:00:00 2001 From: Immortal Date: Sun, 3 Mar 2024 20:24:08 +0330 Subject: [PATCH] Update randomizeElements.ts --- src/helpers/randomizeElements.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/helpers/randomizeElements.ts b/src/helpers/randomizeElements.ts index 034aafe..441f355 100644 --- a/src/helpers/randomizeElements.ts +++ b/src/helpers/randomizeElements.ts @@ -1,6 +1,8 @@ -export function randomizeElements(arr: T[]) { - let result = [...arr].sort(() => 0.5 - Math.random() ); - result = result.slice(0, 100); - result = result.sort(() => 0.5 - Math.random() ); - return result; +export function randomizeElements(arr: T[]): T[] { + const result: T[] = [...arr]; + for (let i: number = result.length - 1; i > 0; i--) { + const j: number = Math.floor(Math.random() * (i + 1)); + [result[i], result[j]] = [result[j], result[i]]; + } + return result.slice(0, 100); } \ No newline at end of file