Skip to content

Instantly share code, notes, and snippets.

@alishahlakhani
Created August 21, 2024 09:29
Show Gist options
  • Save alishahlakhani/08b4744e7f850709e4811def0f1f61a2 to your computer and use it in GitHub Desktop.
Save alishahlakhani/08b4744e7f850709e4811def0f1f61a2 to your computer and use it in GitHub Desktop.
Algorithms // Generate random number stream // From "Data Structures and Algorithms: From Zero to Hero" on Udemy
// Variable to keep track of old value
const A: number = 22695477;
const B: number = 12820163;
const M: number = Math.pow(2, 24);
let prev: number = 0;
function modGen(val: number, mod: number): number {
return val - Math.trunc(val / mod) * mod;
}
function gen(): number {
return modGen(prev * A + B, M);
}
function genStream(variations: number): void {
for (let index = 0; index < variations; index++) {
if (index === 0) {
console.log(prev);
} else {
const newValue = gen();
if (newValue === prev) {
} else prev = newValue;
console.log(newValue);
}
}
}
genStream(999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment