Reputation: 12900
I'm making some kind of game with JS. And I want to have an ability to restore game board from previous games. I think I will be able to achieve this behavior if I will have an ability to reproduce sequence of pseudo random numbers. This pseudocode should illustrate my idea:
var seed = 1; // for example
var random1 = initRandom(seed); // I'm looking for this function initRandom
var random2 = initRandom(seed);
console.assert(random1() === random2()); // both random1 and random2 generates pseudo random numbers
console.assert(random1() === random2());
console.assert(random1() === random2());
console.assert(random1() === random2());
console.assert(random1() === random2()); // I will use those number for board generation
Can somebody propose a way to achieve this behavior?
N.B.: random1
and random2
must be time-independent.
Upvotes: 0
Views: 368
Reputation: 1405
The standard psuedo-random generator in JavaScript is not seedable, but you can implement some standard algorithm or use a library such as https://github.com/skeeto/rng-js by Christopher Wellons.
Upvotes: 1