@ohos-ports/stdlib-random-base-minstd 0.0.0-stage → 0.2.3-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/factory.js ADDED
@@ -0,0 +1,416 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ /* eslint-disable max-len */
20
+
21
+ 'use strict';
22
+
23
+ // MODULES //
24
+
25
+ var setReadOnly = require( '@ohos-ports/stdlib-utils-define-nonenumerable-read-only-property' );
26
+ var setReadOnlyAccessor = require( '@ohos-ports/stdlib-utils-define-nonenumerable-read-only-accessor' );
27
+ var setReadWriteAccessor = require( '@ohos-ports/stdlib-utils-define-nonenumerable-read-write-accessor' );
28
+ var hasOwnProp = require( '@ohos-ports/stdlib-assert-has-own-property' );
29
+ var isObject = require( '@ohos-ports/stdlib-assert-is-plain-object' );
30
+ var isBoolean = require( '@ohos-ports/stdlib-assert-is-boolean' ).isPrimitive;
31
+ var isCollection = require( '@ohos-ports/stdlib-assert-is-collection' );
32
+ var isPositiveInteger = require( '@ohos-ports/stdlib-assert-is-positive-integer' ).isPrimitive;
33
+ var isInt32Array = require( '@stdlib/assert-is-int32array' );
34
+ var format = require( '@ohos-ports/stdlib-string-format' );
35
+ var INT32_MAX = require( '@stdlib/constants-int32-max' );
36
+ var Int32Array = require( '@stdlib/array-int32' );
37
+ var gcopy = require( '@ohos-ports/stdlib-blas-base-gcopy' );
38
+ var typedarray2json = require( '@ohos-ports/stdlib-array-to-json' );
39
+ var randint32 = require( './rand_int32.js' );
40
+
41
+
42
+ // VARIABLES //
43
+
44
+ var NORMALIZATION_CONSTANT = (INT32_MAX - 1)|0; // asm type annotation
45
+ var MAX_SEED = (INT32_MAX - 1)|0; // asm type annotation
46
+ var A = 16807|0; // asm type annotation
47
+
48
+ // Define the state array schema version:
49
+ var STATE_ARRAY_VERSION = 1; // NOTE: anytime the state array schema changes, this value should be incremented!!!
50
+
51
+ // Define the number of sections in the state array:
52
+ var NUM_STATE_SECTIONS = 2; // state, seed
53
+
54
+ // Define the index offset of the "state" section in the state array:
55
+ var STATE_SECTION_OFFSET = 2; // | version | num_sections | state_length | ...state | seed_length | ...seed |
56
+
57
+ // Define the index offset of the seed section in the state array:
58
+ var SEED_SECTION_OFFSET = 4; // | version | num_sections | state_length | ...state | seed_length | ...seed |
59
+
60
+ // Define the length of the "fixed" length portion of the state array:
61
+ var STATE_FIXED_LENGTH = 5; // 1 (version) + 1 (num_sections) + 1 (state_length) + 1 (state) + 1 (seed_length)
62
+
63
+
64
+ // FUNCTIONS //
65
+
66
+ /**
67
+ * Verifies state array integrity.
68
+ *
69
+ * @private
70
+ * @param {Int32Array} state - state array
71
+ * @param {boolean} FLG - flag indicating whether the state array was provided as an option (true) or an argument (false)
72
+ * @returns {(Error|null)} an error or `null`
73
+ */
74
+ function verifyState( state, FLG ) {
75
+ var s1;
76
+ if ( FLG ) {
77
+ s1 = 'option';
78
+ } else {
79
+ s1 = 'argument';
80
+ }
81
+ // The state array must have a minimum length...
82
+ if ( state.length < STATE_FIXED_LENGTH+1 ) {
83
+ return new RangeError( format( 'invalid %s. State array has insufficient length.', s1 ) );
84
+ }
85
+ // The first element of the state array must equal the supported state array schema version...
86
+ if ( state[ 0 ] !== STATE_ARRAY_VERSION ) {
87
+ return new RangeError( format( 'invalid %s. State array has an incompatible schema version. Expected: `%s`. Actual: `%s`.', s1, STATE_ARRAY_VERSION, state[ 0 ] ) );
88
+ }
89
+ // The second element of the state array must contain the number of sections...
90
+ if ( state[ 1 ] !== NUM_STATE_SECTIONS ) {
91
+ return new RangeError( format( 'invalid %s. State array has an incompatible number of sections. Expected: `%s`. Actual: `%s`.', s1, NUM_STATE_SECTIONS, state[ 1 ] ) );
92
+ }
93
+ // The length of the "state" section must equal `1`...
94
+ if ( state[ STATE_SECTION_OFFSET ] !== 1 ) {
95
+ return new RangeError( format( 'invalid %s. State array has an incompatible state length. Expected: `%u`. Actual: `%u`.', s1, 1, state[ STATE_SECTION_OFFSET ] ) );
96
+ }
97
+ // The length of the "seed" section much match the empirical length...
98
+ if ( state[ SEED_SECTION_OFFSET ] !== state.length-STATE_FIXED_LENGTH ) {
99
+ return new RangeError( format( 'invalid %s. State array length is incompatible with seed section length. Expected: `%u`. Actual: `%u`.', s1, state.length-STATE_FIXED_LENGTH, state[ SEED_SECTION_OFFSET ] ) );
100
+ }
101
+ return null;
102
+ }
103
+
104
+
105
+ // MAIN //
106
+
107
+ /**
108
+ * Returns a linear congruential pseudorandom number generator (LCG) based on Park and Miller.
109
+ *
110
+ * @param {Options} [options] - options
111
+ * @param {PRNGSeedMINSTD} [options.seed] - pseudorandom number generator seed
112
+ * @param {PRNGStateMINSTD} [options.state] - pseudorandom number generator state
113
+ * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
114
+ * @throws {TypeError} options argument must be an object
115
+ * @throws {TypeError} a seed must be either a positive integer less than the maximum signed 32-bit integer or an array-like object containing integers less than the maximum signed 32-bit integer
116
+ * @throws {RangeError} a numeric seed must be a positive integer less than the maximum signed 32-bit integer
117
+ * @throws {TypeError} state must be an `Int32Array`
118
+ * @throws {Error} must provide a valid state
119
+ * @throws {TypeError} `copy` option must be a boolean
120
+ * @returns {PRNG} LCG PRNG
121
+ *
122
+ * @example
123
+ * var minstd = factory();
124
+ *
125
+ * var v = minstd();
126
+ * // returns <number>
127
+ *
128
+ * @example
129
+ * // Return a seeded LCG:
130
+ * var minstd = factory({
131
+ * 'seed': 1234
132
+ * });
133
+ *
134
+ * var v = minstd();
135
+ * // returns 20739838
136
+ */
137
+ function factory( options ) {
138
+ var STATE;
139
+ var state;
140
+ var opts;
141
+ var seed;
142
+ var slen;
143
+ var err;
144
+
145
+ opts = {};
146
+ if ( arguments.length ) {
147
+ if ( !isObject( options ) ) {
148
+ throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
149
+ }
150
+ if ( hasOwnProp( options, 'copy' ) ) {
151
+ opts.copy = options.copy;
152
+ if ( !isBoolean( options.copy ) ) {
153
+ throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'copy', options.copy ) );
154
+ }
155
+ }
156
+ if ( hasOwnProp( options, 'state' ) ) {
157
+ state = options.state;
158
+ opts.state = true;
159
+ if ( !isInt32Array( state ) ) {
160
+ throw new TypeError( format( 'invalid option. `%s` option must be an Int32Array. Option: `%s`.', 'state', state ) );
161
+ }
162
+ err = verifyState( state, true );
163
+ if ( err ) {
164
+ throw err;
165
+ }
166
+ if ( opts.copy === false ) {
167
+ STATE = state;
168
+ } else {
169
+ STATE = new Int32Array( state.length );
170
+ gcopy( state.length, state, 1, STATE, 1 );
171
+ }
172
+ // Create a state "view":
173
+ state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 );
174
+
175
+ // Create a seed "view":
176
+ seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), state[ SEED_SECTION_OFFSET ] );
177
+ }
178
+ // If provided a PRNG state, we ignore the `seed` option...
179
+ if ( seed === void 0 ) {
180
+ if ( hasOwnProp( options, 'seed' ) ) {
181
+ seed = options.seed;
182
+ opts.seed = true;
183
+ if ( isPositiveInteger( seed ) ) {
184
+ if ( seed > MAX_SEED ) {
185
+ throw new RangeError( format( 'invalid option. `%s` option must be a positive integer less than the maximum signed 32-bit integer. Option: `%u`.', 'seed', seed ) );
186
+ }
187
+ seed |= 0; // asm type annotation
188
+ } else if ( isCollection( seed ) && seed.length > 0 ) {
189
+ slen = seed.length;
190
+ STATE = new Int32Array( STATE_FIXED_LENGTH+slen );
191
+
192
+ // Initialize sections:
193
+ STATE[ 0 ] = STATE_ARRAY_VERSION;
194
+ STATE[ 1 ] = NUM_STATE_SECTIONS;
195
+ STATE[ STATE_SECTION_OFFSET ] = 1;
196
+ STATE[ SEED_SECTION_OFFSET ] = slen;
197
+
198
+ // Copy the provided seed array to prevent external mutation, as mutation would lead to an inability to reproduce PRNG values according to the PRNG's stated seed:
199
+ gcopy.ndarray( slen, seed, 1, 0, STATE, 1, SEED_SECTION_OFFSET+1 );
200
+
201
+ // Create a state "view":
202
+ state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 );
203
+
204
+ // Create a seed "view":
205
+ seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), slen );
206
+
207
+ // Initialize the internal PRNG state:
208
+ state[ 0 ] = seed[ 0 ];
209
+ } else {
210
+ throw new TypeError( format( 'invalid option. `%s` option must be either a positive integer less than the maximum signed 32-bit integer or an array-like object containing integer values less than the maximum signed 32-bit integer. Option: `%s`.', 'seed', seed ) );
211
+ }
212
+ } else {
213
+ seed = randint32()|0; // asm type annotation
214
+ }
215
+ }
216
+ } else {
217
+ seed = randint32()|0; // asm type annotation
218
+ }
219
+ if ( state === void 0 ) {
220
+ STATE = new Int32Array( STATE_FIXED_LENGTH+1 );
221
+
222
+ // Initialize sections:
223
+ STATE[ 0 ] = STATE_ARRAY_VERSION;
224
+ STATE[ 1 ] = NUM_STATE_SECTIONS;
225
+ STATE[ STATE_SECTION_OFFSET ] = 1;
226
+ STATE[ SEED_SECTION_OFFSET ] = 1;
227
+ STATE[ SEED_SECTION_OFFSET+1 ] = seed;
228
+
229
+ // Create a state "view":
230
+ state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 );
231
+
232
+ // Create a seed "view":
233
+ seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 );
234
+
235
+ // Initialize the internal PRNG state:
236
+ state[ 0 ] = seed[ 0 ];
237
+ }
238
+ setReadOnly( minstd, 'NAME', 'minstd' );
239
+ setReadOnlyAccessor( minstd, 'seed', getSeed );
240
+ setReadOnlyAccessor( minstd, 'seedLength', getSeedLength );
241
+ setReadWriteAccessor( minstd, 'state', getState, setState );
242
+ setReadOnlyAccessor( minstd, 'stateLength', getStateLength );
243
+ setReadOnlyAccessor( minstd, 'byteLength', getStateSize );
244
+ setReadOnly( minstd, 'toJSON', toJSON );
245
+ setReadOnly( minstd, 'MIN', 1 );
246
+ setReadOnly( minstd, 'MAX', INT32_MAX-1 );
247
+ setReadOnly( minstd, 'normalized', normalized );
248
+
249
+ setReadOnly( normalized, 'NAME', minstd.NAME );
250
+ setReadOnlyAccessor( normalized, 'seed', getSeed );
251
+ setReadOnlyAccessor( normalized, 'seedLength', getSeedLength );
252
+ setReadWriteAccessor( normalized, 'state', getState, setState );
253
+ setReadOnlyAccessor( normalized, 'stateLength', getStateLength );
254
+ setReadOnlyAccessor( normalized, 'byteLength', getStateSize );
255
+ setReadOnly( normalized, 'toJSON', toJSON );
256
+ setReadOnly( normalized, 'MIN', (minstd.MIN-1.0) / NORMALIZATION_CONSTANT );
257
+ setReadOnly( normalized, 'MAX', (minstd.MAX-1.0) / NORMALIZATION_CONSTANT );
258
+
259
+ return minstd;
260
+
261
+ /**
262
+ * Returns the PRNG seed.
263
+ *
264
+ * @private
265
+ * @returns {PRNGSeedMINSTD} seed
266
+ */
267
+ function getSeed() {
268
+ var len = STATE[ SEED_SECTION_OFFSET ];
269
+ return gcopy( len, seed, 1, new Int32Array( len ), 1 );
270
+ }
271
+
272
+ /**
273
+ * Returns the PRNG seed length.
274
+ *
275
+ * @private
276
+ * @returns {PositiveInteger} seed length
277
+ */
278
+ function getSeedLength() {
279
+ return STATE[ SEED_SECTION_OFFSET ];
280
+ }
281
+
282
+ /**
283
+ * Returns the PRNG state length.
284
+ *
285
+ * @private
286
+ * @returns {PositiveInteger} state length
287
+ */
288
+ function getStateLength() {
289
+ return STATE.length;
290
+ }
291
+
292
+ /**
293
+ * Returns the PRNG state size (in bytes).
294
+ *
295
+ * @private
296
+ * @returns {PositiveInteger} state size (in bytes)
297
+ */
298
+ function getStateSize() {
299
+ return STATE.byteLength;
300
+ }
301
+
302
+ /**
303
+ * Returns the current PRNG state.
304
+ *
305
+ * ## Notes
306
+ *
307
+ * - The PRNG state array is comprised of a preamble followed by `2` sections:
308
+ *
309
+ * 0. preamble (version + number of sections)
310
+ * 1. internal PRNG state
311
+ * 2. PRNG seed
312
+ *
313
+ * - The first element of the PRNG state array preamble is the state array schema version.
314
+ *
315
+ * - The second element of the PRNG state array preamble is the number of state array sections (i.e., `2`).
316
+ *
317
+ * - The first element of each section following the preamble specifies the section length. The remaining section elements comprise the section contents.
318
+ *
319
+ * @private
320
+ * @returns {PRNGStateMINSTD} current state
321
+ */
322
+ function getState() {
323
+ var len = STATE.length;
324
+ return gcopy( len, STATE, 1, new Int32Array( len ), 1 );
325
+ }
326
+
327
+ /**
328
+ * Sets the PRNG state.
329
+ *
330
+ * ## Notes
331
+ *
332
+ * - If PRNG state is "shared" (meaning a state array was provided during PRNG creation and **not** copied) and one sets the generator state to a state array having a different length, the PRNG does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize PRNG output according to the new shared state array, the state array for **each** relevant PRNG must be **explicitly** set.
333
+ * - If PRNG state is "shared" and one sets the generator state to a state array of the same length, the PRNG state is updated (along with the state of all other PRNGs sharing the PRNG's state array).
334
+ *
335
+ * @private
336
+ * @param {PRNGStateMINSTD} s - generator state
337
+ * @throws {TypeError} must provide an `Int32Array`
338
+ * @throws {Error} must provide a valid state
339
+ */
340
+ function setState( s ) {
341
+ var err;
342
+ if ( !isInt32Array( s ) ) {
343
+ throw new TypeError( format( 'invalid argument. Must provide an Int32Array. Value: `%s`.', s ) );
344
+ }
345
+ err = verifyState( s, false );
346
+ if ( err ) {
347
+ throw err;
348
+ }
349
+ if ( opts.copy === false ) {
350
+ if ( opts.state && s.length === STATE.length ) {
351
+ gcopy( s.length, s, 1, STATE, 1 ); // update current shared state
352
+ } else {
353
+ STATE = s; // point to new shared state
354
+ opts.state = true; // setting this flag allows updating a shared state even if a state array was not provided at PRNG creation
355
+ }
356
+ } else {
357
+ // Check if we can reuse allocated memory...
358
+ if ( s.length !== STATE.length ) {
359
+ STATE = new Int32Array( s.length ); // reallocate
360
+ }
361
+ gcopy( s.length, s, 1, STATE, 1 );
362
+ }
363
+ // Create a new state "view":
364
+ state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 );
365
+
366
+ // Create a new seed "view":
367
+ seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), STATE[ SEED_SECTION_OFFSET ] );
368
+ }
369
+
370
+ /**
371
+ * Serializes the pseudorandom number generator as a JSON object.
372
+ *
373
+ * ## Notes
374
+ *
375
+ * - `JSON.stringify()` implicitly calls this method when stringifying a PRNG.
376
+ *
377
+ * @private
378
+ * @returns {Object} JSON representation
379
+ */
380
+ function toJSON() {
381
+ var out = {};
382
+ out.type = 'PRNG';
383
+ out.name = minstd.NAME;
384
+ out.state = typedarray2json( STATE );
385
+ out.params = [];
386
+ return out;
387
+ }
388
+
389
+ /**
390
+ * Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\).
391
+ *
392
+ * @private
393
+ * @returns {integer32} pseudorandom integer
394
+ */
395
+ function minstd() {
396
+ var s = state[ 0 ]|0; // asm type annotation
397
+ s = ( (A*s)%INT32_MAX )|0; // asm type annotation
398
+ state[ 0 ] = s;
399
+ return s|0; // asm type annotation
400
+ }
401
+
402
+ /**
403
+ * Generates a pseudorandom number on the interval \\( [0,1) \\).
404
+ *
405
+ * @private
406
+ * @returns {number} pseudorandom number
407
+ */
408
+ function normalized() {
409
+ return (minstd()-1) / NORMALIZATION_CONSTANT;
410
+ }
411
+ }
412
+
413
+
414
+ // EXPORTS //
415
+
416
+ module.exports = factory;
package/lib/index.js ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ /**
22
+ * A linear congruential pseudorandom number generator (LCG) based on Park and Miller.
23
+ *
24
+ * @module @stdlib/random-base-minstd
25
+ *
26
+ * @example
27
+ * var minstd = require( '@stdlib/random-base-minstd' );
28
+ *
29
+ * var v = minstd();
30
+ * // returns <number>
31
+ *
32
+ * @example
33
+ * var factory = require( '@stdlib/random-base-minstd' ).factory;
34
+ *
35
+ * var minstd = factory({
36
+ * 'seed': 1234
37
+ * });
38
+ *
39
+ * var v = minstd();
40
+ * // returns 20739838
41
+ */
42
+
43
+ // MODULES //
44
+
45
+ var setReadOnly = require( '@ohos-ports/stdlib-utils-define-nonenumerable-read-only-property' );
46
+ var main = require( './main.js' );
47
+ var factory = require( './factory.js' );
48
+
49
+
50
+ // MAIN //
51
+
52
+ setReadOnly( main, 'factory', factory );
53
+
54
+
55
+ // EXPORTS //
56
+
57
+ module.exports = main;
package/lib/main.js ADDED
@@ -0,0 +1,102 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var factory = require( './factory.js' );
24
+ var randint32 = require( './rand_int32.js' );
25
+
26
+
27
+ // MAIN //
28
+
29
+ /**
30
+ * Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\).
31
+ *
32
+ * ## Method
33
+ *
34
+ * Linear congruential generators (LCGs) use the recurrence relation
35
+ *
36
+ * ```tex
37
+ * X_{n+1} = ( a \cdot X_n + c ) \operatorname{mod}(m)
38
+ * ```
39
+ *
40
+ * where the modulus \\( m \\) is a prime number or power of a prime number and \\( a \\) is a primitive root modulo \\( m \\).
41
+ *
42
+ * <!-- <note> -->
43
+ *
44
+ * For an LCG to be a Lehmer RNG, the seed \\( X_0 \\) must be coprime to \\( m \\).
45
+ *
46
+ * <!-- </note> -->
47
+ *
48
+ * In this implementation, the constants \\( a \\), \\( c \\), and \\( m \\) have the values
49
+ *
50
+ * ```tex
51
+ * \begin{align*}
52
+ * a &= 7^5 = 16807 \\
53
+ * c &= 0 \\
54
+ * m &= 2^{31} - 1 = 2147483647
55
+ * \end{align*}
56
+ * ```
57
+ *
58
+ * <!-- <note> -->
59
+ *
60
+ * The constant \\( m \\) is a Mersenne prime (modulo \\(31\\)).
61
+ *
62
+ * <!-- </note> -->
63
+ *
64
+ * <!-- <note> -->
65
+ *
66
+ * The constant \\( a \\) is a primitive root (modulo \\(31\\)).
67
+ *
68
+ * <!-- </note> -->
69
+ *
70
+ * Accordingly, the maximum possible product is
71
+ *
72
+ * ```tex
73
+ * 16807 \cdot (m - 1) \approx 2^{46}
74
+ * ```
75
+ *
76
+ * The values for \\( a \\), \\( c \\), and \\( m \\) are taken from Park and Miller, "Random Number Generators: Good Ones Are Hard To Find". Park's and Miller's article is also the basis for a recipe in the second edition of _Numerical Recipes in C_.
77
+ *
78
+ * ## Notes
79
+ *
80
+ * - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279).
81
+ *
82
+ * ## References
83
+ *
84
+ * - Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042](http://dx.doi.org/10.1145/63039.63042).
85
+ * - Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press.
86
+ *
87
+ * @function minstd
88
+ * @type {PRNG}
89
+ * @returns {PositiveInteger} pseudorandom integer
90
+ *
91
+ * @example
92
+ * var v = minstd();
93
+ * // returns <number>
94
+ */
95
+ var minstd = factory({
96
+ 'seed': randint32()
97
+ });
98
+
99
+
100
+ // EXPORTS //
101
+
102
+ module.exports = minstd;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2018 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var INT32_MAX = require( '@stdlib/constants-int32-max' );
24
+ var floor = require( '@ohos-ports/stdlib-math-base-special-floor' );
25
+
26
+
27
+ // VARIABLES //
28
+
29
+ var MAX = INT32_MAX - 1;
30
+
31
+
32
+ // MAIN //
33
+
34
+ /**
35
+ * Returns a pseudorandom integer on the interval \\([1, 2^{31}-1)\\).
36
+ *
37
+ * @private
38
+ * @returns {PositiveInteger} pseudorandom integer
39
+ *
40
+ * @example
41
+ * var v = randint32();
42
+ * // returns <number>
43
+ */
44
+ function randint32() {
45
+ var v = floor( 1.0 + (MAX*Math.random()) ); // eslint-disable-line stdlib/no-builtin-math
46
+ return v|0; // asm type annotation
47
+ }
48
+
49
+
50
+ // EXPORTS //
51
+
52
+ module.exports = randint32;
package/manifest.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "options": {},
3
+ "fields": [
4
+ {
5
+ "field": "src",
6
+ "resolve": true,
7
+ "relative": true
8
+ },
9
+ {
10
+ "field": "include",
11
+ "resolve": true,
12
+ "relative": true
13
+ },
14
+ {
15
+ "field": "libraries",
16
+ "resolve": false,
17
+ "relative": false
18
+ },
19
+ {
20
+ "field": "libpath",
21
+ "resolve": true,
22
+ "relative": false
23
+ }
24
+ ],
25
+ "confs": [
26
+ {
27
+ "src": [
28
+ "./src/main.c"
29
+ ],
30
+ "include": [
31
+ "./include"
32
+ ],
33
+ "libraries": [],
34
+ "libpath": [],
35
+ "dependencies": [
36
+ "@stdlib/random-base-shared"
37
+ ]
38
+ }
39
+ ]
40
+ }