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

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

Open
danzen opened this issue Jan 19, 2023 · 1 comment
Open

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

danzen opened this issue Jan 19, 2023 · 1 comment

Comments

@danzen
Copy link

danzen commented Jan 19, 2023

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

@plasma4
Copy link

plasma4 commented Jul 10, 2024

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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants