davidbau / seedrandom

seeded random number generator for Javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to go back steps and repeat them?

danzen opened this issue · comments

Not sure if saving the state is how to do this. I want to be able to go back a number of steps and repeat the random numbers again from there. So:

const r = seedrandom(100000);
console.log(Math.random(), Math.random(), Math.random()); // .78, .55. .13 // shortened ;-)
r.step(-3);
console.log(Math.random(), Math.random(), Math.random()); // .78, .55. .13

commented

If you look at the article's comments, you'll be able to see that "since seedrandom.js uses RC4 which is a cryptographically strong generator, it is designed very difficult to reverse. In other words, even if you know the sequence of generated bits, it is hard to go backwards to figure out the seed, and it is hard to go forward to predict the next random bits."

Further down, someone has stated that you can use the insert a few lines behind the arc4 = new ARC4(key); line:

// remember and restore PRNG state
math['getPrng'] = function(){ return [arc4.i,arc4.j,arc4.S.slice(0)];}; //copies of i,j and S
math['setPrng'] = function(prng){ arc4.i = prng[0]; arc4.j = prng[1]; arc4.S = prng[2].slice(0); };

Hope this helps! (This wasn't made by me: you can see who made it in the comments.)