[{"id":"bf051c9.452f96","type":"tab","label":"Random Number Generators","disabled":false,"info":""},{"id":"3f5cdd9f.a44f52","type":"debug","z":"bf051c9.452f96","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":970,"y":560,"wires":[]},{"id":"4dffce72.2bcd48","type":"inject","z":"bf051c9.452f96","name":"Begin","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"1","x":310,"y":160,"wires":[["986c2654.33466","f79a2199.e417e"]]},{"id":"9f9325d1.023298","type":"function","z":"bf051c9.452f96","name":"Store","func":"/* Push the object instance onto the pool of global variables. */\nflow.set(msg.topic, msg.payload, msg.store);\n","outputs":0,"noerr":0,"x":990,"y":160,"wires":[]},{"id":"986c2654.33466","type":"template","z":"bf051c9.452f96","name":"Class Definition","field":"payload","fieldType":"msg","format":"javascript","syntax":"plain","template":"/* \n Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,\n All rights reserved. \n \n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions\n are met:\n \n 1. Redistributions of source code must retain the above copyright\n notice, this list of conditions and the following disclaimer.\n \n 2. Redistributions in binary form must reproduce the above copyright\n notice, this list of conditions and the following disclaimer in the\n documentation and/or other materials provided with the distribution.\n \n 3. The names of its contributors may not be used to endorse or promote \n products derived from this software without specific prior written \n permission.\n \n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n \n \n Any feedback is very welcome.\n http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html\n email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)\n\n Wrapped by Sean McCullough (banksean@gmail.com) into JavaScript,\n Altered by Wizardry and Steamworks (grimore.org) into a JavaScript class-based variation.\n*/\n\nclass MersenneTwister {\n // class methods\n constructor(seed) {\n if (seed === undefined) {\n seed = new Date().getTime();\n }\n /* Period parameters */\n this.N = 624;\n this.M = 397;\n this.MATRIX_A = 0x9908b0df; /* constant vector a */\n this.UPPER_MASK = 0x80000000; /* most significant w-r bits */\n this.LOWER_MASK = 0x7fffffff; /* least significant r bits */\n\n this.mt = new Array(this.N); /* the array for the state vector */\n this.mti = this.N + 1; /* mti==N+1 means mt[N] is not initialized */\n\n this.init_genrand(seed);\n }\n\n /* initializes mt[N] with a seed */\n init_genrand() {\n this.mt[0] = s >>> 0;\n for (this.mti = 1; this.mti < this.N; this.mti++) {\n var s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30);\n this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)\n + this.mti;\n /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */\n /* In the previous versions, MSBs of the seed affect */\n /* only MSBs of the array mt[]. */\n /* 2002/01/09 modified by Makoto Matsumoto */\n this.mt[this.mti] >>>= 0;\n /* for >32 bit machines */\n }\n }\n\n /* initialize by an array with array-length */\n /* init_key is the array for initializing keys */\n /* key_length is its length */\n /* slight change for C++, 2004/2/26 */\n init_by_array(init_key, key_length) {\n var i, j, k;\n this.init_genrand(19650218);\n i = 1; j = 0;\n k = (this.N > key_length ? this.N : key_length);\n for (; k; k--) {\n var s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30)\n this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525)))\n + init_key[j] + j; /* non linear */\n this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */\n i++; j++;\n if (i >= this.N) { this.mt[0] = this.mt[this.N - 1]; i = 1; }\n if (j >= key_length) j = 0;\n }\n for (k = this.N - 1; k; k--) {\n var s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);\n this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941))\n - i; /* non linear */\n this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */\n i++;\n if (i >= this.N) { this.mt[0] = this.mt[this.N - 1]; i = 1; }\n }\n\n this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */\n }\n\n /* generates a random number on [0,0xffffffff]-interval */\n genrand_int32() {\n var y;\n var mag01 = new Array(0x0, this.MATRIX_A);\n /* mag01[x] = x * MATRIX_A for x=0,1 */\n\n if (this.mti >= this.N) { /* generate N words at one time */\n var kk;\n\n if (this.mti == this.N + 1) /* if init_genrand() has not been called, */\n this.init_genrand(5489); /* a default initial seed is used */\n\n for (kk = 0; kk < this.N - this.M; kk++) {\n y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);\n this.mt[kk] = this.mt[kk + this.M] ^ (y >>> 1) ^ mag01[y & 0x1];\n }\n for (; kk < this.N - 1; kk++) {\n y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);\n this.mt[kk] = this.mt[kk + (this.M - this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];\n }\n y = (this.mt[this.N - 1] & this.UPPER_MASK) | (this.mt[0] & this.LOWER_MASK);\n this.mt[this.N - 1] = this.mt[this.M - 1] ^ (y >>> 1) ^ mag01[y & 0x1];\n\n this.mti = 0;\n }\n\n y = this.mt[this.mti++];\n\n /* Tempering */\n y ^= (y >>> 11);\n y ^= (y << 7) & 0x9d2c5680;\n y ^= (y << 15) & 0xefc60000;\n y ^= (y >>> 18);\n\n return y >>> 0;\n }\n\n /* generates a random number on [0,0x7fffffff]-interval */\n genrand_int31() {\n return (this.genrand_int32() >>> 1);\n }\n\n /* generates a random number on [0,1]-real-interval */\n genrand_real1() {\n return this.genrand_int32() * (1.0 / 4294967295.0);\n /* divided by 2^32-1 */\n }\n\n /* generates a random number on [0,1)-real-interval */\n random() {\n return this.genrand_int32() * (1.0 / 4294967296.0);\n /* divided by 2^32 */\n }\n\n /* generates a random number on (0,1)-real-interval */\n genrand_real3() {\n return (this.genrand_int32() + 0.5) * (1.0 / 4294967296.0);\n /* divided by 2^32 */\n }\n\n /* generates a random number on [0,1) with 53-bit resolution*/\n genrand_res53() {\n var a = this.genrand_int32() >>> 5, b = this.genrand_int32() >>> 6;\n return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);\n }\n}\n","output":"str","x":480,"y":80,"wires":[["e0a73b2e.e3e1d"]]},{"id":"e0a73b2e.e3e1d","type":"function","z":"bf051c9.452f96","name":"Instantiation","func":"/* Retrieve the class definition from the passed message. */\nvar classDefinition = msg.payload;\n\n/* Construct the object from the class definition. */\nvar object = eval(`new ${classDefinition}()`);\n\n/* Return the instantiated object. */\nmsg.topic = object.constructor.name;\nmsg.payload = object;\n\nreturn msg;","outputs":1,"noerr":0,"x":670,"y":160,"wires":[["2575ce58.41a362"]]},{"id":"df62633f.d51c38","type":"function","z":"bf051c9.452f96","name":"Method Invoker","func":"var engine = flow.get(msg.payload, 'randomEngine');\nmsg.payload = engine.random();\nreturn msg;\n","outputs":1,"noerr":0,"x":680,"y":400,"wires":[["64ac43e2.5e73bc","aaae4e9d.091358"]]},{"id":"2575ce58.41a362","type":"change","z":"bf051c9.452f96","name":"Storage","rules":[{"t":"set","p":"store","pt":"msg","to":"randomEngine","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":160,"wires":[["9f9325d1.023298"]]},{"id":"e5fa9a1b.38e4c","type":"link in","z":"bf051c9.452f96","name":"","links":[],"x":435,"y":400,"wires":[["df62633f.d51c38"]]},{"id":"64ac43e2.5e73bc","type":"link out","z":"bf051c9.452f96","name":"","links":[],"x":895,"y":400,"wires":[]},{"id":"f79a2199.e417e","type":"template","z":"bf051c9.452f96","name":"Class Definition","field":"payload","fieldType":"msg","format":"javascript","syntax":"plain","template":"/*\n * Converted from Lehmer Random Number Generator in LOLCode.\n * \n * OBTW Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 TLDR\n * OBTW X(k+1) = g * X(k) mod n\n * I HAS A COUNTER ITZ 1\n * HOW IZ I WAS_MESS YR NUMBER\n * I HAS A THING ITZ MAEK NUMBER A NUMBAR\n * IM IN YR LOOP UPPIN YR ROUNDS WILE DIFFRINT ROUNDS AN NUMBER\n * THING R MOD OF PRODUKT OF 75 AN SUM OF THING AN COUNTER AN 65537\n * COUNTER R SUM OF COUNTER AN 1\n * IM OUTTA YR LOOP\n * FOUND YR MOD OF THING AN NUMBER\n * IF U SAY SO\n *\n */\n \nclass Lehmer {\n constructor(g, n, max) {\n if(g === undefined) {\n this.g = 75;\n }\n \n if (n === undefined) {\n this.n = 65537;\n }\n \n if(max === undefined) {\n this.max = 100;\n }\n \n this.i = 1;\n }\n \n random() {\n this.i = (this.g * (this.max + this.i++)) % this.n;\n return this.i;\n }\n}\n","output":"str","x":480,"y":240,"wires":[["e0a73b2e.e3e1d"]]},{"id":"97dde7ac.c1f06","type":"inject","z":"bf051c9.452f96","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":460,"y":500,"wires":[["df62633f.d51c38"]]},{"id":"aaae4e9d.091358","type":"function","z":"bf051c9.452f96","name":"Normalize [0,1]","func":"const magnitude = Math.ceil(Math.log10(msg.payload + 1));\n\nvar modulo = msg.payload % magnitude;\n\nswitch(Math.abs(parseInt(msg.payload))) {\n case 0:\n msg.payload = modulo;\n break;\n default:\n msg.payload = modulo / magnitude;\n break;\n}\n\nreturn msg;\n","outputs":1,"noerr":0,"x":880,"y":480,"wires":[["3f5cdd9f.a44f52"]]}]