deno.land / x / froebel@v0.23.2 / shuffle.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/** * Shuffles `list` using the Fisher-Yates shuffle algorithm. * The original `list` is not modified and the shuffled list is returned. */const shuffle = <T>(list: T[]): T[] => { const shuffled = [...list]; shuffleInPlace(shuffled); return shuffled;};
export default shuffle;
/** * Same as {@link shuffle} but shuffles `list` in place. */export const shuffleInPlace = (list: unknown[]): void => { for (let i = list.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); const tmp = list[j]; list[j] = list[i]; list[i] = tmp; }};
froebel

Version Info

Tagged at
a year ago