// Takes any integer
Math.seed = function(s) {
var m_w = s;
var m_z = 987654321;
var mask = 0xffffffff;
return function()
// Returns number between 0 (inclusive) and 1.0 (exclusive),
// just like Math.random().
{
m_z = (36969 * (m_z & 65535) + (m_z >> 16)) & mask;
m_w = (18000 * (m_w & 65535) + (m_w >> 16)) & mask;
var result = ((m_z << 16) + m_w) & mask;
result /= 4294967296;
return result + 0.5;
}
}
Another fast and simple PRNG, without magic numbers:Math.seed = function(s) {
return function() {
s = Math.sin(s) * 10000;
return s - Math.floor(s);
};
};
You can replace the default PRNG of JavaScript by, e.g.,
Math.random = Math.seed(Math.round(Math.seed(42)*10000));
Reference
1. http://stackoverflow.com/questions/521295/javascript-random-seeds/5213232. https://github.com/davidbau/seedrandom
No comments:
Post a Comment