Skip to content

Instantly share code, notes, and snippets.

@JetLua
Last active February 18, 2019 15:31
Show Gist options
  • Save JetLua/54952e09f29db5cb15f971c38646bfbc to your computer and use it in GitHub Desktop.
Save JetLua/54952e09f29db5cb15f971c38646bfbc to your computer and use it in GitHub Desktop.
let seed = 1
function seededRandom(max = 1, min = 0) {
seed = (seed * 9301 + 49297) % 233280;
let rnd = seed / 233280.0;
return min + rnd * (max - min);
}
for (let i = 0; i < 9; i++) {
console.log(seededRandom())
}
const MT = {
queue: [],
seed: 0,
index: 0,
init(seed=0) {
const queue = this.queue
this.seed = seed
queue[0] = seed
for (let i = 1; i < 623; i++) {
queue[i] = 0x6c078965 * (queue[i - 1] ^ queue[i - 1] >> 30) + i
}
},
twist() {
const queue = this.queue
this.index = 0
for (let item, i = 0; i < queue.length; i++) {
item = queue[i]
item = (item & 0x80000000) + (queue[(i + 1) % 624] & 0x7fffffff)
queue[i] = item ^ queue[(i + 397) % 624] >> 1
item % 2 ? queue[i] ^= 0x9908b0df : null
}
},
random() {
let v = this.queue[this.index]
++this.index > this.queue.length - 1 && this.twist()
v ^= v >> 11
v ^= v << 7 & 0x9d2c5680
v ^= v << 15 & 0xefc60000
v ^= v >> 18
return v
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment