mersenne-twister.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. /*
  3. https://github.com/banksean wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
  4. so it's better encapsulated. Now you can have multiple random number generators
  5. and they won't stomp all over eachother's state.
  6. If you want to use this as a substitute for Math.random(), use the random()
  7. method like so:
  8. var m = new MersenneTwister();
  9. var randomNumber = m.random();
  10. You can also call the other genrand_{foo}() methods on the instance.
  11. If you want to use a specific seed in order to get a repeatable random
  12. sequence, pass an integer into the constructor:
  13. var m = new MersenneTwister(123);
  14. and that will always produce the same random sequence.
  15. Sean McCullough (banksean@gmail.com)
  16. */
  17. /*
  18. A C-program for MT19937, with initialization improved 2002/1/26.
  19. Coded by Takuji Nishimura and Makoto Matsumoto.
  20. Before using, initialize the state by using init_seed(seed)
  21. or init_by_array(init_key, key_length).
  22. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  23. All rights reserved.
  24. Redistribution and use in source and binary forms, with or without
  25. modification, are permitted provided that the following conditions
  26. are met:
  27. 1. Redistributions of source code must retain the above copyright
  28. notice, this list of conditions and the following disclaimer.
  29. 2. Redistributions in binary form must reproduce the above copyright
  30. notice, this list of conditions and the following disclaimer in the
  31. documentation and/or other materials provided with the distribution.
  32. 3. The names of its contributors may not be used to endorse or promote
  33. products derived from this software without specific prior written
  34. permission.
  35. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  39. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  40. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  41. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  42. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  45. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. Any feedback is very welcome.
  47. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  48. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
  49. */
  50. var MersenneTwister = function(seed) {
  51. if (seed == undefined) {
  52. seed = new Date().getTime();
  53. }
  54. /* Period parameters */
  55. this.N = 624;
  56. this.M = 397;
  57. this.MATRIX_A = 0x9908b0df; /* constant vector a */
  58. this.UPPER_MASK = 0x80000000; /* most significant w-r bits */
  59. this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
  60. this.mt = new Array(this.N); /* the array for the state vector */
  61. this.mti=this.N+1; /* mti==N+1 means mt[N] is not initialized */
  62. if (seed.constructor == Array) {
  63. this.init_by_array(seed, seed.length);
  64. }
  65. else {
  66. this.init_seed(seed);
  67. }
  68. };
  69. /* initializes mt[N] with a seed */
  70. /* origin name init_genrand */
  71. MersenneTwister.prototype.init_seed = 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_seed(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. this.mt[0] = 0x80000000; /* MSB is 1; assuring non-zero initial array */
  112. };
  113. /* generates a random number on [0,0xffffffff]-interval */
  114. /* origin name genrand_int32 */
  115. MersenneTwister.prototype.random_int = 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_seed() has not been called, */
  122. this.init_seed(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. /* origin name genrand_int31 */
  145. MersenneTwister.prototype.random_int31 = function() {
  146. return (this.random_int()>>>1);
  147. };
  148. /* generates a random number on [0,1]-real-interval */
  149. /* origin name genrand_real1 */
  150. MersenneTwister.prototype.random_incl = function() {
  151. return this.random_int()*(1.0/4294967295.0);
  152. /* divided by 2^32-1 */
  153. };
  154. /* generates a random number on [0,1)-real-interval */
  155. MersenneTwister.prototype.random = function() {
  156. return this.random_int()*(1.0/4294967296.0);
  157. /* divided by 2^32 */
  158. };
  159. /* generates a random number on (0,1)-real-interval */
  160. /* origin name genrand_real3 */
  161. MersenneTwister.prototype.random_excl = function() {
  162. return (this.random_int() + 0.5)*(1.0/4294967296.0);
  163. /* divided by 2^32 */
  164. };
  165. /* generates a random number on [0,1) with 53-bit resolution*/
  166. /* origin name genrand_res53 */
  167. MersenneTwister.prototype.random_long = function() {
  168. var a=this.random_int()>>>5, b=this.random_int()>>>6;
  169. return (a*67108864.0+b)*(1.0/9007199254740992.0);
  170. };
  171. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  172. var mersenneTwister = MersenneTwister;
  173. export { mersenneTwister as default };