k98kurz

JavaScript XORShiftPlus PRNG (old)

Feb 1st, 2016 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  *  Class: XORShiftPlus: "non-linear" PRNG
  3.  *  Author: k98kurz (@gmail)
  4.  *  License: MIT
  5.  *  Description: Based upon the xorshift128+ generator, one of the fastest generators passing BigCrush.
  6.  *  Methods:
  7.  *      next(n)         returns Uint32Array of n random numbers
  8.  *      nextHex(n)      returns Array of n hexidecimal numbers
  9.  *      bytes(n)        returns Uint8Array of n random bytes
  10.  *      bytesHex(n)     returns Array of n random bytes in hexidecimal
  11.  *      hex(n)          returns hexidecimal string of n bytes
  12.  */
  13. function XORShiftPlus (seed) {
  14.     var x = new Uint32Array(1), y = new Uint32Array(1), z = new Uint32Array(1), w = new Uint32Array(1);
  15.     x[0] = seed ? seed|0 : 317973455;
  16.     y[0] = x[0]<<362436069;
  17.     z[0] = y[0]+x[0];
  18.     w[0] = z[0]^x[0]+y[0];
  19.  
  20.     function next () {
  21.         var t = new Uint32Array(1);
  22.         t[0] = x[0]^(x<<11);
  23.         x[0] = y[0]; y[0] = z[0]; z[0] = w[0];
  24.         w[0] = w[0]^(w[0]>>19)^(t[0]^(t[0]>>8));
  25.         return w[0]+y[0];
  26.     }
  27.  
  28.     // discard the first numbers as they are somewhat predictable
  29.     for (var i=0, n=next(), j=next()%256; i<=j; next(), ++i);
  30.  
  31.     this.next = function (n, option) {
  32.         if (typeof n == 'undefined' || typeof n !== 'number')
  33.             var n = 1;
  34.         var t; n |= 0; n = n>0 ? n : 1;
  35.         if (typeof option == 'undefined' || typeof n !== 'number')
  36.             var option = 0;
  37.  
  38.         // redundant code, but improved performance
  39.         switch (option) {
  40.             // nextHex
  41.             case 1:
  42.                 t = [];
  43.                 for (var i=0, il=n; i<il; t[i++] = next().toString(16));
  44.                 break;
  45.             // bytes
  46.             case 2:
  47.                 t = new Uint8Array(n);
  48.                 for (var i=0, il=n; i<il; t[i++] = next());
  49.                 break;
  50.             // bytesHex
  51.             case 3:
  52.                 t = [];
  53.                 for (var i=0, il=n; i<il; t[i] = (next()%256).toString(16), t[i] = t[i].length%2 ? '0'+t[i] : t[i], ++i);
  54.                 break;
  55.             // hex
  56.             case 4:
  57.                 t = [];
  58.                 for (var i=0, il=n; i<il; t[i] = (next()%256).toString(16), t[i] = t[i].length%2 ? '0'+t[i] : t[i], ++i);
  59.                 t = t.join('');
  60.                 break;
  61.             // next
  62.             default:
  63.                 t = new Uint32Array(n);
  64.                 for (var i=0, il=n; i<il; t[i++] = next());
  65.         }
  66.  
  67.         return t;
  68.     };
  69.  
  70.     this.nextHex = function (n) { return this.next(n, 1); };
  71.  
  72.     this.bytes = function (n) { return this.next(n, 2); };
  73.  
  74.     this.bytesHex = function (n) { return this.next(n, 3); };
  75.  
  76.     this.hex = function (n) { return this.next(n, 4); };
  77.  
  78.     this.nextMod = function (m, n) {
  79.         var t = this.next(n ? n|0 : 1, 0);
  80.         if (typeof n == "number" && !isNaN(n) && n > 1) {
  81.             m = (typeof m == "number") ? m|0 : Number.MAX_SAFE_INTEGER;
  82.             for (var i=0, il=t.length; i<il; ++i) {
  83.                 t[i] = t[i] % m;
  84.             }
  85.             return t;
  86.         }
  87.         return m ? t % (m|0) : t;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment