Skip to content

Instantly share code, notes, and snippets.

@rasmusmerzin
Last active October 23, 2022 15:46
Show Gist options
  • Save rasmusmerzin/9876f831b0d58b8d6821b883687e4cd6 to your computer and use it in GitHub Desktop.
Save rasmusmerzin/9876f831b0d58b8d6821b883687e4cd6 to your computer and use it in GitHub Desktop.
// nolib ANSI RNG
export const m = 2 ** 31; // just outside of i32
export const a = 1103515245;
export const c = 12345;
// returns integer [0; 2 ** 31 -1]
export function rand(seed: number): () => number {
return () => (seed = (a * seed + c) % m | 0);
}
// returns float [0; 1]
export function randf(seed: number): () => number {
const rint = rand(seed);
return () => rint() / (m - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment