mersenne-twister.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
  3. so it's better encapsulated. Now you can have multiple random number generators
  4. and they won't stomp all over eachother's state.
  5. If you want to use this as a substitute for Math.random(), use the random()
  6. method like so:
  7. var m = new MersenneTwister();
  8. var randomNumber = m.random();
  9. You can also call the other genrand_{foo}() methods on the instance.
  10. If you want to use a specific seed in order to get a repeatable random
  11. sequence, pass an integer into the constructor:
  12. var m = new MersenneTwister(123);
  13. and that will always produce the same random sequence.
  14. Sean McCullough (banksean@gmail.com)
  15. */
  16. /*
  17. A C-program for MT19937, with initialization improved 2002/1/26.
  18. Coded by Takuji Nishimura and Makoto Matsumoto.
  19. Before using, initialize the state by using init_genrand(seed)
  20. or init_by_array(init_key, key_length).
  21. */
  22. /**
  23. @license
  24. mersenne-twister.js - https://gist.github.com/banksean/300494
  25. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  26. All rights reserved.
  27. Redistribution and use in source and binary forms, with or without
  28. modification, are permitted provided that the following conditions
  29. are met:
  30. 1. Redistributions of source code must retain the above copyright
  31. notice, this list of conditions and the following disclaimer.
  32. 2. Redistributions in binary form must reproduce the above copyright
  33. notice, this list of conditions and the following disclaimer in the
  34. documentation and/or other materials provided with the distribution.
  35. 3. The names of its contributors may not be used to endorse or promote
  36. products derived from this software without specific prior written
  37. permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  42. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  45. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  46. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  47. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  48. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. */
  50. /*
  51. Any feedback is very welcome.
  52. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  53. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  54. */
  55. define(function() {
  56. var MersenneTwister = function(seed) {
  57. if (seed == undefined) {
  58. seed = new Date().getTime();
  59. }
  60. /* Period parameters */
  61. this.N = 624;
  62. this.M = 397;
  63. this.MATRIX_A = 0x9908b0df; /* constant vector a */
  64. this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
  65. this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
  66. this.mt = new Array(this.N); /* the array for the state vector */
  67. this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */
  68. this.init_genrand(seed);
  69. }
  70. /* initializes mt[N] with a seed */
  71. MersenneTwister.prototype.init_genrand = function(s) {
  72. this.mt[0] = s >>> 0;
  73. for (this.mti=1; this.mti<this.N; this.mti++) {
  74. var s = this.mt[this.mti-1] ^ (this.mt[this.mti-1] >>> 30);
  75. this.mt[this.mti] = (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253)
  76. + this.mti;
  77. /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  78. /* In the previous versions, MSBs of the seed affect */
  79. /* only MSBs of the array mt[]. */
  80. /* 2002/01/09 modified by Makoto Matsumoto */
  81. this.mt[this.mti] >>>= 0;
  82. /* for >32 bit machines */
  83. }
  84. }
  85. /* initialize by an array with array-length */
  86. /* init_key is the array for initializing keys */
  87. /* key_length is its length */
  88. /* slight change for C++, 2004/2/26 */
  89. //MersenneTwister.prototype.init_by_array = function(init_key, key_length) {
  90. // var i, j, k;
  91. // this.init_genrand(19650218);
  92. // i=1; j=0;
  93. // k = (this.N>key_length ? this.N : key_length);
  94. // for (; k; k--) {
  95. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30)
  96. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525)))
  97. // + init_key[j] + j; /* non linear */
  98. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  99. // i++; j++;
  100. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  101. // if (j>=key_length) j=0;
  102. // }
  103. // for (k=this.N-1; k; k--) {
  104. // var s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
  105. // this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941))
  106. // - i; /* non linear */
  107. // this.mt[i] >>>= 0; /* for WORDSIZE > 32 machines */
  108. // i++;
  109. // if (i>=this.N) { this.mt[0] = this.mt[this.N-1]; i=1; }
  110. // }
  111. //
  112. // this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  113. //}
  114. /* generates a random number on [0,0xffffffff]-interval */
  115. MersenneTwister.prototype.genrand_int32 = function() {
  116. var y;
  117. var mag01 = new Array(0x0, this.MATRIX_A);
  118. /* mag01[x] = x * MATRIX_A for x=0,1 */
  119. if (this.mti >= this.N) { /* generate N words at one time */
  120. var kk;
  121. if (this.mti == this.N+1) /* if init_genrand() has not been called, */
  122. this.init_genrand(5489); /* a default initial seed is used */
  123. for (kk=0;kk<this.N-this.M;kk++) {
  124. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  125. this.mt[kk] = this.mt[kk+this.M] ^ (y >>> 1) ^ mag01[y & 0x1];
  126. }
  127. for (;kk<this.N-1;kk++) {
  128. y = (this.mt[kk]&this.UPPER_MASK)|(this.mt[kk+1]&this.LOWER_MASK);
  129. this.mt[kk] = this.mt[kk+(this.M-this.N)] ^ (y >>> 1) ^ mag01[y & 0x1];
  130. }
  131. y = (this.mt[this.N-1]&this.UPPER_MASK)|(this.mt[0]&this.LOWER_MASK);
  132. this.mt[this.N-1] = this.mt[this.M-1] ^ (y >>> 1) ^ mag01[y & 0x1];
  133. this.mti = 0;
  134. }
  135. y = this.mt[this.mti++];
  136. /* Tempering */
  137. y ^= (y >>> 11);
  138. y ^= (y << 7) & 0x9d2c5680;
  139. y ^= (y << 15) & 0xefc60000;
  140. y ^= (y >>> 18);
  141. return y >>> 0;
  142. }
  143. /* generates a random number on [0,0x7fffffff]-interval */
  144. //MersenneTwister.prototype.genrand_int31 = function() {
  145. // return (this.genrand_int32()>>>1);
  146. //}
  147. /* generates a random number on [0,1]-real-interval */
  148. //MersenneTwister.prototype.genrand_real1 = function() {
  149. // return this.genrand_int32()*(1.0/4294967295.0);
  150. // /* divided by 2^32-1 */
  151. //}
  152. /* generates a random number on [0,1)-real-interval */
  153. MersenneTwister.prototype.random = function() {
  154. return this.genrand_int32()*(1.0/4294967296.0);
  155. /* divided by 2^32 */
  156. }
  157. /* generates a random number on (0,1)-real-interval */
  158. //MersenneTwister.prototype.genrand_real3 = function() {
  159. // return (this.genrand_int32() + 0.5)*(1.0/4294967296.0);
  160. // /* divided by 2^32 */
  161. //}
  162. /* generates a random number on [0,1) with 53-bit resolution*/
  163. //MersenneTwister.prototype.genrand_res53 = function() {
  164. // var a=this.genrand_int32()>>>5, b=this.genrand_int32()>>>6;
  165. // return(a*67108864.0+b)*(1.0/9007199254740992.0);
  166. //}
  167. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  168. return MersenneTwister;
  169. });