PageSourceSearch

http://zhaojifang.com/libs/home/MersenneTwister.js

js zhaojifang.com collected 2026-10-04 21:49:38 UTC 6,983 bytes, 243 lines download raw bytes

1(function (root, factory) {
2    'use strict';
3
4    if (typeof exports === 'object') {
5        module.exports = factory();
6    } else if (typeof define === 'function' && define.amd) {
7        define(factory);
8    } else {
9        root.MersenneTwister = factory();
10    }
11}(this, function () {
12    /**
13     * A standalone, pure JavaScript implementation of the Mersenne Twister pseudo random number generator. Compatible
14     * with Node.js, requirejs and browser environments. Packages are available for npm, Jam and Bower.
15     *
16     * @module MersenneTwister
17     * @author Raphael Pigulla <[email protected]>
18     * @license See the attached LICENSE file.
19     * @version 0.2.3
20     */
21
22    /*
23     * Most comments were stripped from the source. If needed you can still find them in the original C code:
24     * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
25     *
26     * The original port to JavaScript, on which this file is based, was done by Sean McCullough. It can be found at:
27     * https://gist.github.com/banksean/300494
28     */
29    'use strict';
30
31    var MAX_INT = 4294967296.0,
32        N = 624,
33        M = 397,
34        UPPER_MASK = 0x80000000,
35        LOWER_MASK = 0x7fffffff,
36        MATRIX_A = 0x9908b0df;
37
38    /**
39     * Instantiates a new Mersenne Twister.
40     *
41     * @constructor
42     * @alias module:MersenneTwister
43     * @since 0.1.0
44     * @param {number=} seed The initial seed value.
45     */
46    var MersenneTwister = function (seed) {
47        if (typeof seed === 'undefined') {
48            seed = new Date().getTime();
49        }
50
51        this.mt = new Array(N);
52        this.mti = N + 1;
53
54        this.seed(seed);
55    };
56
57    /**
58     * Initializes the state vector by using one unsigned 32-bit integer "seed", which may be zero.
59     *
60     * @since 0.1.0
61     * @param {number} seed The seed value.
62     */
63    MersenneTwister.prototype.seed = function (seed) {
64        var s;
65
66        this.mt[0] = seed >>> 0;
67
68        for (this.mti = 1; this.mti < N; this.mti++) {
69            s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] >>> 30);
70            this.mt[this.mti] =
71                (((((s & 0xffff0000) >>> 16) * 1812433253) << 16) + (s & 0x0000ffff) * 1812433253) + this.mti;
72            this.mt[this.mti] >>>= 0;
73        }
74    };
75
76    /**
77     * Initializes the state vector by using an array key[] of unsigned 32-bit integers of the specified length. If
78     * length is smaller than 624, then each array of 32-bit integers gives distinct initial state vector. This is
79     * useful if you want a larger seed space than 32-bit word.
80     *
81     * @since 0.1.0
82     * @param {array} vector The seed vector.
83     */
84    MersenneTwister.prototype.seedArray = function (vector) {
85        var i = 1,
86            j = 0,
87            k = N > vector.length ? N : vector.length,
88            s;
89
90        this.seed(19650218);
91
92        for (; k > 0; k--) {
93            s = this.mt[i-1] ^ (this.mt[i-1] >>> 30);
94
95            this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1664525) << 16) + ((s & 0x0000ffff) * 1664525))) +
96                vector[j] + j;
97            this.mt[i] >>>= 0;
98            i++;
99            j++;
100            if (i >= N) {
101                this.mt[0] = this.mt[N - 1];
102                i = 1;
103            }
104            if (j >= vector.length) {
105                j = 0;
106            }
107        }
108
109        for (k = N - 1; k; k--) {
110            s = this.mt[i - 1] ^ (this.mt[i - 1] >>> 30);
111            this.mt[i] =
112                (this.mt[i] ^ (((((s & 0xffff0000) >>> 16) * 1566083941) << 16) + (s & 0x0000ffff) * 1566083941)) - i;
113            this.mt[i] >>>= 0;
114            i++;
115            if (i >= N) {
116                this.mt[0] = this.mt[N - 1];
117                i = 1;
118            }
119        }
120
121        this.mt[0] = 0x80000000;
122    };
123
124    /**
125     * Generates a random unsigned 32-bit integer.
126     *
127     * @since 0.1.0
128     * @returns {number}
129     */
130    MersenneTwister.prototype.int = function () {
131        var y,
132            kk,
133            mag01 = new Array(0, MATRIX_A);
134
135        if (this.mti >= N) {
136            if (this.mti === N + 1) {
137                this.seed(5489);
138            }
139
140            for (kk = 0; kk < N - M; kk++) {
141                y = (this.mt[kk] & UPPER_MASK) | (this.mt[kk + 1] & LOWER_MASK);
142                this.mt[kk] = this.mt[kk + M] ^ (y >>> 1) ^ mag01[y & 1];
143            }
144
145            for (; kk < N - 1; kk++) {
146                y = (this.mt[kk] & UPPER_MASK) | (this.mt[kk + 1] & LOWER_MASK);
147                this.mt[kk] = this.mt[kk + (M - N)] ^ (y >>> 1) ^ mag01[y & 1];
148            }
149
150            y = (this.mt[N - 1] & UPPER_MASK) | (this.mt[0] & LOWER_MASK);
151            this.mt[N - 1] = this.mt[M - 1] ^ (y >>> 1) ^ mag01[y & 1];
152            this.mti = 0;
153        }
154
155        y = this.mt[this.mti++];
156
157        y ^= (y >>> 11);
158        y ^= (y << 7) & 0x9d2c5680;
159        y ^= (y << 15) & 0xefc60000;
160        y ^= (y >>> 18);
161
162        return y >>> 0;
163    };
164
165    /**
166     * Generates a random unsigned 31-bit integer.
167     *
168     * @since 0.1.0
169     * @returns {number}
170     */
171    MersenneTwister.prototype.int31 = function () {
172        return this.int() >>> 1;
173    };
174
175    /**
176     * Generates a random real in the interval [0;1] with 32-bit resolution.
177     *
178     * @since 0.1.0
179     * @returns {number}
180     */
181    MersenneTwister.prototype.real = function () {
182        return this.int() * (1.0 / (MAX_INT - 1));
183    };
184
185    /**
186     * Generates a random real in the interval ]0;1[ with 32-bit resolution.
187     *
188     * @since 0.1.0
189     * @returns {number}
190     */
191    MersenneTwister.prototype.realx = function () {
192        return (this.int() + 0.5) * (1.0 / MAX_INT);
193    };
194
195    /**
196     * Generates a random real in the interval [0;1[ with 32-bit resolution.
197     *
198     * @since 0.1.0
199     * @returns {number}
200     */
201    MersenneTwister.prototype.rnd = function () {
202        return this.int() * (1.0 / MAX_INT);
203    };
204
205    /**
206     * Generates a random real in the interval [0;1[ with 32-bit resolution.
207     *
208     * Same as .rnd() method - for consistency with Math.random() interface.
209     *
210     * @since 0.2.0
211     * @returns {number}
212     */
213    MersenneTwister.prototype.random = MersenneTwister.prototype.rnd;
214
215    /**
216     * Generates a random real in the interval [0;1[ with 53-bit resolution.
217     *
218     * @since 0.1.0
219     * @returns {number}
220     */
221    MersenneTwister.prototype.rndHiRes = function () {
222        var a = this.int() >>> 5,
223            b = this.int() >>> 6;
224
225        return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
226    };
227
228    var instance = new MersenneTwister();
229
230    /**
231     * A static version of [rnd]{@link module:MersenneTwister#rnd} on a randomly seeded instance.
232     *
233     * @static
234     * @function random
235     * @memberof module:MersenneTwister
236     * @returns {number}
237     */
238    MersenneTwister.random = function () {
239        return instance.rnd();
240    };
241
242    return MersenneTwister;
243}));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.