KHD

Untitled

KHD
Jul 22nd, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Seed(words) {
  2.     switch (typeof words) {
  3.         case 'string':
  4.             var match = words.match(/^(0x)?[0-9A-Fa-f]{1,16}$/);
  5.             if (match.length) {
  6.                 var oword = ("0000000000000000" + match[match.length]).slice(-16);
  7.                 words = [];
  8.                 for (var i = 0; i < 4; ++i) {
  9.                     words[i] = parseInt(oword.substr(4 * i, 4), 16);
  10.                 }
  11.             } else {
  12.                 words = [0, 0, 0, 0];
  13.             }
  14.             break;
  15.         case 'object':
  16.             if (words == null) {
  17.                 words = [0, 0, 0, 0];
  18.                 break;
  19.             }
  20.         default:
  21.             break;
  22.     }
  23.     this.seed = words;
  24.     this.u32 = (this.seed[0] << 16) + this.seed[1];
  25.     this.l32 = (this.seed[2] << 16) + this.seed[4];
  26. }
  27.  
  28. Seed.prototype.random = function (n) {
  29.     return Math.floor((this.u32 * n) / 0x100000000)
  30. }
  31.  
  32. Seed.prototype.toString = function () {
  33.     return words.map(function (word) {
  34.         return ("0000" + word.toString(16)).slice(-4);
  35.     }).join('').toUpperCase();
  36. };
  37.  
  38. Seed.prototype.nextSeed = function (n) {
  39.     var seed = this.seed;
  40.     n = n || 1;
  41.     for (var frame = 0; frame < n; ++frame) {
  42.  
  43.         var a = [0x5D58, 0x8B65, 0x6C07, 0x8965];
  44.         var c = [0, 0, 0x26, 0x9EC3];
  45.  
  46.         var nextSeed = [0, 0, 0, 0];
  47.         var carry = 0;
  48.  
  49.         for (var cN = seed.length - 1; cN >= 0; --cN) {
  50.             nextSeed[cN] = carry;
  51.             carry = 0;
  52.  
  53.             var aN = seed.length - 1;
  54.             var seedN = cN;
  55.             for (; seedN < seed.length; --aN, ++seedN) {
  56.                 var nextWord = a[aN] * seed[seedN];
  57.                 carry += nextWord >>> 16;
  58.                 nextSeed[cN] += nextWord & 0xFFFF;
  59.             }
  60.             nextSeed[cN] += c[cN];
  61.             carry += nextSeed[cN] >>> 16;
  62.             nextSeed[cN] &= 0xFFFF;
  63.         }
  64.  
  65.         seed = nextSeed;
  66.     }
  67.     return new Seed(seed);
  68. }
  69.  
  70. Seed.prototype.advance = function (n) {
  71.     var nextSeed = this.nextSeed(n);
  72.     this.seed = nextSeed.seed;
  73.     this.u32 = nextSeed.u32;
  74.     this.l32 = nextSeed.l32;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment