Shuffles the given array in-place in random order with Math.random as the default.

Type signature

(xs: any[], random?: () => number) => any[]

Examples

let i = 0;

const random = () =>
  [
    0.013606630487694282,
    0.21052486239086554,
    0.28299838254636556,
    0.696161009199874,
    0.32165320593537117,
  ][i++];

shuffleInPlace(
  [1, 2, 3, 4, 5],
  random
); // => [3, 5, 4, 2, 1]
Try in REPL

Questions

  • How to shuffle an array in place?

TypeScript sourceJavaScript source