Skip to content

Commit

Permalink
inline lcg
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Apr 1, 2022
1 parent 9e0ac43 commit 1f6e4c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
12 changes: 8 additions & 4 deletions src/array.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import lcg from "./lcg.js";

export default function(x) {
return typeof x === "object" && "length" in x
? x // Array, TypedArray, NodeList, array-like
: Array.from(x); // Map, Set, iterable, string, or anything else
}

// https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
const a = 1664525;
const c = 1013904223;
const mm = 4294967296; // 2^32

export function shuffle(array) {
const random = lcg();
let s = 1;
let m = array.length,
t,
i;

while (m) {
i = random() * m-- | 0;
s = (a * s + c) % mm;
i = s / mm * m-- | 0;
t = array[m];
array[m] = array[i];
array[i] = t;
Expand Down
9 changes: 0 additions & 9 deletions src/lcg.js

This file was deleted.

0 comments on commit 1f6e4c4

Please sign in to comment.