Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inline lcg #192

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
inline lcg
  • Loading branch information
Fil committed Apr 1, 2022
commit 1f6e4c47f786f66bd1565801a5e3b6c317853df5
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.