mersenne.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // this program is a JavaScript version of Mersenne Twister,
  2. // a straight conversion from the original program, mt19937ar.c,
  3. // translated by y. okada on july 17, 2006.
  4. // and modified a little at july 20, 2006, but there are not any substantial differences.
  5. // modularized by Ian Bicking, March 25, 2013 (found original version at http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/JAVASCRIPT/java-script.html)
  6. // in this program, procedure descriptions and comments of original source code were not removed.
  7. // lines commented with //c// were originally descriptions of c procedure. and a few following lines are appropriate JavaScript descriptions.
  8. // lines commented with /* and */ are original comments.
  9. // lines commented with // are additional comments in this JavaScript version.
  10. /*
  11. A C-program for MT19937, with initialization improved 2002/1/26.
  12. Coded by Takuji Nishimura and Makoto Matsumoto.
  13. Before using, initialize the state by using init_genrand(seed)
  14. or init_by_array(init_key, key_length).
  15. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  16. All rights reserved.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. 1. Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. 2. Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in the
  24. documentation and/or other materials provided with the distribution.
  25. 3. The names of its contributors may not be used to endorse or promote
  26. products derived from this software without specific prior written
  27. permission.
  28. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  36. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. Any feedback is very welcome.
  40. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  41. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  42. */
  43. (function () {
  44. function RandomStream(seed) {
  45. /*jshint bitwise:false */
  46. /* Period parameters */
  47. //c//#define N 624
  48. //c//#define M 397
  49. //c//#define MATRIX_A 0x9908b0dfUL /* constant vector a */
  50. //c//#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
  51. //c//#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
  52. var N = 624;
  53. var M = 397;
  54. var MATRIX_A = 0x9908b0df; /* constant vector a */
  55. var UPPER_MASK = 0x80000000; /* most significant w-r bits */
  56. var LOWER_MASK = 0x7fffffff; /* least significant r bits */
  57. //c//static unsigned long mt[N]; /* the array for the state vector */
  58. //c//static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
  59. var mt = new Array(N); /* the array for the state vector */
  60. var mti = N+1; /* mti==N+1 means mt[N] is not initialized */
  61. function unsigned32 (n1) // returns a 32-bits unsiged integer from an operand to which applied a bit operator.
  62. {
  63. return n1 < 0 ? (n1 ^ UPPER_MASK) + UPPER_MASK : n1;
  64. }
  65. function subtraction32 (n1, n2) // emulates lowerflow of a c 32-bits unsiged integer variable, instead of the operator -. these both arguments must be non-negative integers expressible using unsigned 32 bits.
  66. {
  67. return n1 < n2 ? unsigned32((0x100000000 - (n2 - n1)) & 0xffffffff) : n1 - n2;
  68. }
  69. function addition32 (n1, n2) // emulates overflow of a c 32-bits unsiged integer variable, instead of the operator +. these both arguments must be non-negative integers expressible using unsigned 32 bits.
  70. {
  71. return unsigned32((n1 + n2) & 0xffffffff);
  72. }
  73. function multiplication32 (n1, n2) // emulates overflow of a c 32-bits unsiged integer variable, instead of the operator *. these both arguments must be non-negative integers expressible using unsigned 32 bits.
  74. {
  75. var sum = 0;
  76. for (var i = 0; i < 32; ++i){
  77. if ((n1 >>> i) & 0x1){
  78. sum = addition32(sum, unsigned32(n2 << i));
  79. }
  80. }
  81. return sum;
  82. }
  83. /* initializes mt[N] with a seed */
  84. //c//void init_genrand(unsigned long s)
  85. function init_genrand(s) {
  86. //c//mt[0]= s & 0xffffffff;
  87. mt[0]= unsigned32(s & 0xffffffff);
  88. for (mti=1; mti<N; mti++) {
  89. mt[mti] =
  90. //c//(1812433253 * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
  91. addition32(multiplication32(1812433253, unsigned32(mt[mti-1] ^ (mt[mti-1] >>> 30))), mti);
  92. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  93. /* In the previous versions, MSBs of the seed affect */
  94. /* only MSBs of the array mt[]. */
  95. /* 2002/01/09 modified by Makoto Matsumoto */
  96. //c//mt[mti] &= 0xffffffff;
  97. mt[mti] = unsigned32(mt[mti] & 0xffffffff);
  98. /* for >32 bit machines */
  99. }
  100. }
  101. /* initialize by an array with array-length */
  102. /* init_key is the array for initializing keys */
  103. /* key_length is its length */
  104. /* slight change for C++, 2004/2/26 */
  105. //c//void init_by_array(unsigned long init_key[], int key_length)
  106. function init_by_array(init_key, key_length) {
  107. //c//int i, j, k;
  108. var i, j, k;
  109. init_genrand(19650218);
  110. i=1; j=0;
  111. k = (N>key_length ? N : key_length);
  112. for (; k; k--) {
  113. //c//mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525))
  114. //c// + init_key[j] + j; /* non linear */
  115. mt[i] = addition32(addition32(unsigned32(mt[i] ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1664525)), init_key[j]), j);
  116. mt[i] =
  117. //c//mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */
  118. unsigned32(mt[i] & 0xffffffff);
  119. i++; j++;
  120. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  121. if (j>=key_length) {
  122. j=0;
  123. }
  124. }
  125. for (k=N-1; k; k--) {
  126. //c//mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941))
  127. //c//- i; /* non linear */
  128. mt[i] = subtraction32(unsigned32((mt[i]) ^ multiplication32(unsigned32(mt[i-1] ^ (mt[i-1] >>> 30)), 1566083941)), i);
  129. //c//mt[i] &= 0xffffffff; /* for WORDSIZE > 32 machines */
  130. mt[i] = unsigned32(mt[i] & 0xffffffff);
  131. i++;
  132. if (i>=N) { mt[0] = mt[N-1]; i=1; }
  133. }
  134. mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  135. }
  136. /* generates a random number on [0,0xffffffff]-interval */
  137. //c//unsigned long genrand_int32(void)
  138. function genrand_int32() {
  139. //c//unsigned long y;
  140. //c//static unsigned long mag01[2]={0x0UL, MATRIX_A};
  141. var y;
  142. var mag01 = [0x0, MATRIX_A];
  143. /* mag01[x] = x * MATRIX_A for x=0,1 */
  144. if (mti >= N) { /* generate N words at one time */
  145. //c//int kk;
  146. var kk;
  147. if (mti == N+1) { /* if init_genrand() has not been called, */
  148. init_genrand(Date.now()); /* a default initial seed is used */
  149. }
  150. for (kk=0;kk<N-M;kk++) {
  151. //c//y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  152. //c//mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
  153. y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK));
  154. mt[kk] = unsigned32(mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]);
  155. }
  156. for (;kk<N-1;kk++) {
  157. //c//y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  158. //c//mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
  159. y = unsigned32((mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK));
  160. mt[kk] = unsigned32(mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]);
  161. }
  162. //c//y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
  163. //c//mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
  164. y = unsigned32((mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK));
  165. mt[N-1] = unsigned32(mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]);
  166. mti = 0;
  167. }
  168. y = mt[mti++];
  169. /* Tempering */
  170. //c//y ^= (y >> 11);
  171. //c//y ^= (y << 7) & 0x9d2c5680;
  172. //c//y ^= (y << 15) & 0xefc60000;
  173. //c//y ^= (y >> 18);
  174. y = unsigned32(y ^ (y >>> 11));
  175. y = unsigned32(y ^ ((y << 7) & 0x9d2c5680));
  176. y = unsigned32(y ^ ((y << 15) & 0xefc60000));
  177. y = unsigned32(y ^ (y >>> 18));
  178. return y;
  179. }
  180. /* generates a random number on [0,0x7fffffff]-interval */
  181. //c//long genrand_int31(void)
  182. function genrand_int31() {
  183. //c//return (genrand_int32()>>1);
  184. return (genrand_int32()>>>1);
  185. }
  186. /* generates a random number on [0,1]-real-interval */
  187. //c//double genrand_real1(void)
  188. function genrand_real1() {
  189. return genrand_int32()*(1.0/4294967295.0);
  190. /* divided by 2^32-1 */
  191. }
  192. /* generates a random number on [0,1)-real-interval */
  193. //c//double genrand_real2(void)
  194. function genrand_real2() {
  195. return genrand_int32()*(1.0/4294967296.0);
  196. /* divided by 2^32 */
  197. }
  198. /* generates a random number on (0,1)-real-interval */
  199. //c//double genrand_real3(void)
  200. function genrand_real3() {
  201. return ((genrand_int32()) + 0.5)*(1.0/4294967296.0);
  202. /* divided by 2^32 */
  203. }
  204. /* generates a random number on [0,1) with 53-bit resolution*/
  205. //c//double genrand_res53(void)
  206. function genrand_res53() {
  207. //c//unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
  208. var a=genrand_int32()>>>5, b=genrand_int32()>>>6;
  209. return (a*67108864.0+b)*(1.0/9007199254740992.0);
  210. }
  211. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  212. var random = genrand_real2;
  213. random.seed = function (seed) {
  214. if (! seed) {
  215. seed = Date.now();
  216. }
  217. if (typeof seed != "number") {
  218. seed = parseInt(seed, 10);
  219. }
  220. if ((seed !== 0 && ! seed) || isNaN(seed)) {
  221. throw "Bad seed";
  222. }
  223. init_genrand(seed);
  224. };
  225. random.seed(seed);
  226. random.int31 = genrand_int31;
  227. random.real1 = genrand_real1;
  228. random.real2 = genrand_real2;
  229. random.real3 = genrand_real3;
  230. random.res53 = genrand_res53;
  231. return random;
  232. }
  233. // Support for AMD/requirejs, CommonJS/Node, and no module system:
  234. if (typeof define != "undefined") {
  235. define([], function () {return RandomStream;});
  236. } else if (typeof module != "undefined") {
  237. module.exports = RandomStream;
  238. } else {
  239. window.MersenneRandomStream = RandomStream;
  240. }
  241. })();