Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class: XORShiftPlus: "non-linear" PRNG
- * Author: k98kurz (@gmail)
- * License: MIT
- * Description: Based upon the xorshift128+ generator, one of the fastest generators passing BigCrush.
- * Methods:
- * next(n) returns Uint32Array of n random numbers
- * nextHex(n) returns Array of n hexidecimal numbers
- * bytes(n) returns Uint8Array of n random bytes
- * bytesHex(n) returns Array of n random bytes in hexidecimal
- * hex(n) returns hexidecimal string of n bytes
- */
- function XORShiftPlus (seed) {
- var x = new Uint32Array(1), y = new Uint32Array(1), z = new Uint32Array(1), w = new Uint32Array(1);
- x[0] = seed ? seed|0 : 317973455;
- y[0] = x[0]<<362436069;
- z[0] = y[0]+x[0];
- w[0] = z[0]^x[0]+y[0];
- function next () {
- var t = new Uint32Array(1);
- t[0] = x[0]^(x<<11);
- x[0] = y[0]; y[0] = z[0]; z[0] = w[0];
- w[0] = w[0]^(w[0]>>19)^(t[0]^(t[0]>>8));
- return w[0]+y[0];
- }
- // discard the first numbers as they are somewhat predictable
- for (var i=0, n=next(), j=next()%256; i<=j; next(), ++i);
- this.next = function (n, option) {
- if (typeof n == 'undefined' || typeof n !== 'number')
- var n = 1;
- var t; n |= 0; n = n>0 ? n : 1;
- if (typeof option == 'undefined' || typeof n !== 'number')
- var option = 0;
- // redundant code, but improved performance
- switch (option) {
- // nextHex
- case 1:
- t = [];
- for (var i=0, il=n; i<il; t[i++] = next().toString(16));
- break;
- // bytes
- case 2:
- t = new Uint8Array(n);
- for (var i=0, il=n; i<il; t[i++] = next());
- break;
- // bytesHex
- case 3:
- t = [];
- 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);
- break;
- // hex
- case 4:
- t = [];
- 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);
- t = t.join('');
- break;
- // next
- default:
- t = new Uint32Array(n);
- for (var i=0, il=n; i<il; t[i++] = next());
- }
- return t;
- };
- this.nextHex = function (n) { return this.next(n, 1); };
- this.bytes = function (n) { return this.next(n, 2); };
- this.bytesHex = function (n) { return this.next(n, 3); };
- this.hex = function (n) { return this.next(n, 4); };
- this.nextMod = function (m, n) {
- var t = this.next(n ? n|0 : 1, 0);
- if (typeof n == "number" && !isNaN(n) && n > 1) {
- m = (typeof m == "number") ? m|0 : Number.MAX_SAFE_INTEGER;
- for (var i=0, il=t.length; i<il; ++i) {
- t[i] = t[i] % m;
- }
- return t;
- }
- return m ? t % (m|0) : t;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment