Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Seed(words) {
- switch (typeof words) {
- case 'string':
- var match = words.match(/^(0x)?[0-9A-Fa-f]{1,16}$/);
- if (match.length) {
- var oword = ("0000000000000000" + match[match.length]).slice(-16);
- words = [];
- for (var i = 0; i < 4; ++i) {
- words[i] = parseInt(oword.substr(4 * i, 4), 16);
- }
- } else {
- words = [0, 0, 0, 0];
- }
- break;
- case 'object':
- if (words == null) {
- words = [0, 0, 0, 0];
- break;
- }
- default:
- break;
- }
- this.seed = words;
- this.u32 = (this.seed[0] << 16) + this.seed[1];
- this.l32 = (this.seed[2] << 16) + this.seed[4];
- }
- Seed.prototype.random = function (n) {
- return Math.floor((this.u32 * n) / 0x100000000)
- }
- Seed.prototype.toString = function () {
- return words.map(function (word) {
- return ("0000" + word.toString(16)).slice(-4);
- }).join('').toUpperCase();
- };
- Seed.prototype.nextSeed = function (n) {
- var seed = this.seed;
- n = n || 1;
- for (var frame = 0; frame < n; ++frame) {
- var a = [0x5D58, 0x8B65, 0x6C07, 0x8965];
- var c = [0, 0, 0x26, 0x9EC3];
- var nextSeed = [0, 0, 0, 0];
- var carry = 0;
- for (var cN = seed.length - 1; cN >= 0; --cN) {
- nextSeed[cN] = carry;
- carry = 0;
- var aN = seed.length - 1;
- var seedN = cN;
- for (; seedN < seed.length; --aN, ++seedN) {
- var nextWord = a[aN] * seed[seedN];
- carry += nextWord >>> 16;
- nextSeed[cN] += nextWord & 0xFFFF;
- }
- nextSeed[cN] += c[cN];
- carry += nextSeed[cN] >>> 16;
- nextSeed[cN] &= 0xFFFF;
- }
- seed = nextSeed;
- }
- return new Seed(seed);
- }
- Seed.prototype.advance = function (n) {
- var nextSeed = this.nextSeed(n);
- this.seed = nextSeed.seed;
- this.u32 = nextSeed.u32;
- this.l32 = nextSeed.l32;
- }
Advertisement
Add Comment
Please, Sign In to add comment