diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/LICENSE b/lib/node_modules/@stdlib/random/base/tinymt32/LICENSE new file mode 100644 index 000000000000..c644840408e4 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/LICENSE @@ -0,0 +1,215 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + +DEPENDENCIES & ATTRIBUTION + +The library links against the following external libraries or contains +implementations from the following external libraries, which have their own +licenses: + +* TinyMT32 + +Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/README.md b/lib/node_modules/@stdlib/random/base/tinymt32/README.md new file mode 100644 index 000000000000..622473dca3a6 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/README.md @@ -0,0 +1,309 @@ + + +# TinyMT32 + +> A 32-bit [Tiny Mersenne Twister][tiny-mersenne-twister] pseudorandom number generator. + +
+ +## Usage + +```javascript +var tinymt32 = require( '@stdlib/random/base/tinymt32' ); +``` + +#### tinymt32() + +Returns a pseudorandom integer on the interval `[0, 4294967295]`. + +```javascript +var r = tinymt32(); +// returns +``` + +#### tinymt32.normalized() + +Returns a normalized pseudorandom number on the interval `[0,1)` with 53-bit precision. + +```javascript +var r = tinymt32.normalized(); +// returns +``` + +#### tinymt32.factory( \[options] ) + +Returns a 32-bit [Tiny Mersenne Twister][tiny-mersenne-twister] pseudorandom number generator. + +```javascript +var rand = tinymt32.factory(); +``` + +The function accepts the following `options`: + +- **seed**: pseudorandom number generator seed. +- **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. +- **copy**: boolean indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that a returned generator has exclusive control over its internal state. Default: `true`. + +By default, a random integer is used to seed the returned generator. To seed the generator, provide either a nonzero integer on the interval `[1, 4294967295]` + +```javascript +var rand = tinymt32.factory({ + 'seed': 1234 +}); + +var r = rand(); +// returns 2682965004 +``` + +or, for arbitrary length seeds, an array-like object containing unsigned 32-bit integers + +```javascript +var Uint32Array = require( '@stdlib/array/uint32' ); + +var rand = tinymt32.factory({ + 'seed': new Uint32Array( [ 291, 564, 837, 1110 ] ) +}); + +var r = rand(); +// returns 3211122912 +``` + +To return a generator having a specific initial state, set the generator `state` option. + +```javascript +// Generate pseudorandom numbers, thus progressing the generator state: +var r; +var i; +for ( i = 0; i < 1000; i++ ) { + r = tinymt32(); +} + +// Create a new TinyMT32 PRNG initialized to the current state of `tinymt32`: +var rand = tinymt32.factory({ + 'state': tinymt32.state +}); + +// Test that the generated pseudorandom numbers are the same: +var bool = ( rand() === tinymt32() ); +// returns true +``` + +#### tinymt32.NAME + +The generator name. + +```javascript +var str = tinymt32.NAME; +// returns 'tinymt32' +``` + +#### tinymt32.MIN + +Minimum possible value. + +```javascript +var min = tinymt32.MIN; +// returns 0 +``` + +#### tinymt32.MAX + +Maximum possible value. + +```javascript +var max = tinymt32.MAX; +// returns 4294967295 +``` + +#### tinymt32.seed + +The value used to seed `tinymt32()`. + +```javascript +// Generate pseudorandom values... +var r; +var i; +for ( i = 0; i < 100; i++ ) { + r = tinymt32(); +} + +// Generate the same pseudorandom values... +var rand = tinymt32.factory({ + 'seed': tinymt32.seed +}); +for ( i = 0; i < 100; i++ ) { + r = rand(); +} +``` + +#### tinymt32.seedLength + +Length of generator seed. + +```javascript +var len = tinymt32.seedLength; +// returns +``` + +#### tinymt32.state + +Writable property for getting and setting the generator state. + +```javascript +var r = tinymt32(); +// returns + +r = tinymt32(); +// returns + +// ... + +// Get a copy of the current state: +var state = tinymt32.state; +// returns + +r = tinymt32(); +// returns + +r = tinymt32(); +// returns + +// Reset the state: +tinymt32.state = state; + +// Replay the last two pseudorandom numbers: +r = tinymt32(); +// returns + +r = tinymt32(); +// returns + +// ... +``` + +#### tinymt32.stateLength + +Length of generator state. + +```javascript +var len = tinymt32.stateLength; +// returns 4 +``` + +#### tinymt32.byteLength + +Size (in bytes) of generator state. + +```javascript +var sz = tinymt32.byteLength; +// returns +``` + +#### tinymt32.toJSON() + +Serializes the pseudorandom number generator as a JSON object. + +```javascript +var o = tinymt32.toJSON(); +// returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } +``` + +
+ + + +
+ +## Notes + +- [TinyMT32][tiny-mersenne-twister] is **not** a cryptographically secure PRNG, as the PRNG is based on a linear recursion. Any pseudorandom number sequence generated by a linear recursion is **insecure**, due to the fact that one can predict future generated outputs by observing a sufficiently long subsequence of generated values. +- Compared to other PRNGs, [TinyMT32][tiny-mersenne-twister] has a small state size (`16 bytes`), making it suitable for memory-constrained environments. However, this comes at the cost of a shorter period length compared to the original Mersenne Twister. +- A seed array of length `1` is considered **equivalent** to an integer seed equal to the lone seed array element and vice versa. +- 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. +- 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). +- The PRNG has a period of `2^127 - 1`. + +
+ + + +
+ +## Examples + + + +```javascript +var tinymt32 = require( '@stdlib/random/base/tinymt32' ); + +// Generate pseudorandom numbers: +var r; +var i; +for ( i = 0; i < 100; i++ ) { + r = tinymt32(); + console.log( r ); +} + +// Create a new TinyMT32 PRNG initialized to the current state of `tinymt32`: +var rand = tinymt32.factory({ + 'state': tinymt32.state +}); + +// Test that the generated pseudorandom numbers are the same: +var bool = ( rand() === tinymt32() ); +console.log( bool ); +``` + +
+ + + +
+ +## References + +- Saito, Makoto, and Mutsuo Saito. 2008. "SIMD-Oriented Fast Mersenne Twister: A 128-bit Pseudorandom Number Generator." In _Monte Carlo and Quasi-Monte Carlo Methods 2006_, edited by Alexander Keller, Stefan Heinrich, and Harald Niederreiter, 607–22. Berlin, Heidelberg: Springer Berlin Heidelberg. doi:[10.1007/978-3-540-74496-2_36][saito-2008]. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.factory.js b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.factory.js new file mode 100644 index 000000000000..60e8961d0696 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.factory.js @@ -0,0 +1,105 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var minstd = require( '@stdlib/random/base/minstd-shuffle' ); +var pkg = require( './../package.json' ).name; +var factory = require( './../lib' ).factory; + + +// MAIN // + +bench( pkg+':factory', function benchmark( b ) { + var f; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + f = factory(); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( isnan( f() ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':factory,seed=', function benchmark( b ) { + var opts; + var f; + var i; + + opts = { + 'seed': 1 + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + opts.seed += 1; + f = factory(opts); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( isnan( f() ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':factory:seed=', function benchmark( b ) { + var seed; + var opts; + var f; + var i; + + opts = {}; + + seed = new Uint32Array( 2 ); + for ( i = 0; i < seed.length; i++ ) { + seed[ i ] = minstd(); + } + opts.seed = seed; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + opts.seed[ 0 ] = i + 1; + f = factory( opts ); + if ( typeof f !== 'function' ) { + b.fail( 'should return a function' ); + } + } + b.toc(); + if ( isnan( f() ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.js b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.js new file mode 100644 index 000000000000..4212390f87c9 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pkg = require( './../package.json' ).name; +var tinymt32 = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = tinymt32(); + if ( isnan( z ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':normalized', function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = tinymt32.normalized(); + if ( isnan( z ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( z ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.to_json.js b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.to_json.js new file mode 100644 index 000000000000..2436a7cbbf30 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/benchmark.to_json.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var pkg = require( './../package.json' ).name; +var tinymt32 = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toJSON', function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = tinymt32.toJSON(); + if ( typeof o !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isObject( o ) ) { + b.fail( 'should return an object' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/c/Makefile b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/c/Makefile new file mode 100644 index 000000000000..5d7e79f50788 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles C source files. +# +# @param {string} SOURCE_FILES - list of C source files +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [C_COMPILER] - C compiler +# @param {string} [CFLAGS] - C compiler flags +# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} SOURCE_FILES - list of C source files +# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) +# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} CC - C compiler +# @param {string} CFLAGS - C compiler flags +# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/c/benchmark.c new file mode 100644 index 000000000000..a39f7e6970d3 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/benchmark/c/benchmark.c @@ -0,0 +1,401 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/random/base/tinymt32.h" +#include "stdlib/random/base/shared.h" +#include +#include +#include +#include +#include +#include + +#define NAME "base/tinymt32" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static double rand_double( void ) { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark1( void ) { + uint32_t seed[1]; + double elapsed; + double t; + int i; + + struct BasePRNGObject *obj; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + seed[ 0 ] = i + 1; + obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); + if ( obj == NULL || obj->prng->min != 1 ) { + printf( "unexpected result\n" ); + break; + } + if ( i < ITERATIONS-1 ) { + stdlib_base_random_tinymt32_free( obj ); + } + } + elapsed = tic() - t; + + if ( obj == NULL || obj->prng->min != 1 ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_tinymt32_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark2( void ) { + uint32_t seed[1]; + double elapsed; + int8_t status; + uint64_t max; + uint64_t v; + double t; + int i; + + seed[ 0 ] = 12345; + + struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + max = obj->prng->max; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + status = obj->prng->next( obj, &v ); + if ( status != 0 || v > max ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + + if ( status != 0 || v > max ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_tinymt32_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark3( void ) { + uint32_t seed[1]; + double elapsed; + int8_t status; + double v; + double t; + int i; + + seed[ 0 ] = 12345; + + struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + status = obj->prng->normalized( obj, &v ); + if ( status != 0 || v != v ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + + if ( status != 0 || v != v ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_tinymt32_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark4( void ) { + uint32_t seed[1]; + double elapsed; + int8_t status; + int64_t len; + double t; + int i; + + seed[ 0 ] = 12345; + + struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + status = stdlib_base_random_tinymt32_seed_length( obj, &len ); + if ( status != 0 || len != 1 ) { + printf( "unexpected result\n" ); + } + status = stdlib_base_random_tinymt32_seed( obj, seed ); + if ( status != 0 || seed[ 0 ] != 12345 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + + if ( status != 0 || len != 1 || seed[ 0 ] != 12345 ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_tinymt32_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark5( void ) { + stdlib_base_random_tinymt32_state_t *state; + uint32_t seed[1]; + double elapsed; + double t; + int i; + + seed[ 0 ] = 12345; + + struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + state = stdlib_base_random_tinymt32_state( obj ); + if ( state == NULL ) { + printf( "unexpected result\n" ); + break; + } + if ( i < ITERATIONS-1 ) { + free( state ); + } + } + elapsed = tic() - t; + + stdlib_base_random_tinymt32_free( obj ); + if ( state == NULL ) { + printf( "unexpected result\n" ); + } else { + free( state->seed ); + free( state ); + } + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark6( void ) { + stdlib_base_random_tinymt32_state_t *states[2]; + uint32_t seed[1]; + double elapsed; + int8_t status; + double t; + int i; + + seed[ 0 ] = 12345; + + struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + for ( i = 0; i < 2; i++ ) { + states[i] = (stdlib_base_random_tinymt32_state_t *)malloc( sizeof( stdlib_base_random_tinymt32_state_t ) ); + if ( states[i] == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + states[i]->seed = (uint32_t *)malloc( sizeof( uint32_t )*1 ); + states[i]->seed[ 0 ] = 12345; + states[i]->seed_length = 1; + states[i]->state[ 0 ] = 1; + states[i]->state[ 1 ] = 2; + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + status = stdlib_base_random_tinymt32_set( obj, (void *)states[i%2] ); + if ( status != 0 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + + if ( status != 0 ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_tinymt32_free( obj ); + for ( i = 0; i < 2; i++ ) { + free( states[i] ); + } + + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int count; + int i; + + count = 0; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + count += 1; + printf( "# c::%s::instantiation\n", NAME ); + elapsed = benchmark1(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + for ( i = 0; i < REPEATS; i++ ) { + count += 1; + printf( "# c::%s:next\n", NAME ); + elapsed = benchmark2(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + for ( i = 0; i < REPEATS; i++ ) { + count += 1; + printf( "# c::%s:normalized\n", NAME ); + elapsed = benchmark3(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + for ( i = 0; i < REPEATS; i++ ) { + count += 1; + printf( "# c::%s::get:seed\n", NAME ); + elapsed = benchmark4(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + for ( i = 0; i < REPEATS; i++ ) { + count += 1; + printf( "# c::%s::get:state\n", NAME ); + elapsed = benchmark5(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + for ( i = 0; i < REPEATS; i++ ) { + count += 1; + printf( "# c::%s::set:state\n", NAME ); + elapsed = benchmark6(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", count ); + } + print_summary( count, count ); +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/docs/repl.txt b/lib/node_modules/@stdlib/random/base/tinymt32/docs/repl.txt new file mode 100644 index 000000000000..c972b10b0b80 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/docs/repl.txt @@ -0,0 +1,181 @@ + +{{alias}}() + Returns a pseudorandom integer on the interval `[0, 4294967295]`. + + This pseudorandom number generator (PRNG) is a 32-bit Tiny Mersenne Twister + pseudorandom number generator. + + The PRNG is *not* a cryptographically secure PRNG. + + The PRNG has a period of 2^127 - 1. + + Returns + ------- + r: integer + Pseudorandom number. + + Examples + -------- + > var r = {{alias}}(); + + +{{alias}}.normalized() + Returns a pseudorandom number on the interval `[0,1)` with 53-bit precision. + + Returns + ------- + r: number + Pseudorandom number. + + Examples + -------- + > var r = {{alias}}.normalized(); + + +{{alias}}.factory( [options] ) + Returns a 32-bit Mersenne Twister pseudorandom number generator. + + Parameters + ---------- + options: Object (optional) + Options. + + options.seed: integer|ArrayLikeObject (optional) + Pseudorandom number generator seed. The seed may be either a positive + unsigned 32-bit integer or, for arbitrary length seeds, an array-like + object containing unsigned 32-bit integers. + + options.state: Uint32Array (optional) + Pseudorandom number generator state. If provided, the `seed` option is + ignored. + + options.copy: boolean (optional) + Boolean indicating whether to copy a provided pseudorandom number + generator state. Setting this option to `false` allows sharing state + between two or more pseudorandom number generators. Setting this option + to `true` ensures that a returned generator has exclusive control over + its internal state. Default: true. + + Returns + ------- + rand: Function + Pseudorandom number generator (PRNG). + + Examples + -------- + // Basic usage: + > var rand = {{alias}}.factory(); + > r = rand(); + > r = rand(); + + // Provide a seed: + > rand = {{alias}}.factory( { 'seed': 1234 } ); + > r = rand() + 2682965004 + + +{{alias}}.NAME + Generator name. + + Examples + -------- + > var str = {{alias}}.NAME + 'tinymt32' + + +{{alias}}.MIN + Minimum possible value. + + Examples + -------- + > var v = {{alias}}.MIN + 0 + + +{{alias}}.MAX + Maximum possible value. + + Examples + -------- + > var v = {{alias}}.MAX + 4294967295 + + +{{alias}}.seed + Pseudorandom number generator seed. + + Examples + -------- + > var seed = {{alias}}.seed; + + +{{alias}}.seedLength + Length of generator seed. + + Examples + -------- + > var len = {{alias}}.seedLength; + + +{{alias}}.state + Generator state. + + Examples + -------- + > var r = {{alias}}() + + > r = {{alias}}() + + > r = {{alias}}() + + + // Get a copy of the current state: + > var state = {{alias}}.state + + + > r = {{alias}}() + + > r = {{alias}}() + + + // Set the state: + > {{alias}}.state = state; + + // Replay the last two pseudorandom numbers: + > r = {{alias}}() + + > r = {{alias}}() + + + +{{alias}}.stateLength + Length of generator state. + + Examples + -------- + > var len = {{alias}}.stateLength; + + +{{alias}}.byteLength + Size (in bytes) of generator state. + + Examples + -------- + > var sz = {{alias}}.byteLength; + + +{{alias}}.toJSON() + Serializes the pseudorandom number generator as a JSON object. + + Returns + ------- + out: Object + JSON representation. + + Examples + -------- + > var o = {{alias}}.toJSON() + { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } + + See Also + -------- diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/docs/types/index.d.ts b/lib/node_modules/@stdlib/random/base/tinymt32/docs/types/index.d.ts new file mode 100644 index 000000000000..7ed8d5d116c6 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/docs/types/index.d.ts @@ -0,0 +1,182 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import * as random from '@stdlib/types/random'; + +/** +* Interface defining `factory` options. +*/ +interface Options { + /** + * Pseudorandom number generator seed. + */ + seed?: random.PRNGSeedTinyMT32; + + /** + * Pseudorandom number generator state. + */ + state?: random.PRNGStateTinyMT32; + + /** + * Specifies whether to copy a provided pseudorandom number generator state. + */ + copy?: boolean; +} + +/** +* Interface for PRNG properties and methods. +*/ +interface PRNG { + /** + * Generator name. + */ + readonly NAME: string; + + /** + * Minimum possible value. + */ + readonly MIN: number; + + /** + * Maximum possible value. + */ + readonly MAX: number; + + /** + * PRNG seed. + */ + readonly seed: random.PRNGSeedTinyMT32; + + /** + * PRNG seed length. + */ + readonly seedLength: number; + + /** + * PRNG state. + */ + state: random.PRNGSeedTinyMT32; + + /** + * PRNG state length. + */ + readonly stateLength: number; + + /** + * PRNG state size (in bytes). + */ + readonly byteLength: number; + + /** + * Serializes the pseudorandom number generator as a JSON object. + * + * @returns JSON representation + */ + toJSON(): string; +} + +/** +* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`. +*/ +interface NullaryFunction extends PRNG { + /** + * Returns a pseudorandom integer on the interval `[0, 4294967295]`. + * + * @returns pseudorandom number + */ + (): number; +} + +/** +* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`. +*/ +interface Random extends PRNG { + /** + * Returns a pseudorandom integer on the interval `[0, 4294967295]`. + * + * ## Notes + * + * - This pseudorandom number generator (PRNG) is a 32-bit Tiny Mersenne Twister pseudorandom number generator. + * - The PRNG is *not* a cryptographically secure PRNG. + * - The PRNG has a period of 2^127 - 1. + * + * @returns pseudorandom number + * + * @example + * var v = tinymt32(); + * // returns + */ + (): number; + + /** + * Returns a 32-bit Tiny Mersenne Twister pseudorandom number generator. + * + * @param options - function options + * @param options.seed - pseudorandom number generator seed + * @param options.state - pseudorandom number generator state + * @param options.copy - boolean indicating whether to copy a provided pseudorandom number generator state (default: true) + * @throws must provide valid options + * @returns pseudorandom number generator + * + * @example + * var rand = tinymt32.factory(); + * var v = rand(); + * // returns + * + * @example + * var rand = tinymt32.factory({ + * 'seed': 12345 + * }); + * var v = rand(); + * // returns + */ + factory( options?: Options ): NullaryFunction; +} + +/** +* Returns a pseudorandom integer on the interval `[0, 4294967295]`. +* +* ## Notes +* +* - This pseudorandom number generator (PRNG) is a 32-bit Tiny Mersenne Twister pseudorandom number generator. +* - The PRNG is *not* a cryptographically secure PRNG. +* - The PRNG has a period of 2^127 - 1. +* +* @returns pseudorandom number +* +* @example +* var v = tinymt32(); +* // returns +* +* @example +* var rand = tinymt32.factory({ +* 'seed': 12345 +* }); +* var v = rand(); +* // returns +*/ +declare var tinymt32: Random; + + +// EXPORTS // + +export = tinymt32; diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/docs/types/test.ts b/lib/node_modules/@stdlib/random/base/tinymt32/docs/types/test.ts new file mode 100644 index 000000000000..f1dac304f5e4 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/docs/types/test.ts @@ -0,0 +1,94 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import tinymt32 from './index'; + + +// TESTS // + +// The function returns a number... +{ + tinymt32(); // $ExpectType number +} + +// The compiler throws an error if the function is provided any arguments... +{ + tinymt32( true ); // $ExpectError + tinymt32( 2, 3 ); // $ExpectError +} + +// Attached to main export is a `factory` method which returns a function... +{ + tinymt32.factory(); // $ExpectType NullaryFunction + tinymt32.factory( { 'copy': false } ); // $ExpectType NullaryFunction +} + +// The `factory` method returns a function which returns a number... +{ + const fcn = tinymt32.factory(); + fcn(); // $ExpectType number +} + +// The compiler throws an error if the function returned by the `factory` method is provided any number of arguments... +{ + const fcn = tinymt32.factory(); + fcn( 1 ); // $ExpectError + fcn( 2, 1 ); // $ExpectError + fcn( 2, 1, 1 ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided an options argument which is not an object... +{ + tinymt32.factory( null ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided a `seed` option which is not a valid seed... +{ + tinymt32.factory( { 'seed': true } ); // $ExpectError + tinymt32.factory( { 'seed': 'abc' } ); // $ExpectError + tinymt32.factory( { 'seed': null } ); // $ExpectError + tinymt32.factory( { 'seed': [ 'a' ] } ); // $ExpectError + tinymt32.factory( { 'seed': {} } ); // $ExpectError + tinymt32.factory( { 'seed': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided a `state` option which is not a valid state... +{ + tinymt32.factory( { 'state': 123 } ); // $ExpectError + tinymt32.factory( { 'state': 'abc' } ); // $ExpectError + tinymt32.factory( { 'state': null } ); // $ExpectError + tinymt32.factory( { 'state': [] } ); // $ExpectError + tinymt32.factory( { 'state': {} } ); // $ExpectError + tinymt32.factory( { 'state': true } ); // $ExpectError + tinymt32.factory( { 'state': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided a `copy` option which is not a boolean... +{ + tinymt32.factory( { 'copy': 123 } ); // $ExpectError + tinymt32.factory( { 'copy': 'abc' } ); // $ExpectError + tinymt32.factory( { 'copy': null } ); // $ExpectError + tinymt32.factory( { 'copy': [] } ); // $ExpectError + tinymt32.factory( { 'copy': {} } ); // $ExpectError + tinymt32.factory( { 'copy': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided more than one argument... +{ + tinymt32.factory( {}, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/examples/c/Makefile b/lib/node_modules/@stdlib/random/base/tinymt32/examples/c/Makefile new file mode 100644 index 000000000000..25ced822f96a --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/examples/c/example.c b/lib/node_modules/@stdlib/random/base/tinymt32/examples/c/example.c new file mode 100644 index 000000000000..4027d6c372e7 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/examples/c/example.c @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "stdlib/random/base/tinymt32.h" +#include "stdlib/random/base/shared.h" +#include +#include +#include + +int main( void ) { + int8_t status; + uint64_t v; + int32_t i; + double d; + + // Define a seed: + uint32_t seed[] = { 12345 }; + + // Create a PRNG... + struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); + if ( obj == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + status = stdlib_base_random_tinymt32_seed( obj, seed ); + if ( status != 0 ) { + printf( "Unable to retrieve the PRNG seed.\n" ); + exit( 1 ); + } + printf( "seed = %d\n", seed[ 0 ] ); + + printf( "name = %s\n", obj->prng->name ); + printf( "min = %"PRIu64"\n", obj->prng->min ); + printf( "max = %"PRIu64"\n", obj->prng->max ); + + printf( "\nPseudorandom integers...\n" ); + for ( i = 0; i < 10; i++ ) { + status = obj->prng->next( obj, &v ); + if ( status != 0 ) { + printf( "Unexpected result.\n" ); + exit( 1 ); + } + printf( "%"PRIu64"\n", v ); + } + + printf( "\n" ); + printf( "min (normalized) = %0.16f\n", obj->prng->normalized_min ); + printf( "max (normalized) = %0.16f\n", obj->prng->normalized_max ); + + printf( "\nPseudorandom doubles...\n" ); + for ( i = 0; i < 10; i++ ) { + status = obj->prng->normalized( obj, &d ); + if ( status != 0 ) { + printf( "Unexpected result.\n" ); + exit( 1 ); + } + printf( "%0.16f\n", d ); + } + + stdlib_base_random_tinymt32_free( obj ); +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/examples/index.js b/lib/node_modules/@stdlib/random/base/tinymt32/examples/index.js new file mode 100644 index 000000000000..e96a02adc95d --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var tinymt32 = require( './../lib' ); + +// Generate pseudorandom numbers... +console.log( '\nseed: %d', tinymt32.seed[ 0 ] ); +var i; +for ( i = 0; i < 100; i++ ) { + console.log( tinymt32() ); +} + +// Create a new pseudorandom number generator... +var seed = 1234; +var rand = tinymt32.factory({ + 'seed': seed +}); +console.log( '\nseed: %d', seed ); +for ( i = 0; i < 100; i++ ) { + console.log( rand() ); +} + +// Create another pseudorandom number generator using a previous seed... +rand = tinymt32.factory({ + 'seed': tinymt32.seed +}); +console.log( '\nseed: %d', rand.seed[ 0 ] ); +for ( i = 0; i < 100; i++ ) { + console.log( rand() ); +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/include/stdlib/random/base/tinymt32.h b/lib/node_modules/@stdlib/random/base/tinymt32/include/stdlib/random/base/tinymt32.h new file mode 100644 index 000000000000..50f251047132 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/include/stdlib/random/base/tinymt32.h @@ -0,0 +1,77 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef STDLIB_RANDOM_BASE_TINYMT32_H +#define STDLIB_RANDOM_BASE_TINYMT32_H + +#include "stdlib/random/base/shared.h" +#include + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Declare an opaque type definition for the PRNG state. +*/ +typedef struct { + uint32_t *seed; + int64_t seed_length; + uint32_t state[4]; + uint32_t mat1; + uint32_t mat2; + uint32_t tmat; +} stdlib_base_random_tinymt32_state_t; + +/** +* Returns a pointer to a dynamically allocated PRNG. +*/ +struct BasePRNGObject * stdlib_base_random_tinymt32_allocate( const uint32_t *seed, const int64_t len ); +/** +* Frees a PRNG's allocated memory. +*/ +void stdlib_base_random_tinymt32_free( struct BasePRNGObject *obj ); + +/** +* Returns the PRNG seed length. +*/ +int8_t stdlib_base_random_tinymt32_seed_length( const struct BasePRNGObject *obj, int64_t *len ); + +/** +* Returns a PRNG seed. +*/ +int8_t stdlib_base_random_tinymt32_seed( const struct BasePRNGObject *obj, uint32_t *seed ); + +/** +* Returns a copy of the current PRNG state. +*/ +void * stdlib_base_random_tinymt32_state( const struct BasePRNGObject *obj ); + +/** +* Sets the PRNG state. +*/ +int8_t stdlib_base_random_tinymt32_set( struct BasePRNGObject *obj, const void *state ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_RANDOM_BASE_TINYMT32_H diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/lib/factory.js b/lib/node_modules/@stdlib/random/base/tinymt32/lib/factory.js new file mode 100644 index 000000000000..4b7c6c39729e --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/lib/factory.js @@ -0,0 +1,631 @@ +/* eslint-disable max-len */ + +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); +var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isCollection = require( '@stdlib/assert/is-collection' ); +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); +var format = require( '@stdlib/string/format' ); +var unsignedMul = require( '@stdlib/number/uint32/base/mul' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var gcopy = require( '@stdlib/blas/base/gcopy' ); +var typedarray2json = require( '@stdlib/array/to-json' ); +var randuint32 = require( './rand_uint32.js' ); + + +// VARIABLES // + +// Define the number of state elements for TinyMT32 +var N = 4; + +// Define a normalization constant when generating double-precision floating-point numbers: 2^53 => 9007199254740992 +var FLOAT64_NORMALIZATION_CONSTANT = 1.0 / ( FLOAT64_MAX_SAFE_INTEGER+1.0 ); // eslint-disable-line id-length + +// 2^26: 67108864 +var TWO_26 = 67108864 >>> 0; // asm type annotation + +// Define the maximum normalized pseudorandom double-precision floating-point number: ( (((2^32-1)>>>5)*2^26)+( (2^32-1)>>>6) ) / 2^53 +var MAX_NORMALIZED = FLOAT64_MAX_SAFE_INTEGER * FLOAT64_NORMALIZATION_CONSTANT; + +// Define the state array schema version: +var STATE_ARRAY_VERSION = 1; // NOTE: anytime the state array schema changes, this value should be incremented!!! + +// Define the number of sections in the state array: +var NUM_STATE_SECTIONS = 2; // state, seed + +// Define the index offset of the "state" section in the state array: +var STATE_SECTION_OFFSET = 2; // | version | num_sections | state_length | ...state | state_index | seed_length | ...seed | + +// Define the index offset of the seed section in the state array: +var SEED_SECTION_OFFSET = N + 4; // | version | num_sections | state_length | ...state | state_index | seed_length | ...seed | + +// Define the length of the "fixed" length portion of the state array: +var STATE_FIXED_LENGTH = N + 5; // 1 (version) + 1 (num_sections) + 1 (state_length) + N (state) + 1 (state_index) + 1 (seed_length) + +// TinyMT32 constants +var MAT1 = 0x8F7011EE; +var MAT2 = 0xFC78FF1F; +var TMAT = 0x3793FDFF; + +// TinyMT32 shift constants +var SH0 = 1; +var SH1 = 10; +var SH8 = 8; +var MASK = 0x7FFFFFFF; + +// Initialization constants +var MIN_LOOP = 8; +var PRE_LOOP = 8; +var KNUTH_MULTIPLIER = 1812433253 >>> 0; // asm type annotation + + +// FUNCTIONS // + +/** +* Verifies state array integrity. +* +* @private +* @param {Uint32Array} state - state array +* @param {boolean} FLG - flag indicating whether the state array was provided as an option (true) or an argument (false) +* @returns {(Error|null)} an error or `null` +*/ +function verifyState( state, FLG ) { + var s1; + if ( FLG ) { + s1 = 'option'; + } else { + s1 = 'argument'; + } + if ( state.length < STATE_FIXED_LENGTH+1 ) { + return new RangeError( format( 'invalid %s. State array has insufficient length.', s1 ) ); + } + if ( state[ 0 ] !== STATE_ARRAY_VERSION ) { + return new RangeError( format( 'invalid %s. State array has an incompatible schema version. Expected: `%s`. Actual: `%s`.', s1, STATE_ARRAY_VERSION, state[ 0 ] ) ); + } + if ( state[ 1 ] !== NUM_STATE_SECTIONS ) { + return new RangeError( format( 'invalid %s. State array has an incompatible number of sections. Expected: `%s`. Actual: `%s`.', s1, NUM_STATE_SECTIONS, state[ 1 ] ) ); + } + if ( state[ STATE_SECTION_OFFSET ] !== N ) { + return new RangeError( format( 'invalid %s. State array has an incompatible state length. Expected: `%u`. Actual: `%u`.', s1, N, state[ STATE_SECTION_OFFSET ] ) ); + } + if ( state[ SEED_SECTION_OFFSET ] !== state.length-STATE_FIXED_LENGTH ) { + 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 ] ) ); + } + return null; +} + +/** +* Returns an initial PRNG state. +* +* @private +* @param {Uint32Array} state - state array +* @param {PositiveInteger} N - state size (should be 4 for TinyMT32) +* @param {uinteger32} s - seed +* @returns {Uint32Array} state array +*/ +function createState( state, N, s ) { + var i; + + // Set the initial state values + state[ 0 ] = s >>> 0; + state[ 1 ] = MAT1; + state[ 2 ] = MAT2; + state[ 3 ] = TMAT; + + // Initialize using the algorithm from tinymt32_init + for ( i = 1; i < MIN_LOOP; i++ ) { + state[ i & 3 ] ^= i + unsignedMul(KNUTH_MULTIPLIER, (state[(i - 1) & 3] ^ (state[(i - 1) & 3] >>> 30))); + } + + // Pre-run the algorithm to improve randomness of the initial state + for ( i = 0; i < PRE_LOOP; i++ ) { + nextState( state ); + } + + return state; +} + +/** +* Function used in initialization by initState. +* @private +* @param {number} x - 32-bit integer +* @returns {number} 32-bit integer +*/ +function iniFunc1(x) { + return unsignedMul((x ^ (x >>> 27)), 1664525); +} + +/** +* Function used in initialization by initState. +* @private +* @param {number} x - 32-bit integer +* @returns {number} 32-bit integer +*/ +function iniFunc2(x) { + return unsignedMul((x ^ (x >>> 27)), 1566083941); +} + +/** +* Initializes the internal state array with an array of 32-bit unsigned integers used as seeds. +* +* @private +* @param {Uint32Array} state - state array +* @param {Uint32Array} initKey - array of 32-bit integers used as seeds +* @param {number} keyLength - length of initKey array +* @returns {void} +*/ +function initState( state, initKey, keyLength ) { + var count; + var size = 4; + var lag = 1; + var mid = 1; + var i; + var j; + var r; + + state[0] = 0; + state[1] = MAT1; + state[2] = MAT2; + state[3] = TMAT; + + count = (keyLength + 1 > MIN_LOOP) ? keyLength + 1 : MIN_LOOP; + + r = iniFunc1(state[0] ^ state[mid % size] ^ state[(size - 1) % size]) >>> 0; + state[mid % size] = (state[mid % size] + r) >>> 0; + r = (r + keyLength) >>> 0; + state[(mid + lag) % size] = (state[(mid + lag) % size] + r) >>> 0; + state[0] = r; + count -= 1; + + for (i = 1, j = 0; (j < count) && (j < keyLength); j++) { + r = iniFunc1(state[i % size] ^ state[(i + mid) % size] ^ state[(i + size - 1) % size]) >>> 0; + state[(i + mid) % size] = (state[(i + mid) % size] + r) >>> 0; + r = (r + initKey[j] + i) >>> 0; + state[(i + mid + lag) % size] = (state[(i + mid + lag) % size] + r) >>> 0; + state[i % size] = r; + i = (i + 1) % size; + } + + for (; j < count; j++) { + r = iniFunc1(state[i % size] ^ state[(i + mid) % size] ^ state[(i + size - 1) % size]) >>> 0; + state[(i + mid) % size] = (state[(i + mid) % size] + r) >>> 0; + r = (r + i) >>> 0; + state[(i + mid + lag) % size] = (state[(i + mid + lag) % size] + r) >>> 0; + state[i % size] = r; + i = (i + 1) % size; + } + + for (j = 0; j < size; j++) { + r = iniFunc2(state[i % size] + state[(i + mid) % size] + state[(i + size - 1) % size]) >>> 0; + state[(i + mid) % size] ^= r; + r = (r - i) >>> 0; + state[(i + mid + lag) % size] ^= r; + state[i % size] = r; + i = (i + 1) % size; + } + + for (i = 0; i < PRE_LOOP; i++) { + nextState(state); + } +} + +/** +* Updates TinyMT32's internal state. +* +* @private +* @param {Uint32Array} state - state array (length 4) +* @returns {Uint32Array} state array +*/ +function nextState( state ) { + var x; + var y; + + // Mix state + y = state[3]; + x = (state[0] & MASK) ^ state[1] ^ state[2]; + x ^= (x << SH0); + y ^= (y >>> SH0) ^ x; + state[0] = state[1]; + state[1] = state[2]; + state[2] = x ^ (y << SH1); + state[3] = y; + + // Apply the tempering + if (y & 1) { + state[1] ^= MAT1; + state[2] ^= MAT2; + } + + return state; +} + +/** +* Generates a tempered output from the state. +* +* @private +* @param {Uint32Array} state - state array +* @returns {number} tempered output +*/ +function temper( state ) { + var t0; + var t1; + + // Tempering + t0 = state[3]; + t1 = (state[0] + (state[2] >>> SH8)) >>> 0; + t0 ^= t1; + + if ((t1 & 1) !== 0) { + t0 ^= TMAT; + } + + return t0 >>> 0; // Ensure unsigned 32-bit integer +} + +/** +* Returns a pseudorandom integer on the interval \\( [0, 2^{32}) \\). +* +* @private +* @param {Uint32Array} state - generator state +* @returns {number} pseudorandom value +*/ +function next( state ) { + nextState( state ); + return temper( state ); +} + + +// MAIN // + +/** +* Returns a TinyMT32 pseudorandom number generator. +* +* ## Notes +* +* - TinyMT32 uses a state of only 127 bits (vs MT19937's 19968 bits), making it more memory efficient. +* - The period of TinyMT32 is 2^127-1, which is significantly shorter than MT19937, but still very large. +* +* @param {Options} [options] - options +* @param {PRNGSeedTinyMT32} [options.seed] - pseudorandom number generator seed +* @param {PRNGStateTinyMT32} [options.state] - pseudorandom number generator state +* @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state +* @throws {TypeError} options argument must be an object +* @throws {TypeError} a seed must be either a positive integer less than the maximum unsigned 32-bit integer or an array-like object containing integers less than the maximum unsigned 32-bit integer +* @throws {RangeError} a numeric seed must be a positive integer less than the maximum unsigned 32-bit integer +* @throws {TypeError} state must be a Uint32Array +* @throws {Error} must provide a valid state +* @throws {TypeError} `copy` option must be a boolean +* @returns {PRNG} TinyMT32 PRNG +* +* @example +* var tinymt32 = factory(); +* +* var v = tinymt32(); +* // returns +* +* @example +* // Return a seeded TinyMT32 PRNG: +* var tinymt32 = factory({ +* 'seed': 1234 +* }); +* +* var v = tinymt32(); +* // returns +*/ +function factory( options ) { + var STATE; + var state; + var opts; + var seed; + var slen; + var err; + + opts = {}; + if ( arguments.length ) { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'copy' ) ) { + opts.copy = options.copy; + if ( !isBoolean( options.copy ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'copy', options.copy ) ); + } + } + if ( hasOwnProp( options, 'state' ) ) { + state = options.state; + opts.state = true; + if ( !isUint32Array( state ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a Uint32Array. Option: `%s`.', 'state', state ) ); + } + err = verifyState( state, true ); + if ( err ) { + throw err; + } + if ( opts.copy === false ) { + STATE = state; + } else { + STATE = new Uint32Array( state.length ); + gcopy( state.length, state, 1, STATE, 1 ); + } + // Create a state "view": + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); + + // Create a seed "view": + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), STATE[ SEED_SECTION_OFFSET ] ); + } + // If provided a PRNG state, we ignore the `seed` option... + if ( seed === void 0 ) { + if ( hasOwnProp( options, 'seed' ) ) { + seed = options.seed; + opts.seed = true; + if ( isPositiveInteger( seed ) ) { + if ( seed > UINT32_MAX ) { + throw new RangeError( format( 'invalid option. `%s` option must be a positive integer less than the maximum unsigned 32-bit integer. Option: `%u`.', 'seed', seed ) ); + } + STATE = new Uint32Array( STATE_FIXED_LENGTH+1 ); + + // Initialize sections: + STATE[ 0 ] = STATE_ARRAY_VERSION; + STATE[ 1 ] = NUM_STATE_SECTIONS; + STATE[ STATE_SECTION_OFFSET ] = N; + STATE[ SEED_SECTION_OFFSET ] = 1; + STATE[ SEED_SECTION_OFFSET+1 ] = seed >>> 0; + + // Create a state "view": + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); + + // Create a seed "view": + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + + // Initialize the internal PRNG state: + createState(state, N, seed[0]); + } else if ( isCollection( seed ) && seed.length > 0 ) { + slen = seed.length; + STATE = new Uint32Array( STATE_FIXED_LENGTH+slen ); + + // Initialize sections: + STATE[ 0 ] = STATE_ARRAY_VERSION; + STATE[ 1 ] = NUM_STATE_SECTIONS; + STATE[ STATE_SECTION_OFFSET ] = N; + STATE[ SEED_SECTION_OFFSET ] = slen; + + // Copy the provided seed array to prevent external mutation: + gcopy.ndarray( slen, seed, 1, 0, STATE, 1, SEED_SECTION_OFFSET+1 ); + + // Create a state "view": + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); + + // Create a seed "view": + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), slen ); + + // Initialize the internal PRNG state using seed array: + initState(state, seed, slen); + } else { + throw new TypeError( format( 'invalid option. `%s` option must be either a positive integer less than the maximum unsigned 32-bit integer or an array-like object containing integer values less than the maximum unsigned 32-bit integer. Option: `%s`.', 'seed', seed ) ); + } + } else { + seed = randuint32() >>> 0; // asm type annotation + } + } + } else { + seed = randuint32() >>> 0; // asm type annotation + } + if ( state === void 0 ) { + STATE = new Uint32Array( STATE_FIXED_LENGTH + 1 ); + + // Initialize sections: + STATE[ 0 ] = STATE_ARRAY_VERSION; + STATE[ 1 ] = NUM_STATE_SECTIONS; + STATE[ STATE_SECTION_OFFSET ] = N; + STATE[ SEED_SECTION_OFFSET ] = 1; + STATE[ SEED_SECTION_OFFSET+1 ] = seed; + + // Create a state "view": + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); + + // Create a seed "view": + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + + // Initialize the internal PRNG state: + createState(state, N, seed[0]); + } + + // Note: property order matters in order to maintain consistency of PRNG "shape" (hidden classes). + setReadOnly( tinymt32, 'NAME', 'tinymt32' ); + setReadOnlyAccessor( tinymt32, 'seed', getSeed ); + setReadOnlyAccessor( tinymt32, 'seedLength', getSeedLength ); + setReadWriteAccessor( tinymt32, 'state', getState, setState ); + setReadOnlyAccessor( tinymt32, 'stateLength', getStateLength ); + setReadOnlyAccessor( tinymt32, 'byteLength', getStateSize ); + setReadOnly( tinymt32, 'toJSON', toJSON ); + setReadOnly( tinymt32, 'MIN', 0 ); + setReadOnly( tinymt32, 'MAX', UINT32_MAX ); + setReadOnly( tinymt32, 'normalized', normalized ); + + setReadOnly( normalized, 'NAME', tinymt32.NAME ); + setReadOnlyAccessor( normalized, 'seed', getSeed ); + setReadOnlyAccessor( normalized, 'seedLength', getSeedLength ); + setReadWriteAccessor( normalized, 'state', getState, setState ); + setReadOnlyAccessor( normalized, 'stateLength', getStateLength ); + setReadOnlyAccessor( normalized, 'byteLength', getStateSize ); + setReadOnly( normalized, 'toJSON', toJSON ); + setReadOnly( normalized, 'MIN', 0.0 ); + setReadOnly( normalized, 'MAX', MAX_NORMALIZED ); + + return tinymt32; + + /** + * Returns the PRNG seed. + * + * @private + * @returns {PRNGSeedTinyMT32} seed + */ + function getSeed() { + return seed; + } + + /** + * Returns the PRNG seed length. + * + * @private + * @returns {PositiveInteger} seed length + */ + function getSeedLength() { + return seed.length; + } + + /** + * Returns the PRNG state length. + * + * @private + * @returns {PositiveInteger} state length + */ + function getStateLength() { + return state.length; + } + + /** + * Returns the PRNG state size (in bytes). + * + * @private + * @returns {PositiveInteger} state size (in bytes) + */ + function getStateSize() { + return STATE.byteLength; + } + + /** + * Returns the current pseudorandom number generator state. + * + * @private + * @returns {PRNGStateTinyMT32} current state + */ + function getState() { + var out = new Uint32Array( STATE.length ); + gcopy( STATE.length, STATE, 1, out, 1 ); + return out; + } + + /** + * Sets the PRNG state. + * + * ## Notes + * + * - 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. + * - 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). + * + * @private + * @param {PRNGStateTinyMT32} s - generator state + * @throws {TypeError} must provide a `Uint32Array` + * @throws {Error} must provide a valid state + */ + function setState( s ) { + var err; + + if ( !isUint32Array( s ) ) { + throw new TypeError( format( 'invalid argument. Must provide a Uint32Array. Value: `%s`.', s ) ); + } + err = verifyState( s, false ); + if ( err ) { + throw err; + } + if ( opts.copy === false ) { + if ( opts.state && s.length === STATE.length ) { + gcopy( s.length, s, 1, STATE, 1 ); // update current shared state + } else { + STATE = s; // point to new shared state + opts.state = true; // setting this flag allows updating a shared state even if a state array was not provided at PRNG creation + } + } else { + // Check if we can reuse allocated memory... + if ( s.length !== STATE.length ) { + STATE = new Uint32Array( s.length ); // reallocate + } + gcopy( s.length, s, 1, STATE, 1 ); + } + // Create a new state "view": + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); + + // Create a new seed "view": + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), STATE[ SEED_SECTION_OFFSET ] ); + } + + /** + * Serializes the pseudorandom number generator as a JSON object. + * + * ## Notes + * + * - `JSON.stringify()` implicitly calls this method when stringifying a PRNG. + * + * @private + * @returns {Object} JSON representation + */ + function toJSON() { + var out = {}; + out.type = 'PRNG'; + out.name = tinymt32.NAME; + out.state = typedarray2json( STATE ); + out.params = []; + return out; + } + + /** + * Generates a pseudorandom number on the interval \\( [0, 1) \\). + * + * ## Notes + * + * - The original C implementation credits Isaku Wada for this algorithm (2002/01/09). + * + * @private + * @returns {number} pseudorandom number + * + * @example + * var r = normalized(); + * // returns + */ + function normalized() { + var x = tinymt32() >>> 5; + var y = tinymt32() >>> 6; + return ( (x*TWO_26)+y ) * FLOAT64_NORMALIZATION_CONSTANT; + } + + /** + * Returns a pseudorandom integer on the interval \\([0,2^{32})\\). + * + * @private + * @returns {number} pseudorandom number + */ + function tinymt32() { + return next(state); + } +} + +module.exports = factory; diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/lib/index.js b/lib/node_modules/@stdlib/random/base/tinymt32/lib/index.js new file mode 100644 index 000000000000..3de032da99ed --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* A 32-bit Tiny Mersenne Twister pseudorandom number generator. +* +* @module @stdlib/random/base/tinymt32 +* +* @example +* var tinymt32 = require( '@stdlib/random/base/tinymt32' ); +* +* var v = tinymt32(); +* // returns +* +* @example +* var factory = require( '@stdlib/random/base/tinymt32' ).factory; +* +* var tinymt32 = factory({ +* 'seed': 1234 +* }); +* +* var v = tinymt32(); +* // returns 2682965004 +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var factory = require( './factory.js' ); + + +// MAIN // + +setReadOnly( main, 'factory', factory ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/lib/main.js b/lib/node_modules/@stdlib/random/base/tinymt32/lib/main.js new file mode 100644 index 000000000000..233473ca6aec --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/lib/main.js @@ -0,0 +1,84 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var factory = require( './factory.js' ); +var randuint32 = require( './rand_uint32.js' ); + + +// MAIN // + +/** +* Generates a pseudorandom integer on the interval \\( [0, 2^{32}) \\). +* +* ## Method +* +* - TinyMT32 uses a state of only 127 bits, consisting of four 32-bit integers, making it more memory efficient than MT19937. +* +* - The state transition involves the following steps: +* ```javascript +* y = state[3]; +* x = (state[0] & 0x7FFFFFFF) ^ state[1] ^ state[2]; +* x ^= (x << 1); +* y ^= (y >> 1) ^ x; +* state[0] = state[1]; +* state[1] = state[2]; +* state[2] = x ^ (y << 10); +* state[3] = y; +* ``` +* +* - The tempering function uses two magic constants (MAT1 = 0x8F7011EE, MAT2 = 0xFC78FF1F) when y is odd: +* ```javascript +* if (y & 1) { +* state[1] ^= MAT1; +* state[2] ^= MAT2; +* } +* ``` +* +* - The final output is generated using another magic constant (TMAT = 0x3793FDFF): +* ```javascript +* y = state[0] + (state[2] >> 8); +* return (y ^ TMAT) >>> 0; +* ``` +* +* - For normalized double-precision floating-point numbers, we multiply the generated 32-bit integer by \\( 2^{-53} \\), which ensures a uniform distribution on [0,1). +* +* ## References +* +* - Saito, Mutsuo, and Matsumoto, Makoto. 2011. "Tiny Mersenne Twister (TinyMT): A small-sized variant of Mersenne Twister." +* +* +* @function tinymt32 +* @type {PRNG} +* @returns {PositiveInteger} pseudorandom integer +* +* @example +* var v = tinymt32(); +* // returns +*/ +var tinymt32 = factory({ + 'seed': randuint32() +}); + + +// EXPORTS // + +module.exports = tinymt32; diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/lib/rand_uint32.js b/lib/node_modules/@stdlib/random/base/tinymt32/lib/rand_uint32.js new file mode 100644 index 000000000000..ed0944c41d78 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/lib/rand_uint32.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// VARIABLES // + +var MAX = UINT32_MAX - 1; + + +// MAIN // + +/** +* Returns a pseudorandom integer on the interval \\( [1, 2^{32}-1) \\). +* +* @private +* @returns {PositiveInteger} pseudorandom integer +* +* @example +* var v = randuint32(); +* // returns +*/ +function randuint32() { + var v = floor( 1.0 + (MAX*Math.random()) ); // eslint-disable-line stdlib/no-builtin-math + return v >>> 0; // asm type annotation +} + + +// EXPORTS // + +module.exports = randuint32; diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/manifest.json b/lib/node_modules/@stdlib/random/base/tinymt32/manifest.json new file mode 100644 index 000000000000..e91236537092 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/manifest.json @@ -0,0 +1,40 @@ +{ + "options": {}, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [ + "@stdlib/random/base/shared" + ] + } + ] +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/package.json b/lib/node_modules/@stdlib/random/base/tinymt32/package.json new file mode 100644 index 000000000000..4dee987a1f32 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/package.json @@ -0,0 +1,74 @@ +{ + "name": "@stdlib/random/base/tinymt32", + "version": "0.0.0", + "description": "A 32-bit Tiny Mersenne Twister pseudorandom number generator.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "statistics", + "stats", + "prng", + "pseudorandom", + "random", + "rand", + "randint", + "randu", + "uniform", + "generator", + "mersenne", + "twister", + "tinymt32", + "seed", + "seedable" + ] +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/src/main.c b/lib/node_modules/@stdlib/random/base/tinymt32/src/main.c new file mode 100644 index 000000000000..51af0b1ee03d --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/src/main.c @@ -0,0 +1,865 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* +* ## Notice +* +* The original C code and copyright notice are from the [source implementation][tinymt32]. +* The implementation has been modified according to the styles and conventions of this project. +* +* ```text +* Copyright (c) 2011, 2013 Mutsuo Saito, Makoto Matsumoto, +* Hiroshima University and The University of Tokyo. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following +* disclaimer in the documentation and/or other materials provided +* with the distribution. +* * Neither the name of the Hiroshima University nor the names of +* its contributors may be used to endorse or promote products +* derived from this software without specific prior written +* permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* ``` +* +* [tinymt32]: https://github.com/MersenneTwister-Lab/TinyMT/blob/master/tinymt/tinymt32.c +*/ + +#include "stdlib/random/base/tinymt32.h" +#include "stdlib/random/base/shared.h" +#include +#include +#include + +// Forward declarations: +static inline int8_t next( struct BasePRNGObject *obj, uint64_t *out ); +static inline int8_t normalized( struct BasePRNGObject *obj, double *out ); +static inline void tinymt32_free( struct BasePRNGObject *obj ); + +static inline void create_state( stdlib_base_random_tinymt32_state_t * random, const uint32_t seed ); +static inline void init_state( stdlib_base_random_tinymt32_state_t * random, const uint32_t *seed, const int64_t len ); +static inline void next_state( stdlib_base_random_tinymt32_state_t *random ); +static inline uint32_t tinymt32_temper(stdlib_base_random_tinymt32_state_t * random); +static uint32_t ini_func1(uint32_t x); +static uint32_t ini_func2(uint32_t x); + +// Define constants for TinyMT32: +static const uint32_t TINYMT32_MASK = 0x7fffffff; +static const uint32_t TINYMT32_SH0 = 1; +static const uint32_t TINYMT32_SH1 = 10; +static const uint32_t TINYMT32_SH8 = 8; +static const uint32_t MIN_LOOP = 8; +static const uint32_t PRE_LOOP = 8; + +// Define the maximum unsigned 32-bit integer: +static const uint32_t MAX_UINT32 = 4294967295; + +// Define the maximum "safe" double-precision floating-point integer: +static const double FLOAT64_MAX_SAFE_INTEGER = 9007199254740991.0; + +// Define a normalization constant when generating double-precision floating-point numbers: +static const double FLOAT64_NORMALIZATION_CONSTANT = 1.0 / ( FLOAT64_MAX_SAFE_INTEGER+1.0 ); + +// Define a multiplier (see Knuth TAOCP Vol2. 3rd Ed. P.106): 1812433253 => 01101100000001111000100101100101 +static const uint32_t KNUTH_MULTIPLIER = 1812433253; + +// 2^26: 67108864 +static const double TWO_26 = 67108864.0; + +// Define the maximum normalized pseudorandom double-precision floating-point number: ( (((2^32-1)>>>5)*2^26)+( (2^32-1)>>>6) ) / 2^53 +static const double MAX_NORMALIZED = FLOAT64_MAX_SAFE_INTEGER * FLOAT64_NORMALIZATION_CONSTANT; + +/** +* TinyMT32 PRNG. +*/ +static struct BasePRNG tinymt32_prng = { + "tinymt32", // name + (uint64_t)0, // min + (uint64_t)MAX_UINT32, // max: (2^{32}-1) + 0.0, // min (normalized) + MAX_NORMALIZED, // max (normalized): (2^{53}-1)/2^{53} + sizeof( stdlib_base_random_tinymt32_state_t ), // state_size + &next, // next() + &normalized, // normalized() + &tinymt32_free // free() +}; + +/** +* Returns a pseudorandom integer. +* +* ## Notes +* +* - The function returns `-1` if unable to generate a pseudorandom integer and `0` otherwise. +* +* ## References +* +* - Matsumoto, Makoto, and Takuji Nishimura. 1998. "Mersenne Twister: A 623-dimensionally Equidistributed Uniform Pseudo-random Number Generator." _ACM Transactions on Modeling and Computer Simulation_ 8 (1). New York, NY, USA: ACM: 3–30. doi:[10.1145/272991.272995][@matsumoto:1998a]. +* +* @param obj PRNG object +* @param out output address +* @return status code +*/ +static inline int8_t next( struct BasePRNGObject *obj, uint64_t *out ) { + stdlib_base_random_tinymt32_state_t *so; + uint32_t y; + + if ( obj == NULL || obj->prng != &tinymt32_prng ) { + return -1; + } + + // Retrieve the state object: + so = (stdlib_base_random_tinymt32_state_t *)( obj->state ); + + // Update the state: + next_state( so ); + + // Temper the state: + y = tinymt32_temper(so); + + // Set the output value: + *out = (uint64_t)( y ); + + return 0; +} + +/** +* Returns a pseudorandom double-precision floating-point number on the interval `[0, 1)`. +* +* ## Method +* +* - When generating normalized double-precision floating-point numbers, we first generate two pseudorandom integers \\( x \\) and \\( y \\) on the interval \\( [0, 2^{32}-1] \\) for a combined \\( 64 \\) random bits. +* +* - We would like \\( 53 \\) random bits to generate a 53-bit precision integer and, thus, want to discard \\( 11 \\) of the generated bits. +* +* - We do so by discarding \\( 5 \\) bits from \\( x \\) and \\( 6 \\) bits from \\( y \\). +* +* - Accordingly, \\( x \\) contains \\( 27 \\) random bits, which are subsequently shifted left \\( 26 \\) bits (multiplied by \\( 2^{26} \\), and \\( y \\) contains \\( 26 \\) random bits to fill in the lower \\( 26 \\) bits. When summed, they combine to comprise \\( 53 \\) random bits of a double-precision floating-point integer. +* +* - As an example, suppose, for the sake of argument, the 32-bit PRNG generates the maximum unsigned 32-bit integer \\( 2^{32}-1 \\) twice in a row. Then, +* +* ```c +* uint32_t x = 4294967295 >> 5; // 00000111111111111111111111111111 +* uint32_t y = 4294967295 >> 6; // 00000011111111111111111111111111 +* ``` +* +* Multiplying \\( x \\) by \\( 2^{26} \\) returns \\( 9007199187632128 \\), which, in binary, is +* +* ```binarystring +* 0 10000110011 11111111111111111111 11111100000000000000000000000000 +* ``` +* +* Adding \\( y \\) yields \\( 9007199254740991 \\) (the maximum "safe" double-precision floating-point integer value), which, in binary, is +* +* ```binarystring +* 0 10000110011 11111111111111111111 11111111111111111111111111111111 +* ``` +* +* - Similarly, suppose the 32-bit PRNG generates the following values +* +* ```c +* uint32_t x = 1 >> 5; // 0 => 00000000000000000000000000000000 +* uint32_t y = 64 >> 6; // 1 => 00000000000000000000000000000001 +* ``` +* +* Multiplying \\( x \\) by \\( 2^{26} \\) returns \\( 0 \\), which, in binary, is +* +* ```binarystring +* 0 00000000000 00000000000000000000 00000000000000000000000000000000 +* ``` +* +* Adding \\( y \\) yields \\( 1 \\), which, in binary, is +* +* ```binarystring +* 0 01111111111 00000000000000000000 00000000000000000000000000000000 +* ``` +* +* - As different combinations of \\( x \\) and \\( y \\) are generated, different combinations of double-precision floating-point exponent and significand bits will be toggled, thus generating pseudorandom double-precision floating-point numbers. +* +* ## Notes +* +* - The function returns `-1` if unable to generate a pseudorandom number and `0` otherwise. +* +* ## References +* +* - Harase, Shin. 2017. "Conversion of Mersenne Twister to double-precision floating-point numbers." _ArXiv_ abs/1708.06018 (September). . +* +* @param obj PRNG object +* @param out output address +* @return status code +*/ +static inline int8_t normalized( struct BasePRNGObject *obj, double *out ) { + uint64_t x; + uint64_t y; + if ( obj == NULL || obj->prng != &tinymt32_prng ) { + return -1; + } + next( obj, &x); + x >>= 5; + + next( obj, &y ); + y >>= 6; + + // Note: casting `x` and `y` to doubles here is fine, as neither will ever exceed the maximum "safe" double-precision floating-point number: + *out = ( ((double)x*TWO_26)+(double)y ) * FLOAT64_NORMALIZATION_CONSTANT; + + return 0; +} + +/** +* Frees a PRNG's allocated memory. +* +* @param obj PRNG object +*/ +static inline void tinymt32_free( struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &tinymt32_prng ) { + return; + } + stdlib_base_random_tinymt32_state_t *state = (stdlib_base_random_tinymt32_state_t *)( obj->state ); + free( state->seed ); + free( obj->state ); + free( obj ); +} + +/** +* Updates the PRNG state. +* +* @param random tinymt internal state +*/ +static inline void next_state( stdlib_base_random_tinymt32_state_t *random ) { + uint32_t x; + uint32_t y; + + y = random->state[3]; + x = (random->state[0] & TINYMT32_MASK) + ^ random->state[1] + ^ random->state[2]; + x ^= (x << TINYMT32_SH0); + y ^= (y >> TINYMT32_SH0) ^ x; + random->state[0] = random->state[1]; + random->state[1] = random->state[2]; + random->state[2] = x ^ (y << TINYMT32_SH1); + random->state[3] = y; + int32_t const a = -((int32_t)(y & 1)) & (int32_t)random->mat1; + int32_t const b = -((int32_t)(y & 1)) & (int32_t)random->mat2; + random->state[1] ^= (uint32_t)a; + random->state[2] ^= (uint32_t)b; +} + +/** + * This function outputs 32-bit unsigned integer from internal state. + * Users should not call this function directly. + * @param random tinymt internal status + * @return 32-bit unsigned pseudorandom number + */ +static inline uint32_t tinymt32_temper(stdlib_base_random_tinymt32_state_t * random) { + uint32_t t0, t1; + t0 = random->state[3]; + t1 = random->state[0] + + (random->state[2] >> TINYMT32_SH8); + t0 ^= t1; + if ((t1 & 1) != 0) { + t0 ^= random->tmat; + } + return t0; +} + +/** + * This function represents a function used in the initialization + * by init_by_array + * @param x 32-bit integer + * @return 32-bit integer + */ +static uint32_t ini_func1(uint32_t x) { + return (x ^ (x >> 27)) * UINT32_C(1664525); +} + +/** + * This function represents a function used in the initialization + * by init_by_array + * @param x 32-bit integer + * @return 32-bit integer + */ +static uint32_t ini_func2(uint32_t x) { + return (x ^ (x >> 27)) * UINT32_C(1566083941); +} + +/** +* Creates an initial PRNG state. +* +* @param random tinymt internal status +* @param seed seed +*/ +static inline void create_state(stdlib_base_random_tinymt32_state_t * random, const uint32_t seed) { + random->seed = malloc(sizeof(uint32_t)); + random->seed[0] = seed; + random->seed_length = 1; + random->state[0] = seed; + random->state[1] = random->mat1; + random->state[2] = random->mat2; + random->state[3] = random->tmat; + + for (unsigned int i = 1; i < MIN_LOOP; i++) { + random->state[i & 3] ^= i + KNUTH_MULTIPLIER + * (random->state[(i - 1) & 3] + ^ (random->state[(i - 1) & 3] >> 30)); + } + + for (unsigned int i = 0; i < PRE_LOOP; i++) { + next_state(random); + } +} + +/** +* Initializes a PRNG state array according to a seed array. +* +* @param random tinymt internal status +* @param seed seed array +* @param len seed array length +*/ +static inline void init_state( stdlib_base_random_tinymt32_state_t * random, const uint32_t *seed, const int64_t len ) { + const unsigned int lag = 1; + const unsigned int mid = 1; + const unsigned int size = 4; + unsigned int i, j; + unsigned int count; + uint32_t r; + uint32_t * st = &random->state[0]; + + random->seed = malloc(sizeof(uint32_t)); + memcpy(random->seed, seed, sizeof(uint32_t)*len); + random->seed_length = len; + + st[0] = 0; + st[1] = random->mat1; + st[2] = random->mat2; + st[3] = random->tmat; + if (len + 1 > MIN_LOOP) { + count = (unsigned int)len + 1; + } else { + count = MIN_LOOP; + } + r = ini_func1(st[0] ^ st[mid % size] + ^ st[(size - 1) % size]); + st[mid % size] += r; + r += (unsigned int)len; + st[(mid + lag) % size] += r; + st[0] = r; + count--; + for (i = 1, j = 0; (j < count) && (j < (unsigned int)len); j++) { + r = ini_func1(st[i % size] + ^ st[(i + mid) % size] + ^ st[(i + size - 1) % size]); + st[(i + mid) % size] += r; + r += seed[j] + i; + st[(i + mid + lag) % size] += r; + st[i % size] = r; + i = (i + 1) % size; + } + for (; j < count; j++) { + r = ini_func1(st[i % size] + ^ st[(i + mid) % size] + ^ st[(i + size - 1) % size]); + st[(i + mid) % size] += r; + r += i; + st[(i + mid + lag) % size] += r; + st[i % size] = r; + i = (i + 1) % size; + } + for (j = 0; j < size; j++) { + r = ini_func2(st[i % size] + + st[(i + mid) % size] + + st[(i + size - 1) % size]); + st[(i + mid) % size] ^= r; + r -= i; + st[(i + mid + lag) % size] ^= r; + st[i % size] = r; + i = (i + 1) % size; + } + for (i = 0; i < PRE_LOOP; i++) { + next_state(random); + } +} + + +/** +* Returns a pointer to a dynamically allocated PRNG. +* +* ## Notes +* +* - The user is responsible for freeing the allocated memory. +* +* @param seed pointer to a seed array +* @param len seed array length +* @return pointer to a dynamically allocated PRNG or, if unable to allocate memory, a null pointer +* +* @example +* #include "stdlib/random/base/tinymt32.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Define a PRNG seed: +* uint32_t seed[] = { 12345 }; +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* uint64_t r; +* int8_t status = obj->prng->next( obj, &r ); +* if ( status != 0 ) { +* fprintf( stderr, "Unexpected result.\n" ); +* exit( 1 ); +* } +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* // Free allocated memory: +* stdlib_base_random_tinymt32_free( obj ); +*/ +struct BasePRNGObject * stdlib_base_random_tinymt32_allocate( const uint32_t *seed, const int64_t len ) { + stdlib_base_random_tinymt32_state_t *state; + struct BasePRNGObject *obj; + + obj = malloc( sizeof( struct BasePRNGObject ) ); + if ( obj == NULL ) { + return NULL; + } + state = malloc( sizeof( stdlib_base_random_tinymt32_state_t ) ); + if ( state == NULL ) { + free( obj ); // prevent memory leaks + return NULL; + } + obj->prng = &tinymt32_prng; + obj->state = state; + + // Initialize mat1, mat2, and tmat before using them + state->mat1 = 0x8f7011ee; // Default parameter values from TinyMT + state->mat2 = 0xfc78ff1f; // Default parameter values from TinyMT + state->tmat = 0x3793fdff; // Default parameter values from TinyMT + + // Initialize the PRNG state: + create_state( state, seed[0] ); + init_state( state, seed, len ); + + return obj; +} + +/** +* Frees a PRNG's allocated memory. +* +* @param obj PRNG object +* +* @example +* #include "stdlib/random/base/tinymt32.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Define a PRNG seed: +* uint32_t seed[] = { 12345 }; +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* uint64_t r; +* int8_t status = obj->prng->next( obj, &r ); +* if ( status != 0 ) { +* fprintf( stderr, "Unexpected result.\n" ); +* exit( 1 ); +* } +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* // Free allocated memory: +* stdlib_base_random_tinymt32_free( obj ); +*/ +void stdlib_base_random_tinymt32_free( struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &tinymt32_prng ) { + return; + } + obj->prng->free( obj ); +} + +/** +* Returns the PRNG seed length. +* +* ## Notes +* +* - The function returns `-1` if unable to resolve a PRNG seed length and `0` otherwise. +* +* @param obj PRNG object +* @param len output address +* @return status code +* +* @example +* #include "stdlib/random/base/tinymt32.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Define a PRNG seed: +* uint32_t seed1[] = { 12345 }; +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed1, 1 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* // ... +* +* // Get the seed length: +* int64_t seed_length; +* int8_t status = stdlib_base_random_tinymt32_seed_length( obj, &seed_length ); +* if ( status != 0 ) { +* fprintf( stderr, "Error encountered when attempting to retrieve the PRNG seed length.\n" ); +* exit( 1 ); +* } +* +* // Get the PRNG seed: +* uint32_t *seed2 = (uint32_t *)malloc( sizeof( uint32_t )*seed_length ); +* if ( seed2 == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* status = stdlib_base_random_tinymt32_seed( obj, seed2 ); +* if ( status != 0 ) { +* fprintf( stderr, "Error encountered when attempting to retrieve the PRNG seed.\n" ); +* exit( 1 ); +* } +* +* // Use the seed to, e.g., create another PRNG which will generate the same sequence... +* +* // Free allocated memory: +* stdlib_base_random_tinymt32_free( obj ); +* free( seed2 ); +*/ +int8_t stdlib_base_random_tinymt32_seed_length( const struct BasePRNGObject *obj, int64_t *len ) { + if ( obj == NULL || obj->prng != &tinymt32_prng ) { + return -1; + } + // Retrieve the state object: + const stdlib_base_random_tinymt32_state_t *state = (stdlib_base_random_tinymt32_state_t *)( obj->state ); + + // Set the seed length: + *len = (int64_t)( state->seed_length ); + + return 0; +} + +/** +* Returns a PRNG seed. +* +* ## Notes +* +* - The function returns `-1` if unable to resolve a PRNG seed and `0` otherwise. +* +* @param obj PRNG object +* @param seed output address +* @return status code +* +* @example +* #include "stdlib/random/base/tinymt32.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Define a PRNG seed: +* uint32_t seed1[] = { 12345 }; +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed1, 1 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* // ... +* +* // Get the seed length: +* int64_t seed_length; +* int8_t status = stdlib_base_random_tinymt32_seed_length( obj, &seed_length ); +* if ( status != 0 ) { +* fprintf( stderr, "Error encountered when attempting to retrieve the PRNG seed length.\n" ); +* exit( 1 ); +* } +* +* // Get the PRNG seed: +* uint32_t *seed2 = (uint32_t *)malloc( sizeof( uint32_t )*seed_length ); +* if ( seed2 == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* status = stdlib_base_random_tinymt32_seed( obj, seed2 ); +* if ( status != 0 ) { +* fprintf( stderr, "Error encountered when attempting to retrieve the PRNG seed.\n" ); +* exit( 1 ); +* } +* +* // Use the seed to, e.g., create another PRNG which will generate the same sequence... +* +* // Free allocated memory: +* stdlib_base_random_tinymt32_free( obj ); +* free( seed2 ); +*/ +int8_t stdlib_base_random_tinymt32_seed( const struct BasePRNGObject *obj, uint32_t *seed ) { + if ( obj == NULL || obj->prng != &tinymt32_prng ) { + return -1; + } + // Retrieve the state object: + const stdlib_base_random_tinymt32_state_t *state = (stdlib_base_random_tinymt32_state_t *)( obj->state ); + + // Copy the seed array: + memcpy( seed, state->seed, sizeof( uint32_t )*(state->seed_length) ); + + return 0; +} + +/** +* Returns a **copy** of the current PRNG state. +* +* ## Notes +* +* - The user is responsible for freeing the allocated memory. +* +* @param obj PRNG object +* @return pointer to a copy of the PRNG's internal state or, if unable to allocate memory, a null pointer +* +* @example +* #include "stdlib/random/base/tinymt32.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Define a PRNG seed: +* uint32_t seed[] = { 12345 }; +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* stdlib_base_random_tinymt32_state_t *state = (stdlib_base_random_tinymt32_state_t *)stdlib_base_random_tinymt32_state( obj ); +* if ( state == NULL ) { +* fprintf( stderr, "Unable to retrieve PRNG state.\n" ); +* exit( 1 ); +* } +* +* // Use the captured state to, e.g., sync another PRNG or to reset a PRNG to a particular state in order to "replay" generated values at a later point in time... +* +* // Free allocated memory: +* stdlib_base_random_tinymt32_free( obj ); +* +* free( state->seed ); +* free( state ); +*/ +void * stdlib_base_random_tinymt32_state( const struct BasePRNGObject *obj ) { + const stdlib_base_random_tinymt32_state_t *so; + stdlib_base_random_tinymt32_state_t *state; + uint64_t nbytes; + uint32_t *seed; + if ( obj == NULL || obj->prng != &tinymt32_prng ) { + return NULL; + } + state = (stdlib_base_random_tinymt32_state_t *)malloc( obj->prng->state_size ); + if ( state == NULL ) { + return NULL; + } + // Copy the state + memcpy( state, obj->state, obj->prng->state_size ); + so = (stdlib_base_random_tinymt32_state_t *)( obj->state ); + + // Now we can safely use seed_length + nbytes = sizeof( uint32_t ) * (state->seed_length); + seed = (uint32_t *)malloc( nbytes ); + if ( seed == NULL ) { + free( state ); // prevent memory leaks + return NULL; + } + + // Explicitly copy the seed array to prevent external mutation: + memcpy( seed, so->seed, nbytes ); + state->seed = seed; + + return (void *)state; +} + +/** +* Sets the PRNG state. +* +* ## Notes +* +* - The function returns `-1` if unable to set a PRNG state and `0` otherwise. +* +* @param obj PRNG object +* @param state state +* @return status code +* +* @example +* #include "stdlib/random/base/tinymt32.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Define a PRNG seed: +* uint32_t seed[] = { 12345 }; +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_tinymt32_allocate( seed, 1 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* uint64_t r; +* int8_t status = obj->prng->next( obj, &r ); +* if ( status != 0 ) { +* fprintf( stderr, "Unexpected result.\n" ); +* exit( 1 ); +* } +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* // Retrieve the current PRNG state... +* stdlib_base_random_tinymt32_state_t *state = (stdlib_base_random_tinymt32_state_t *)stdlib_base_random_tinymt32_state( obj ); +* if ( state == NULL ) { +* fprintf( stderr, "Error encountered when attempting to retrieve PRNG state.\n" ); +* exit( 1 ); +* } +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* // Reset the PRNG to a previous state... +* status = stdlib_base_random_tinymt32_set( obj, (void *)state ); +* if ( status != 0 ) { +* fprintf( stderr, "Error encountered when attempting to set PRNG state.\n" ); +* exit( 1 ); +* } +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* status = obj->prng->next( obj, &r ); +* +* // ... +* +* // Free allocated memory: +* stdlib_base_random_tinymt32_free( obj ); +* +* free( state->seed ); +* free( state ); +*/ +int8_t stdlib_base_random_tinymt32_set( struct BasePRNGObject *obj, const void *state ) { + stdlib_base_random_tinymt32_state_t *vstate; + uint64_t nbytes; + uint32_t *seed; + if ( obj == NULL || state == NULL || obj->prng != &tinymt32_prng ) { + return -1; + } + // Copy the provided seed array: + vstate = ( stdlib_base_random_tinymt32_state_t *)state; + nbytes = sizeof( uint32_t ) * ( vstate->seed_length ); + seed = (uint32_t *)malloc( nbytes ); + if ( seed == NULL ) { + return -1; + } + memcpy( seed, vstate->seed, nbytes ); + + // Retrieve the current PRNG state: + vstate = ( stdlib_base_random_tinymt32_state_t *)( obj->state ); + + // Free the memory held by the current seed array: + free( vstate->seed ); + + // Overwrite the current state with the provided state: + memcpy( vstate, state, obj->prng->state_size ); + + // Update the seed array pointer: + vstate->seed = seed; + + return 0; +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/Makefile b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/Makefile new file mode 100644 index 000000000000..5e8143aaa644 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/Makefile @@ -0,0 +1,134 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2025 The Stdlib Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +endif + +# Determine the OS: +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate [position independent code][1]: +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of C sources: +c_sources := tinymt32.c + +# List of C targets: +c_targets := runner.out + + +# RULES # + +#/ +# Compiles C source files. +# +# @param {string} [C_COMPILER] - C compiler +# @param {string} [CFLAGS] - C compiler flags +# @param {(string|void)} [fPIC] - flag indicating whether to generate position independent code +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler +# @param {string} CFLAGS - C compiler flags +# @param {(string|void)} fPIC - flag indicating whether to generate position independent code +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $(c_sources) $< -lm + +#/ +# Generates test fixtures. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean + +#/ +# Removes generated test fixtures. +# +# @example +# make clean-fixtures +#/ +clean-fixtures: + $(QUIET) -rm -f *.json + +.PHONY: clean-fixtures diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/float64_array_seed.json b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/float64_array_seed.json new file mode 100644 index 000000000000..b1a9a06507f2 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/float64_array_seed.json @@ -0,0 +1 @@ +{"expected":[0.74764781988264306,0.32454978568883919,0.99065350103458572,0.73641454781779825,0.55966713617760855,0.96253943180233936,0.1850883527146413,0.29165958353363686,0.45995962581294336,0.64190806303980996,0.35985571166011232,0.027728273862237307,0.99353365956960626,0.026123488552754259,0.4097085162140689,0.40935636924472163,0.58609913393201118,0.11136086059829486,0.87548463506832574,0.46736198848132204,0.35537704632967138,0.0067980758596167812,0.50287145295249891,0.18272600541361927,0.52501717829509231,0.040860010851226036,0.62618204633729202,0.30467166145776925,0.77012014043926591,0.75135221101292571,0.64077927817331859,0.1597420748892846,0.42404501619128321,0.29770699841568948,0.26096010773226752,0.64759051391970757,0.5505445139517624,0.067284663954374491,0.6726831240002592,0.69046642368789657,0.42364330876156431,0.565230715693401,0.34883213855342388,0.82275439885306878,0.64973946064351984,0.31018547941030783,0.55446255924330912,0.28709050515963019,0.73075948001262558,0.66719942425842205,0.71459249773972422,0.08081724040189131,0.44961815893404766,0.73844112333688749,0.024728356669208806,0.76591470182307164,0.2906856234378109,0.39636639162989096,0.70826494859499856,0.87707655523284944,0.16658307567046626,0.27494887177877003,0.94445831312851891,0.7021987174312293,0.81130538190966894,0.76029492866142034,0.99439293243152693,0.41263104171326637,0.67157124519403666,0.93298554100088005,0.1898155814937732,0.18047595302701425,0.74491758587461299,0.74679550654719362,0.09579356752788426,0.71539151917411004,0.84826871983632335,0.75837474728513687,0.29037009713129458,0.28869109095184098,0.92675958681804949,0.2196978339842165,0.83913023719907864,0.059150788654645847,0.96674121918080125,0.48967761484355055,0.99958544585090692,0.94373631877780162,0.55967318409247324,0.114349148307205,0.5907145530506297,0.78903346245369133,0.63393133237580013,0.24811132572492234,0.37539793748996153,0.62600931682182626,0.63714765401633189,0.41076860313974572,0.053494038983175174,0.96576163498629031,0.70811302649429508,0.27369105853994846,0.13854337654969062,0.88867872569806217,0.51491109032287485,0.025308727849475465,0.12533076995616244,0.93223867411198358,0.19758296443625778,0.45888621119379491,0.84361184126423572,0.6040122150645616,0.45616344373310114,0.45383360775606585,0.80017329887450861,0.24463946148245375,0.083530102163879127,0.93023142913767076,0.71106506558510185,0.67986250387010849,0.57224939086908733,0.74681780509806195,0.7995470321683984,0.32923842968765116,0.75974970534716368,0.28769954918644347,0.30527062916171044,0.53792838125072129,0.34227394270724787,0.63884651492078959,0.12365834407068255,0.2118393846551847,0.47401097392617719,0.013526465529399623,0.9562920948500081,0.3506430058046307,0.61752164317311764,0.62509898662641739,0.29426822372778183,0.30799329845953072,0.45806400802290004,0.27650215519219612,0.11537528593051571,0.21469817370734812,0.99280757156382504,0.24596350376717424,0.84718531598847091,0.95751370206461772,0.064748358940139084,0.93849862761819725,0.35904449622443024,0.28194667462081491,0.43909349120306751,0.97295272394905519,0.84480664983039455,0.8390914301558009,0.38967688451297744,0.38083318749433692,0.061811369608674172,0.42715439256754995,0.19680237198454908,0.85364795931150228,0.34098189576073523,0.47396076795161612,0.18491804170438308,0.52217002739094298,0.66984621604842209,0.070412833037978406,0.42633290455743011,0.28489178683520755,0.1380906714942296,0.32514133854006977,0.42728229981435539,0.15591191002163107,0.40232355713349099,0.56389278129939324,0.89937969312827604,0.40236043996341753,0.017507962988834502,0.78620526354670217,0.27929003954389831,0.10861725030732816,0.74659744986278342,0.51198016888672027,0.64468367648361513,0.83152169787419139,0.15388434323078981,0.61564958537234971,0.91001625210505155,0.30853884356673233,0.57647049168549069,0.43889556004946284,0.96414753287967947,0.26422261317471063,0.49576388722300724,0.84861002151739462,0.51538562771144514,0.1422420136830993,0.55490808680996762,0.37259201414781806,0.23607333019929722,0.65222175535725735,0.61349501923220595,0.00083754467979002545,0.88911931631480734,0.6368198119417563,0.6251408670651295,0.0068821033216408534,0.86405182683831339,0.99663236785792186,0.81315777868374173,0.20402222532319392,0.56947385648680815,0.66899438980569292,0.38302877138283742,0.30109445903678378,0.56130207774022811,0.56737323269752538,0.77110892510583895,0.54782826309673116,0.49431158499556804,0.71096439141838919,0.60991000043495025,0.24075275024296994,0.49533329468383946,0.57481043245746721,0.18926010179800756,0.30366594407044978,0.5562254954736745,0.55008620993788437,0.84859089796107934,0.15418840163295899,0.25883495600423045,0.053814996878207899,0.092399384674552665,0.63656293523158314,0.99772842781464843,0.65267993128040025,0.66608939998761685,0.25023671768456768,0.66917994618475485,0.035206673918180775,0.29670204893814978,0.28096184059278939,0.3941634600359456,0.93508163869316308,0.56955486648527387,0.66451868279301318,0.82481898840347478,0.34308138738197991,0.52372254126658524,0.78376303485541499,0.03741411360157787,0.60634965808106256,0.24565961029768291,0.49268597117167345,0.70626333337587277,0.19800766729918984,0.076402032092181327,0.77266932018552115,0.28835010320521959,0.74354520832465987,0.3665933805080509,0.75113602230955545,0.7736823546262579,0.3711406336057782,0.63336868482489073,0.32876437605251341,0.061255124708284425,0.8386928817343251,0.86944288402692582,0.31860664633791225,0.70388449803640252,0.54397791843472321,0.3462872020761727,0.03873681906941373,0.039898137291430102,0.68711140827880945,0.075084761949194223,0.081517430195456986,0.60342739516129684,0.33894842148466386,0.43201251884338066,0.14273457721285854,0.12287620135857413,0.78055135556975719,0.46876761776468479,0.27582589044465511,0.59292930534581156,0.41843175907520835,0.42733298976655276,0.11937812701712092,0.53283351767701315,0.97713447209079995,0.15593348379994743,0.034335812257203835,0.12148990025298223,0.096932513117882202,0.18967021520760829,0.85007670076449926,0.7122423317108374,0.080008062619082776,0.94707893092614592,0.36227989162438268,0.40354886035268545,0.19725748513825814,0.81080987399553461,0.18227905651689347,0.034030913755005221,0.46154334592688184,0.55930482165595696,0.35694168456048414,0.22334495993565329,0.40746899940134063,0.77040692699990254,0.095603817846406813,0.099932547775147818,0.40330769908563324,0.18674954460555004,0.86482187724645709,0.59926965146105893,0.30706519167923507,0.70005587259422297,0.94307180163131676,0.32717490337449895,0.95458371730431935,0.76123669782167347,0.0055021696223549466,0.31328241714236016,0.92941561533210937,0.1126592691788918,0.96315559161288333,0.697291263723823,0.0029079025630543986,0.56495774159740486,0.5544998292309925,0.80235825917679282,0.78120714827538329,0.76917571900642301,0.25854204597928876,0.98785635221219115,0.29152745567802107,0.6795917673930667,0.56072230364469955,0.67487986638927489,0.71111794845707688,0.7860983693628506,0.5834449406806711,0.02426300654143565,0.62729130401933886,0.8391695789046717,0.86441164873775955,0.089515547121181482,0.31303004699056525,0.1785298522104688,0.95939026651297976,0.88740961992105616,0.30578093463740508,0.15180792776690433,0.32018658791150612,0.31141633207916752,0.85939627019601117,0.98556066540360454,0.21550232600486585,0.30259674040822437,0.52780397639254995,0.54935133402816438,0.97795058832973569,0.7392646464646867,0.48820970587848789,0.088816839432883765,0.65493156837084188,0.066424149319019077,0.61751969455509625,0.93982595892322851,0.28870803930645816,0.32750789918788448,0.99396445936698863,0.90523469940446233,0.12633106059872368,0.85231840349118881,0.30152665010881863,0.91555711485111735,0.91294876722819041,0.089481668381120705,0.010551262754794233,0.73544280202044987,0.99486367785705232,0.83247224326037628,0.22872743172699483,0.14714653248834753,0.72035550682925842,0.54754672045924957,0.20675148030838031,0.054663416125458109,0.7286710329650945,0.62523229175211814,0.35051539854738978,0.96368398849625392,0.15092218815742808,0.54058742974359797,0.7682331976568515,0.31542236969520943,0.59504540618251567,0.84512167170212082,0.37967821745275077,0.72552002805513216,0.23213224578634151,0.95276801391743882,0.41305363271347795,0.92045805290584326,0.72391318447444442,0.60849873851785441,0.10816456388876483,0.85380597631101796,0.90616331649012016,0.10489196608297657,0.94537693134098033,0.060023468259067236,0.58789942137749573,0.20282341672010451,0.40271666770542924,0.610427715571222,0.82653886772913643,0.10865607117908627,0.9077344093027101,0.34905685691432498,0.0061253493782356117,0.28464962163904939,0.048183025688900982,0.61448487507403793,0.50838245222270395,0.0040430050817017449,0.06198075379919421,0.17790433418345364,0.17495068104398848,0.26240350347986097,0.99707771886840335,0.32945394501512026,0.29262720778792239,0.9860378272148449,0.86696757170669658,0.56291245021198988,0.79008690705658069,0.67585718124244976,0.92380500136346011,0.58262224365005788,0.24391273370864341,0.086183184242174082,0.15691965074204661,0.44075792829115501,0.11028192787447622,0.11228791032795038,0.73568479330318881,0.39044724154989208,0.56970976525581885,0.25097553758024549,0.14169491644254961,0.036957740990879784,0.87618462081988191,0.27469684471793898,0.58202090787663807,0.56356856866761607,0.083132572055992671,0.24989217397147867,0.70299811122187761,0.42061228863069289,0.47320171362961871,0.80094207310636556,0.23399482224418922,0.03664976365396222,0.86681259409938505,0.12225239918947506,0.32201425100848935,0.31147823143121578,0.22769240282354941,0.094601390008918429,0.49697881794278653,0.69294615988126307,0.48421253132003494,0.11386527371094579,0.14030144802788647,0.5231301151052854,0.65050286138834024,0.8051762754041728,0.81387268709354843,0.89835995466048424,0.74837829472460993,0.26030451236129271,0.79291347035051551,0.89177689726608356,0.23642377941302328,0.77398960680160767,0.34293311980831465,0.95133521099473051,0.44903747307528241,0.46092413539697275,0.5651245400235968,0.24770851975606956,0.035040153363729121,0.070986924354665359,0.71501064261588609,0.67614859131472338,0.2784745014557074,0.60006790259805265,0.035197124297454252,0.41080184395338015,0.12786362644853333,0.53640203766056138,0.19127602494073059,0.056048077306841138,0.086290760458581417,0.71616878106873039,0.0705354392775982,0.46312218207046441,0.91378051462682441,0.66850117872347503,0.47809586000851001,0.71085667419560583,0.22919265539301881,0.8930109374385965,0.48694427432847764,0.34363740493576367,0.75947990996045622,0.40889997448164384,0.080954831329798527,0.5797789734056269,0.64198839314282252,0.97394094648858187,0.98226060061357623,0.043721377641432246,0.080485295732891537,0.015386225768194928,0.28758860911511053,0.40748779474633579,0.19422153594650704,0.70553062079608364,0.061118979892882841,0.77159698369739971,0.79627445117010665,0.76857856070909658,0.16192752069383254,0.29541717015594271,0.80102218251360136,0.82798523843026783,0.41850130829241283,0.32629454164274019,0.019501594726245797,0.60542975801923415,0.87535952622669599,0.26102877529347823,0.011506341444060708,0.44844032998133454,0.35465763833907082,0.15878029280632744,0.19593135985179522,0.35517022498871476,0.49440061240750666,0.34393596790560821,0.56543643483972394,0.13383367576394933,0.57021255212777633,0.68917191538107792,0.010659203759741609,0.29480271687331661,0.92677727830104017,0.027738241253024265,0.099309083229207951,0.19629763116549581,0.77945192462442836,0.53221960180226968,0.45245997506650582,0.49268085612454249,0.70003581154652239,0.77271348102788617,0.16750068119575956,0.023803962360899211,0.4556924196087323,0.36785559000156787,0.019157005164716856,0.94874935280121742,0.098442091722960812,0.34961168405142096,0.47033398107149715,0.45394759008612828,0.43865800175503689,0.04669236353073547,0.58076886979912679,0.72888330312362393,0.25790313123952679,0.63866480153893701,0.7502011929849145,0.30569344723621861,0.329305974577356,0.043297619691772371,0.18000469630960036,0.3527933732352434,0.50649609681101526,0.093386262059156011,0.90268917464426313,0.83774641045526188,0.52350944350894846,0.71510651363309852,0.01327353189131697,0.59522546042938596,0.70704194256597064,0.36467114774074549,0.80177611797478909,0.40440391378855867,0.83659650206115355,0.15550457055401279,0.95605234598620592,0.61801057508610591,0.49893672132782296,0.76507434317455636,0.74033935600766654,0.40240250031064739,0.94860826865360426,0.39958639368691129,0.93776606462741818,0.63052907191220842,0.71151683392264253,0.47501487367862227,0.19846922642011233,0.75497424053951256,0.75459664164087348,0.017804340481793579,0.91804271271056981,0.79889752479590126,0.55235182823104756,0.91404545871009968,0.59387188741626595,0.4966480011597848,0.18869406604105143,0.96023257694067088,0.36769208308685231,0.19449010564263003,0.74658710859060662,0.14639846583296101,0.17679681601888642,0.087409682387271204,0.56217208044741918,0.876071560807551,0.043454443438478485,0.40139157773810463,0.46645401294001487,0.039627097460096627,0.72106720546531711,0.79132621394096814,0.95585219207363914,0.034331349103603914,0.33211838236859081,0.40818569180319886,0.51372437514476887,0.43639794439181434,0.29770649628446744,0.96682836801835681,0.076637396751042419,0.3467861861697813,0.17541054332380668,0.31384416048262676,0.11546188042474415,0.74093451262851562,0.95945913273968275,0.77802434236998408,0.11860012830922428,0.60039634070196457,0.044876322097625154,0.11346723115459001,0.82277207514154393,0.052870978522398415,0.35120509277308509,0.55588614133208658,0.39837052117753535,0.80139715616044915,0.32740976420697909,0.76888624210793621,0.60751702833560384,0.36471525094516399,0.24229556533713303,0.82189235229628477,0.53040120004311142,0.74262234213867895,0.32996231235642393,0.32839994711459974,0.91209192838269448,0.19960859325424041,0.57029746244473978,0.90153479602660891,0.78330893687420799,0.37516099229107414,0.73588321469208651,0.45613334259444382,0.5814092561285108,0.71097215345662834,0.62222592518823538,0.44402721192045624,0.32140136074088088,0.98154667493869829,0.20614411453783188,0.68473479752962918,0.67679438592278873,0.4322816108130012,0.017710036599303813,0.56092019386472181,0.55068302900487975,0.78524211520507692,0.11247502081732474,0.047025236507166945,0.030820108458662565,0.22680462761846643,0.65234888753706455,0.45670051027506808,0.11622666490365119,0.0058667349371764876,0.099367272034148191,0.55517876406965649,0.60270547985967016,0.54153793998167998,0.75908512928857763,0.61735262003664804,0.35935945517255607,0.68380535211365689,0.93688354049377331,0.13113474317157015,0.42372011803445875,0.96106216131506406,0.87135372016218315,0.11365890080872021,0.77153396900879412,0.39441572948936254,0.65976051604051178,0.58364065168479995,0.092965595858617855,0.79897042801696749,0.52998649692990729,0.56451224892831287,0.2339250915704737,0.98768552881954474,0.27498092788947226,0.53717150127078905,0.74842064373828743,0.55001915984860539,0.25936576011810852,0.61392334731754805,0.60652631545671365,0.78906417367458248,0.99519207607222515,0.75925153224980413,0.32125549948951115,0.07915345572832011,0.16065088456534704,0.10167024265231628,0.65305585315970116,0.054634141648069456,0.54086384446500602,0.82190548725923485,0.714905125008651,0.03031132076062959,0.9961990362180092,0.482869722382544,0.87613834283725067,0.41833586682768709,0.43045550587840686,0.53837545257515595,0.51011485075200513,0.21422972370438575,0.78498696205433871,0.6293008091804424,0.25282085223294137,0.6013563416978398,0.92326127407523262,0.66017086820877491,0.67215792494612514,0.74023757238520871,0.17785589727375239,0.51063752790667893,0.085622164012651436,0.42992850700438734,0.20834285917655493,0.64642266408971905,0.52128130439224662,0.60627596882453405,0.25131221722465591,0.77611235188293326,0.54470992292784048,0.67017234135551984,0.950849652042697,0.043907322093812651,0.53508371153385059,0.47292585009316535,0.63995139653499988,0.20116792296887509,0.75145769024824716,0.71566380430965959,0.59517655887558718,0.8197356423756742,0.61816184001071062,0.87407764599859405,0.1146477486656039,0.61582930494350441,0.50074073952548703,0.41226353397830173,0.27651454518016383,0.11116642703024837,0.58864363887845661,0.90687180673906931,0.63163536238463891,0.25968716406957448,0.65043112052823271,0.097515953747292272,0.34905474420582527,0.59114189753071533,0.66312131591700119,0.97132631184371909,0.64897446244584633,0.68197488002869433,0.41270781801657908,0.28705236741395623,0.32072173175543506,0.4299316784851881,0.056883939866662958,0.33603708270797017,0.31745399564550503,0.8535715362632148,0.033865860617936394,0.62594999195700496,0.85194169346975113,0.83936935377144561,0.033020868366431033,0.36859645040425726,0.70350963395526478,0.90690048077110352,0.33716724057320291,0.64980976622666908,0.48776876102262878,0.74548240537817012,0.28661557682086425,0.36864754475176131,0.91315983514807175,0.14911775613847267,0.36316214530201962,0.17436246955199475,0.46255189055591894,0.78478083980349989,0.7615950084373373,0.53750323416295964,0.49352152200039523,0.15971521031884872,0.023681578077126564,0.6161652789323917,0.70373240378440038,0.78706326862313047,0.79919081041883477,0.70064526229071722,0.048386649713968,0.66158396768190353,0.37701096967798797,0.036990751770942687,0.15292548681028384,0.22817336287684864,0.75980896685254062,0.35952766867544983,0.89975528279506589,0.22432011001327312,0.94669602488904581,0.966875887267727,0.65337554827374866,0.36995855585768955,0.78277774605706851,0.53760404850507937,0.84674701263651619,0.42523717016957341,0.19111278516243047,0.093099624845248696,0.501152927892483,0.86873468077155069,0.92383667974756167,0.0039380336330134824,0.67793938398909359,0.61489751289639749,0.018126617146940638,0.82029445298964987,0.77550460173497271,0.47143058795634729,0.21681997698837618,0.53201171393843383,0.80885836980843817,0.023267497383524405,0.73470395086869056,0.24157008538093594,0.055685796778560892,0.89316004649669778,0.2497453534224563,0.15867301087892227,0.42654611027369471,0.4233419985894048,0.97272667621333464,0.40205129433425257,0.42070747197064051,0.29244337767203332,0.39674917797484421,0.19625827360695169,0.86140022778692649,0.79057509738991349,0.86210301416864965,0.31550694744205598,0.5443246062862781,0.69906669695739732,0.53083052861600477,0.86274694054821643,0.15243170917410442,0.60886972691633356,0.86097144994049823,0.5909719347156227,0.268800154641893,0.46726831500996191,0.2103989884791132,0.63096553691066393,0.64173374930133797,0.20119203457697565,0.16252572875022608,0.454654297644279,0.90479563055623724,0.060423891798524698,0.085243232826462068,0.020675575566056259,0.51088491448029882,0.60097057074980709,0.7370334607132375,0.92547934576556801,0.75279568935126084,0.95209096355340128,0.86507907825762165,0.18082482260607802,0.61175601558491466,0.82534043118155609,0.72007134168148612,0.71910196838651108,0.089191483781107306,0.80370185966313812,0.4238225138599051,0.1700828973306997,0.70967960739168112,0.24863763764574098,0.1710633218558606,0.31189581691193791,0.16075582459350346,0.51252842855097691,0.51967016787120091,0.44018885434678068,0.16004215563199209,0.49556398054920137,0.10800862851169746,0.80288522423604658,0.48254892668682103,0.59999117033821769,0.90933930129052243,0.56945601387441147,0.42110138910874972,0.10715559906965899,0.47914241100325605,0.55681289073232798,0.050377056252913333,0.40876037548878785,0.41178456350390724,0.25116439783172584,0.55446225277264938,0.92220369470264918,0.67070652471082115,0.30286516391155582,0.49099463995852133,0.41315922969821495,0.086381601414076581,0.56033750840403151,0.84414287007770272,0.83573208266808996,0.93427400465390409,0.83935047834568144,0.58665210059078576,0.74886479646705006,0.51206696454707912,0.9437456609468422,0.54389613943505355,0.014236384296593574,0.34936013180827197,0.1969368950443271,0.25894369001384809,0.51362505739951081,0.54435689003895205,0.46002336998571292,0.17567736569847281,0.17366094727601977,0.77887408522711377,0.99921296721802411,0.29275967391475999,0.43169547902463434,0.25238682393163014,0.66270009681037578,0.77434792279682108]} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/float64_integer_seed.json b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/float64_integer_seed.json new file mode 100644 index 000000000000..995dda104739 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/float64_integer_seed.json @@ -0,0 +1 @@ +{"expected":[0.62467647240664403,0.040113734316677641,0.45419934732459766,0.30754255956501031,0.69078685236128112,0.46223182179767175,0.38815354067371299,0.45862698667194512,0.060546778930606893,0.98169185260265579,0.43344495693676066,0.21868748545970529,0.7150649914523407,0.6443079195412601,0.013552925887702738,0.020152792137804298,0.18547557055288388,0.46014484064974281,0.85801024946296678,0.10033349976944561,0.27962134679078399,0.079688824135026981,0.11092994423335167,0.89020126783655507,0.65796253351789746,0.62219973070295753,0.62350411175182874,0.00033934492754073009,0.64174387056982929,0.7877830698669932,0.42578988142161001,0.93923661605897923,0.45761680331930588,0.51352982983930417,0.58294327186941197,0.51724899032365745,0.12628968908087546,0.88358709201123375,0.38986681820124069,0.41887578654104618,0.57370373683101883,0.95193416488260474,0.026846506699824135,0.31358193557697855,0.88069815191602463,0.79912175702015797,0.4993395384079512,0.10240885690024437,0.90745363856461114,0.53982971795780865,0.3572074392457405,0.28143835105616211,0.40418939170769963,0.50819869823116592,0.76151232628427312,0.16585126679714934,0.16510864767981659,0.0095440315313609636,0.29948087432197545,0.98267353160849202,0.26162271194710995,0.56064909300682886,0.0896852131925161,0.072093244421821745,0.39537792350375534,0.63113729374017724,0.78499312477314975,0.16893470016408185,0.43164063195603419,0.80753366780084612,0.89723978945575056,0.1618814058630309,0.34742042973694198,0.79230558084264679,0.13573571903239723,0.80048393997316469,0.60371767467963189,0.41196045907244794,0.28315238794533237,0.90805700023624791,0.26525051971290459,0.81730974005399948,0.71715059783120949,0.54582428127105653,0.9692259404930812,0.4244680275742726,0.76504190453165966,0.73183991480670396,0.82228589205235891,0.87067291247659429,0.26518279421632662,0.53822582514861972,0.08083940236330045,0.73679242064156247,0.51239147082863556,0.40185097256282176,0.10646014161990602,0.62837761499601752,0.55089548035842795,0.55729676206849721,0.84520754763885098,0.72490930764341122,0.61672483875352246,0.66412148803756754,0.37607963644045639,0.12163474002532282,0.60469224787305165,0.79341091806913211,0.94950439702931644,0.45254337352975504,0.23189191880623705,0.72415016356612516,0.74033775302827531,0.71253680570503253,0.8667502395522757,0.85040113488145375,0.74730105944896119,0.12335359052150896,0.90625973328641352,0.40485584647529138,0.085614394232334301,0.021418450393813626,0.74364928012610665,0.65647942672624748,0.21123293838742552,0.073258124398804192,0.1274121560754703,0.62431949687435462,0.33886260492154996,0.87802543829453938,0.46616635751237689,0.33190188744597993,0.0935602106815423,0.86889881510600586,0.6096579864672429,0.65147144145939051,0.953076513549176,0.19345521244431796,0.0024227695116998405,0.35703034288425761,0.2030409931985846,0.61451855980490233,0.33220341932063258,0.38159617863111406,0.16264215564866391,0.96333528470999386,0.86088687259667118,0.25163815550159918,0.46548487620770262,0.52685276094784972,0.30954931378111816,0.1830395403205648,0.92453949264062063,0.81408979741578857,0.57117371973335329,0.86823179645910276,0.098152352938387422,0.95662676517536627,0.39285682921137033,0.88800276047156212,0.55422586522500839,0.50609678897586607,0.42121228309295178,0.48208144487812588,0.82507461210911781,0.78781410747790392,0.74553660433111701,0.27101576883315459,0.83523852946430788,0.33568933385891919,0.23595760055746207,0.060618406886123855,0.86915102216037177,0.42465947812271532,0.30550270463837126,0.32973132807837258,0.9397692347533585,0.30684276593684889,0.51274894074551391,0.022746415019763822,0.2834176593867278,0.063556749657267098,0.64307389165201545,0.56166184649771445,0.77481583906809914,0.43152257862050947,0.361741319650421,0.98836537911908195,0.10731872134583853,0.545548026072599,0.7389830847314901,0.14811232205686431,0.96372573669427031,0.53951489795934615,0.84339659990479676,0.59852204233469986,0.46187848722808533,0.36168273459042821,0.24065157984454177,0.71907099165469313,0.4029887881360642,0.22425635992023596,0.30481039845537838,0.69769591190890745,0.27285343198950895,0.29455316510578933,0.1163183872031277,0.97413897481811385,0.87330668427375091,0.87853492529797428,0.77508674141411305,0.89471603186309934,0.92167399847920817,0.065100212235820409,0.3346947876882731,0.35134606192038753,0.52719491679076247,0.65482171177787596,0.60543016359872825,0.45054923438518191,0.45782116026577446,0.77714572774655233,0.99701876165068326,0.086797368430756805,0.16892087887483487,0.14751436069025792,0.62445085938624545,0.95877461063165492,0.12336417564100355,0.71380067448031115,0.22914879269876975,0.20721593865540822,0.50273515897045207,0.23712504823076597,0.91683273445078861,0.66638907332567021,0.44864962694668087,0.97672039446223513,0.96235572036038497,0.46796552419466175,0.3885165384646514,0.30323478398799208,0.18960224580990015,0.9320745253910514,0.9511038703627267,0.73791954914550306,0.35224164912565503,0.051946934618085083,0.76783448243972796,0.46920135054362755,0.6813942290045375,0.37660917524792137,0.32671326366451425,0.97217866846606871,0.48053403520473881,0.13641264532085862,0.39152493146281131,0.14351880060947941,0.30039174496757337,0.34536711427279776,0.2123489108281168,0.14354918413340056,0.89455526077416292,0.50644421428131703,0.29765169970068472,0.13418197087519335,0.19308184610242718,0.61436060786287716,0.85380406873380965,0.88087947979045844,0.38376511109003542,0.3043253234118849,0.4603006371245244,0.38646838230536229,0.031814911363494014,0.040321563745754241,0.048752353120239955,0.86185593295848228,0.93127023471431214,0.35861808097519743,0.7217157817146046,0.76521437844418194,0.28470474530602852,0.8254142233668661,0.61485084483508268,0.57485562686256686,0.54473071030538722,0.88418423961121473,0.85869402723631005,0.44103398011706874,0.53350471291609836,0.92633360852482038,0.26768072880899996,0.30509430620128641,0.078958617721121338,0.36346208964065274,0.22554264274786195,0.9765952184916421,0.40158854713117942,0.29983681375767057,0.96153258362114258,0.30738070918099525,0.87841214249044641,0.80765877287267107,0.20474089777631688,0.68268648956563771,0.88990497404580282,0.17953579936735986,0.20878589210785703,0.74492520988915689,0.57621298076690453,0.35758698640320774,0.22361054560716187,0.6161572157582027,0.78429423424959654,0.075033849388128737,0.30780845824042691,0.95988698923508464,0.10229135651762511,0.4959057955023618,0.57590956508884272,0.43773346271621039,0.59704298868612948,0.41363225486116673,0.68505238616295294,0.37869461058222365,0.12708147117918389,0.2718912907258183,0.85977082524654014,0.12139454281827222,0.91820386442728863,0.45422935047374424,0.69825402460575858,0.86356531293458882,0.95674522978751797,0.1901502743824548,0.12347187771055024,0.46972188835516882,0.24055295451107939,0.30391898151358487,0.96219864358284513,0.52912070740355721,0.15670794647603337,0.90181720551077593,0.13715237307423134,0.70822497425681241,0.5023002588431692,0.27797626899778782,0.72436974373271812,0.53462313240964821,0.16002104482754786,0.92871962061284252,0.98984293403297274,0.14322391156087011,0.29019643400907591,0.1156734898809948,0.13226249311185967,0.083732078894065753,0.98539344179934041,0.58324380662235142,0.14157669186601007,0.22644313585505949,0.50567289137571247,0.57246588970915102,0.022155039911626262,0.39636305789506676,0.92572231358355195,0.18270676408136555,0.55257222470576517,0.73732626049153682,0.17352468261974174,0.29027578568769552,0.77596789557910206,0.64568214646870614,0.22155795761084163,0.58521316546017765,0.37664722363943592,0.84058552003297782,0.27544050150751931,0.30638522761641629,0.67076671717944347,0.72099516090718863,0.13030805091015518,0.58747247096998267,0.9581811749356286,0.72483866802559871,0.56832486683898398,0.89859311664281949,0.53651875043869734,0.85201642591093385,0.34580785908400746,0.21894601244605205,0.89795902683304751,0.0041222062730237319,0.74530339417034441,0.877897782799263,0.61973255308076325,0.069860178325626854,0.2718181070113902,0.80034530875439625,0.46955348035847821,0.16826080170353541,0.029567738543955113,0.0067268506557885965,0.87954276221818206,0.94505955498056538,0.27209969403187151,0.62140773250860348,0.55398699392313944,0.94948198329777989,0.56405559323714605,0.054846143186454754,0.58544407557769218,0.33839595677416012,0.094200957935389185,0.060992837629456176,0.43199566394769262,0.291600282064435,0.90947881517243456,0.99920128951390896,0.058819196352918768,0.3139746609134394,0.23443525539692345,0.40934247320422534,0.86880919545672863,0.7557685796327801,0.44262840499049771,0.66516108057806111,0.86240802996313837,0.96822226107526788,0.86182363829501252,0.38185016664769122,0.94869444909238831,0.45765279315570306,0.33005247725566522,0.6407669099054627,0.18683203092325018,0.091561215761309156,0.83157274802941694,0.55856357879218754,0.48778996294071386,0.014076626452348195,0.3875489002734398,0.43271830634567232,0.26173126530395141,0.091366331342456863,0.78602493634920712,0.96790650226182007,0.051754181171785429,0.77110088601016014,0.25145963855520226,0.26555339388826582,0.85735366039002936,0.97513239232105142,0.92264420467116581,0.77710786758287964,0.82101731441541803,0.9276392214514132,0.33298452277209512,0.12800506000913048,0.47415927537078173,0.22844081009788431,0.88399569497102459,0.029736090300578,0.91404206424232137,0.83557829430711783,0.40786039009434161,0.59034491275600443,0.43638315485328139,0.8379604364113622,0.57159204090956295,0.62810575600307372,0.0089550740573733911,0.84228202175666855,0.11593252517704411,0.1806683502875791,0.62285582954352947,0.13118678040190002,0.57704500818452842,0.89633809269451514,0.6708586185423896,0.99159803109278255,0.063868122868165123,0.44373646487536333,0.32310503543451852,0.85748752939880235,0.62890204068867839,0.55871753468206453,0.18049565236935017,0.053153132724889529,0.93011330161320471,0.18844900893568195,0.98900028914506011,0.58963387367180153,0.73315289901961256,0.58724163541182439,0.46355090912800645,0.48442467246756926,0.77634261254640813,0.31796444205707253,0.80273065695532975,0.54216412666502634,0.081967330837398045,0.71552501718905759,0.9359182319889956,0.70422076574344783,0.94004379912387359,0.20127345876971303,0.77177200601167462,0.39836047555974696,0.2088067838443628,0.94627062274136819,0.34872124848746555,0.7468301232649549,0.94471255024399581,0.88078998010807008,0.66320361996916777,0.61451372233028434,0.88749089690598426,0.64047996916508976,0.94151483074918529,0.68549287062597919,0.81561426843940044,0.37049979430078961,0.73634806758238491,0.65585577504388037,0.28770618031982365,0.46976167879456054,0.88009712792532424,0.72115311813069405,0.15458106383137249,0.12998492055834088,0.71674823929276288,0.787324025066789,0.022672129077232817,0.64464743181424911,0.79860208161508883,0.59667036543752372,0.68921660912178384,0.16747053618048047,0.79516662439659502,0.39228555880479621,0.1129150954440078,0.74835752219705409,0.22381682113728874,0.29479114509881754,0.37671071720208582,0.71433527038453848,0.55298729061915763,0.66050294408053212,0.9208629089814433,0.72259644174558968,0.19280971890093024,0.89927858255448578,0.21890778179388581,0.26473010360643623,0.96958347369610032,0.99251927905974913,0.3442621933598059,0.69187224566450733,0.36078861319809274,0.80637031578021867,0.70294210502248267,0.97118259093654091,0.42065473712246904,0.96408656536012383,0.37980366301331137,0.87238132244876687,0.87517216152846022,0.64135772427852022,0.3344483963781345,0.31380181050447287,0.74232541552037168,0.28155973037928894,0.96020721478638427,0.81870458899138721,0.77277099467537047,0.007235248396495475,0.71848764199719917,0.86349155435741454,0.61714664892618298,0.51563681917012094,0.83804502968104067,0.3718468008157102,0.66191877660459819,0.17007572615759803,0.7448035210075975,0.78294714149330635,0.72874250468733148,0.95997622637422209,0.88536068081612351,0.42781710768720249,0.31552221789663093,0.8129124984262569,0.0041410008906814788,0.14846386132929024,0.75058777595596471,0.49047236736068123,0.30243531735304774,0.33465711914717988,0.23740046315939811,0.64450094506732658,0.63131053758035172,0.039783170637703757,0.12971292493662967,0.72334525155925866,0.18618841229731786,0.25395525827621734,0.67522385763472526,0.3177148654389601,0.91808596021271072,0.82752295587978952,0.4666258551440764,0.25089699281057642,0.65795033245393986,0.54770726598225095,0.90436701160001409,0.55727359854415448,0.5151596430792692,0.0033973966992049132,0.46651250711482006,0.51095401956630693,0.57604559155698842,0.40425614593280967,0.40452196720848332,0.25013806245115067,0.59983734986582604,0.3837603070350013,0.84673529310358031,0.58877297731894052,0.3892172128010718,0.57094012799300686,0.51198206803656843,0.48774961747580858,0.1003438554840369,0.47192817052497393,0.20541329964657384,0.09093733778329105,0.4478233671383689,0.27435795917798345,0.0041338268249049293,0.76458002642708778,0.3215140781900131,0.19720509642198314,0.5871035201029613,0.36795603471902649,0.86816588440594389,0.73922721626208732,0.10432358504819061,0.028263762390181357,0.9524471830134178,0.12611479215580046,0.68919777444145747,0.77287981614655088,0.16682394978792636,0.92054781277520858,0.68952157723396723,0.97673022008216215,0.64150581540594143,0.22266212708659072,0.89228615363746588,0.17960507910021606,0.8699568445288256,0.60335635437288493,0.67390782806530103,0.73072575851855437,0.67998603544252445,0.57379651786577823,0.69474492371927199,0.67108946764757105,0.84161637488045371,0.041781452503798944,0.6779444548867708,0.32576362903576384,0.87823581403402462,0.16376454799197115,0.83047850412697422,0.74764489403499745,0.73505149754092247,0.84623457676722491,0.37744424360763884,0.79737558795639041,0.55828550548176448,0.068133725409093238,0.225249121654435,0.95663930713745038,0.016845383908708711,0.79788171723409451,0.16388985325466432,0.79962759147793649,0.031819839618212109,0.53203809124087109,0.10837163227940505,0.18105976165345028,0.33618172150629178,0.7850169097644164,0.12493964037350225,0.27407577579301545,0.66170504609874725,0.95048033975219626,0.80316468040199984,0.92895054840403901,0.15509978657167889,0.91630257171267415,0.93002578624622811,0.75661676503239195,0.80537709741682262,0.54114541834275631,0.76357785825050628,0.85189989621451934,0.51265669036942374,0.31023629000580977,0.26533031546764685,0.54538064297194855,0.91552601316693094,0.98599518199603087,0.99197665570991234,0.041717159646436075,0.053893250186436426,0.85968865053404464,0.72224228565224646,0.14368734172949316,0.030056274478410194,0.14433980595864204,0.76621175194212232,0.75435240247319679,0.97620042570480969,0.29046535106863369,0.32162492828527212,0.89538940022389257,0.111359506544641,0.76360800317110933,0.77503597567408422,0.98971034770812516,0.38063547609693216,0.41981810200763559,0.10181948719202305,0.83035784371910448,0.2320271825503516,0.043124870452590724,0.36969555243328955,0.18387214190752754,0.36881482003639932,0.68345354663952029,0.8715561797857293,0.72616112290916468,0.26007995601526934,0.55907037573946128,0.05216940052898178,0.40763068094846233,0.40604449921914498,0.54942647013067059,0.74090316340013707,0.76365287312235219,0.6601647986507313,0.39510896700116083,0.70496039493466001,0.68132855773171774,0.01186006928219896,0.03615848459085147,0.12243302062481665,0.28733958589054509,0.14897503112511512,0.53352938879389578,0.74391121943488814,0.30299051538067112,0.91965549945213743,0.5283710365136931,0.37860161053831887,0.20536961521779751,0.63338920694648582,0.23882394715608057,0.22624921350573834,0.64218606677361267,0.99753827899674208,0.77537660915034767,0.63897723014920416,0.79142065795208694,0.99184916693467495,0.88599099054483366,0.76528158390806789,0.24133957934201367,0.71620380812540163,0.76134863602696756,0.94266765875921688,0.63833187344056974,0.45204767126601131,0.62847147381448465,0.08049527207153484,0.93289207605672775,0.67611870412075781,0.99059721984883087,0.049181402346176628,0.17310608250312376,0.54730217040158313,0.87054473650229725,0.11288211063972542,0.78287971190523065,0.13051853287240933,0.50380236489836994,0.36138762142616487,0.068102128035049447,0.14584224080707309,0.25989787868197956,0.24884717638778919,0.30651855322263688,0.41854626441267573,0.052362914048855069,0.12647610683349664,0.10681392399090783,0.99185497992696037,0.25123597799657338,0.19753823179762842,0.89305517098212106,0.74337898857811902,0.56220315101396234,0.84980834656013082,0.49701921640793145,0.39848721204753101,0.019380144140304867,0.56246597971743739,0.69659861286075941,0.74299626280109754,0.87910157898810226,0.45830008880903961,0.33772456400890583,0.27610881875550464,0.18553690871896289,0.14826749567472153,0.34709464512758825,0.60617103999292443,0.34755503077268568,0.5418361435492125,0.065253588715359578,0.80325441979143153,0.75830706526226166,0.48400921351045312,0.27754247253130071,0.27582683100656002,0.99807617303072804,0.62988523189047074,0.70032491724152979,0.87019151504662995,0.30692108051163047,0.41628160880947551,0.49451680319683733,0.51306553222579343,0.24606138633260288,0.83623792712065814,0.20441744325200373,0.565201020475536,0.95306673538956954,0.37104472369561836,0.77047755186738021,0.91128211435393491,0.12112131038366225,0.32642749244518743,0.37988436927273261,0.27037000347545692,0.81112120273241139,0.64557730972444627,0.36641299022124518,0.93257384771996088,0.98156787632496922,0.51886849913943123,0.61151287310551339,0.50300119646758656,0.54270921571376451,0.72975788503883221,0.39083815994829041,0.086409096020504284,0.87862915839846001,0.2046594678067799,0.78304323188593783,0.57347757353117712,0.012431589661813569,0.87434648217282296,0.34386179902360692,0.21876078758641782,0.72335067953615984,0.99972697392025278,0.63116722735275976,0.5556486533231344,0.63754515452928062,0.049674338739706059,0.11582393694618276,0.6678239221222948,0.88676161400589004,0.023443776719131448,0.82930203497931088,0.18149052794574771,0.0078042281748256226,0.54130561036061797,0.95836338941921007,0.95766264142362867,0.54832707569746297,0.30375438310992831,0.8506277659115401,0.35576252755122961,0.06066875532345084,0.11164111907753438,0.43090523532043101,0.71395831076442284,0.63968525263448517,0.5223100043416018,0.6740320303164804,0.99539359554935647,0.80877540863498099,0.72208198638078347,0.14849171317056098,0.97949914963664886,0.73113463294497116,0.52655883586653041,0.0016522997600727507,0.56916168364421005,0.81954603079267163,0.33722822339082092,0.84869878766195705,0.82489688840358444,0.13218543170369812,0.49426598432991298,0.74381671361986046,0.71880460396441015,0.82303664545668953,0.73080434116371928,0.32748767936522671,0.76850192440529064,0.35237547144105819,0.12407126282907388,0.81151533127621189,0.32089497933272482,0.65903359565976749,0.84043970720196914,0.090478317972980005,0.90822548691977345,0.61821367798506588,0.17786070430557066,0.46629139440458689,0.80859263181115415,0.5067332330972103,0.13259357528580962,0.68749619619760849,0.16511972078185655,0.48564701634883078,0.93702614644201576,0.50101624615068929,0.22205938984979312,0.42885533291867461,0.0030217576702790128,0.66394145934797844,0.24687209637868179,0.91356927035433855,0.10438129595711654,0.59921069161934892,0.62676437508230309,0.19235543509043507,0.032167994950093481,0.66197363165482204,0.44791578767166051,0.932974813987406,0.89936059216379827,0.26595422239175426,0.72234353351632896,0.17861260309699956,0.91007960832609724,0.66459382179836846,0.050534240551140863,0.84940368637996633,0.23732053825110766,0.55795793728041054,0.21880745030856141,0.97677467985442146,0.14945905601456022,0.62738178970429936,0.17351853823690422,0.3925253226886104,0.57286993023724109,0.060607293876551394,0.16527921259326639,0.09387419880512049,0.20076546110343252,0.032684342475079009,0.99796946319018842,0.31356375156290406,0.92004163232560288,0.58926126601837281,0.88131737700529822,0.10394826767661669,0.32689406868722326,0.9346088160341236,0.040306573680111257,0.21464108540129889,0.98783501238858795,0.13328118413674828,0.02956914034865521,0.66981376002290494,0.76616568386151029,0.83196035674120761,0.44613742746852803,0.10669182512572084]} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/runner.c b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/runner.c new file mode 100644 index 000000000000..1faba777d03f --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/runner.c @@ -0,0 +1,464 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/** +* Generate C test fixtures. +* +* ## Notes +* +* - Run this script from the directory in which fixtures should be written. +* +*/ + +#include +#include + +/** +* Define prototypes for external functions. +*/ +extern void init_by_array( unsigned long *init_key, int key_length ); +extern void init_genrand( unsigned long s ); +extern unsigned long genrand_int32(); +extern double genrand_res53(); + +/** +* Generates a linearly spaced numeric array of doubles. +* +* ## Notes +* +* - Assumes that the output array has at least 2 elements. +* - Output array is guaranteed to include both the start and end values. +* +* +* @param out output array +* @param len array length +* @param start first value +* @param end last value +*/ +void linspace_f64( double *out, const unsigned int len, const double start, const double end ) { + unsigned int i; + double incr; + + incr = (end-start) / (len-1); + for ( i = 0; i < len-1; i++ ) { + out[ i ] = start + (incr*i); + } + out[ i ] = end; +} + +/** +* Generates a linearly spaced numeric array of integers. +* +* ## Notes +* +* - Assumes that the output array has at least 2 elements. +* - Output array is guaranteed to include both the start and end values. +* +* +* @param out output array +* @param len array length +* @param start first value +* @param end last value +*/ +void linspace_ui32( unsigned int *out, const unsigned int len, const unsigned int start, const unsigned int end ) { + unsigned int incr; + unsigned int i; + + incr = (end-start) / (len-1); + for ( i = 0; i < len-1; i++ ) { + out[ i ] = start + (incr*i); + } + out[ i ] = end; +} + +/** +* Generates a random double on the interval [0,1]. +* +* @return random double +*/ +double rand_double() { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Generates an array of pseudorandom doubles drawn from a uniform distribution. +* +* @param out output array +* @param len array length +* @param a lower bound (inclusive) +* @param b upper bound (exclusive) +*/ +void rand_array_f64( double *out, const unsigned int len, const double a, const double b ) { + unsigned int i; + double delta; + + delta = b - a; + + for ( i = 0; i < len; i++ ) { + out[ i ] = a + ( rand_double()*delta ); + } +} + +/** +* Generates an array of pseudorandom integers drawn from a uniform distribution. +* +* ## Notes +* +* - WARNING: the method used here is not particularly robust, as some integer values may be sampled more frequently than others. +* +* +* @param out output array +* @param len array length +* @param a lower bound (inclusive) +* @param b upper bound (exclusive) +*/ +void rand_array_ui32( unsigned int *out, const unsigned int len, const unsigned int a, const unsigned int b ) { + unsigned int i; + unsigned int r; + double delta; + + delta = (double)b - (double)a; + + for ( i = 0; i < len; i++ ) { + r = (unsigned int)( delta * rand_double() ); // truncation + out[ i ] = a + r; + } +} + +/** +* Casts an array of integers to an array of doubles. +* +* @param out output array +* @param x input array +* @param len array length +*/ +void ui32_to_f64( double *out, const unsigned int *x, unsigned int len ) { + unsigned int i; + + for ( i = 0; i < len; i++ ) { + out[ i ] = (double) x[ i ]; + } +} + +/** +* Casts an array of doubles to an array of integers. +* +* @param out output array +* @param x input array +* @param len array length +*/ +void f64_to_ui32( unsigned int *out, const double *x, unsigned int len ) { + unsigned int i; + + for ( i = 0; i < len; i++ ) { + out[ i ] = (unsigned int) x[ i ]; + } +} + +/** +* Writes an array of doubles to a file as a series of comma-separated values. +* +* @param f file to write to +* @param x array of doubles +* @param len array length +*/ +void write_array_f64( FILE *f, const double *x, const unsigned int len ) { + unsigned int i; + + for ( i = 0; i < len; i++ ) { + fprintf( f, "%.17g", x[ i ] ); + if ( i < len-1 ) { + fprintf( f, "," ); + } + } +} + +/** +* Writes an array of integers to a file as a series of comma-separated values. +* +* @param f file to write to +* @param x array of integers +* @param len array length +*/ +void write_array_ui32( FILE *f, const unsigned int *x, const unsigned int len ) { + unsigned int i; + + for ( i = 0; i < len; i++ ) { + fprintf( f, "%u", x[ i ] ); + if ( i < len-1 ) { + fprintf( f, "," ); + } + } +} + +/** +* Writes a named array of doubles to a file as JSON. +* +* @param f file to write to +* @param name array name +* @param x data +* @param len array length +*/ +void write_named_array_f64( FILE *f, const char *name, const double *x, const unsigned int len ) { + fprintf( f, "\"%s\":[", name ); + write_array_f64( f, x, len ); + fprintf( f, "]" ); +} + +/** +* Writes a named array of integers to a file as JSON. +* +* @param f file to write to +* @param name array name +* @param x data +* @param len array length +*/ +void write_named_array_ui32( FILE *f, const char *name, const unsigned int *x, const unsigned int len ) { + fprintf( f, "\"%s\":[", name ); + write_array_ui32( f, x, len ); + fprintf( f, "]" ); +} + +/** +* Writes data to a file as JSON. +* +* ## Notes +* +* - This function SHOULD be tailored to the input data (e.g., input types, output types, number of arguments, etc) and may vary from use case to use case. +* +* +* @param f file to write to +* @param y results +* @param len array length +*/ +void write_data_as_json_ui32( FILE *f, const unsigned int *y, const unsigned int len ) { + fprintf( f, "{" ); + write_named_array_ui32( f, "expected", y, len ); + fprintf( f, "}" ); +} + +/** +* Writes data to a file as JSON. +* +* ## Notes +* +* - This function SHOULD be tailored to the input data (e.g., input types, output types, number of arguments, etc) and may vary from use case to use case. +* +* +* @param f file to write to +* @param y results +* @param len array length +*/ +void write_data_as_json_f64( FILE *f, const double *y, const unsigned int len ) { + fprintf( f, "{" ); + write_named_array_f64( f, "expected", y, len ); + fprintf( f, "}" ); +} + +/** +* Generates test fixtures. +* +* @param seed PRNG seed +* @param len number of output values +* @param name output filename +*/ +void generate_ui32_integer_seed( const unsigned int seed, const unsigned int len, const char *name ) { + unsigned int *y; + unsigned int i; + FILE *f; + + // Allocate an output array: + y = (unsigned int*) malloc( len * sizeof(unsigned int) ); + if ( y == NULL ) { + printf( "Error allocating memory.\n" ); + exit( 1 ); + } + + // Initialize the PRNG: + init_genrand( seed ); + + // Generate fixture data: + for ( i = 0; i < len; i++ ) { + y[ i ] = genrand_int32(); + } + // Open a new file: + f = fopen( name, "w" ); + if ( f == NULL ) { + printf( "Error opening file.\n" ); + exit( 1 ); + } + // Write data as JSON: + write_data_as_json_ui32( f, y, len ); + + // Close the file: + fclose( f ); + + // Free allocated memory: + free( y ); +} + +/** +* Generates test fixtures. +* +* @param seed PRNG seed array +* @param nwords seed array length +* @param len number of output values +* @param name output filename +*/ +void generate_ui32_array_seed( unsigned long *seed, const unsigned int nwords, const unsigned int len, const char *name ) { + unsigned int *y; + unsigned int i; + FILE *f; + + // Allocate an output array: + y = (unsigned int*) malloc( len * sizeof(unsigned int) ); + if ( y == NULL ) { + printf( "Error allocating memory.\n" ); + exit( 1 ); + } + + // Initialize the PRNG: + init_by_array( seed, nwords ); + + // Generate fixture data: + for ( i = 0; i < len; i++ ) { + y[ i ] = genrand_int32(); + } + // Open a new file: + f = fopen( name, "w" ); + if ( f == NULL ) { + printf( "Error opening file.\n" ); + exit( 1 ); + } + // Write data as JSON: + write_data_as_json_ui32( f, y, len ); + + // Close the file: + fclose( f ); + + // Free allocated memory: + free( y ); +} + +/** +* Generates test fixtures. +* +* @param seed PRNG seed +* @param len number of output values +* @param name output filename +*/ +void generate_f64_integer_seed( const unsigned int seed, const unsigned int len, const char *name ) { + unsigned int i; + double *y; + FILE *f; + + // Allocate an output array: + y = (double*) malloc( len * sizeof(double) ); + if ( y == NULL ) { + printf( "Error allocating memory.\n" ); + exit( 1 ); + } + + // Initialize the PRNG: + init_genrand( seed ); + + // Generate fixture data: + for ( i = 0; i < len; i++ ) { + y[ i ] = genrand_res53(); + } + // Open a new file: + f = fopen( name, "w" ); + if ( f == NULL ) { + printf( "Error opening file.\n" ); + exit( 1 ); + } + // Write data as JSON: + write_data_as_json_f64( f, y, len ); + + // Close the file: + fclose( f ); + + // Free allocated memory: + free( y ); +} + +/** +* Generates test fixtures. +* +* @param seed PRNG seed array +* @param nwords seed array length +* @param len number of output values +* @param name output filename +*/ +void generate_f64_array_seed( unsigned long *seed, const unsigned int nwords, const unsigned int len, const char *name ) { + unsigned int i; + double *y; + FILE *f; + + // Allocate an output array: + y = (double*) malloc( len * sizeof(double) ); + if ( y == NULL ) { + printf( "Error allocating memory.\n" ); + exit( 1 ); + } + + // Initialize the PRNG: + init_by_array( seed, nwords ); + + // Generate fixture data: + for ( i = 0; i < len; i++ ) { + y[ i ] = genrand_res53(); + } + // Open a new file: + f = fopen( name, "w" ); + if ( f == NULL ) { + printf( "Error opening file.\n" ); + exit( 1 ); + } + // Write data as JSON: + write_data_as_json_f64( f, y, len ); + + // Close the file: + fclose( f ); + + // Free allocated memory: + free( y ); +} + +/** +* Main execution sequence. +*/ +int main( void ) { + unsigned int len; + + // Define the array length: + len = 1000; + + // Define a seed array: + unsigned long seed[ 4 ] = { 0x123, 0x234, 0x345, 0x456 }; + + // Generate fixture data: + generate_ui32_integer_seed( 1234, len, "uint32_integer_seed.json" ); + generate_ui32_array_seed( seed, 4, len, "uint32_array_seed.json" ); + + generate_f64_integer_seed( 1234, len, "float64_integer_seed.json" ); + generate_f64_array_seed( seed, 4, len, "float64_array_seed.json" ); + + return 0; +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/tinymt32.c b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/tinymt32.c new file mode 100644 index 000000000000..e9e292f69861 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/tinymt32.c @@ -0,0 +1,302 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* +* ## Notice +* +* The original C code and copyright notice are from the source implementation +* (see http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/TINYMT/). +* The implementation has been modified according to project styles and +* conventions. +* +* ```text +* Tiny Mersenne Twister only 127 bit internal state. +* Coded by Mutsuo Saito and Makoto Matsumoto. +* +* Copyright (C) 2011, 2013 Mutsuo Saito, Makoto Matsumoto, +* Hiroshima University and The University of Tokyo. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* +* 3. The names of its contributors may not be used to endorse or promote +* products derived from this software without specific prior written +* permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* ``` +*/ + +#include + +// Period parameters: +#define TINYMT32_MEXP 127 +#define TINYMT32_SH0 1 +#define TINYMT32_SH1 10 +#define TINYMT32_SH8 8 +#define TINYMT32_MASK ((unsigned int)0x7fffffff) +#define TINYMT32_MUL (1.0f / 4294967296.0f) +#define MIN_LOOP 8 +#define PRE_LOOP 8 + +#define MAT1 0x8F7011EE +#define MAT2 0xFC78FF1F +#define TMAT 0x3793FDFF + +typedef struct { + unsigned int state[4]; + unsigned int mat1; + unsigned int mat2; + unsigned int tmat; +} tinymt32_t; + +static tinymt32_t random; + +/** +* This function represents a function used in the initialization +* by init_by_array. +* +* ## Notes +* +* - Used for initialization of the internal state array. +* +* @param x 32-bit integer +* @return 32-bit integer +*/ +static unsigned int ini_func1(unsigned int x) { + return (x ^ (x >> 27)) * (unsigned int)1664525UL; +} + +/** +* This function represents a function used in the initialization +* by init_by_array. +* +* ## Notes +* +* - Used for initialization of the internal state array. +* +* @param x 32-bit integer +* @return 32-bit integer +*/ +static unsigned int ini_func2(unsigned int x) { + return (x ^ (x >> 27)) * (unsigned int)1566083941UL; +} + +/** +* This function changes internal state of tinymt32. +* +* ## Notes +* +* - Users should not call this function directly. +* +*/ +inline static void tinymt32_next_state() { + unsigned int x; + unsigned int y; + + y = random.state[3]; + x = (random.state[0] & TINYMT32_MASK) + ^ random.state[1] + ^ random.state[2]; + x ^= (x << TINYMT32_SH0); + y ^= (y >> TINYMT32_SH0) ^ x; + random.state[0] = random.state[1]; + random.state[1] = random.state[2]; + random.state[2] = x ^ (y << TINYMT32_SH1); + random.state[3] = y; + int const a = -((int)(y & 1)) & (int)random.mat1; + int const b = -((int)(y & 1)) & (int)random.mat2; + random.state[1] ^= (unsigned int)a; + random.state[2] ^= (unsigned int)b; +} + +/** +* This function outputs 32-bit unsigned integer from internal state. +* +* ## Notes +* +* - Users should not call this function directly. +* +* @return 32-bit unsigned pseudorandom number +*/ +inline static unsigned int tinymt32_temper() { + unsigned int t0, t1; + t0 = random.state[3]; + t1 = random.state[0] + + (random.state[2] >> TINYMT32_SH8); + t0 ^= t1; + + if ((t1 & 1)) { + t0 ^= random.tmat; + } + return t0; +} + +/** +* This function initializes the internal state array with a 32-bit +* unsigned integer seed. +* +* ## Notes +* +* - See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. +* +* @param seed a 32-bit unsigned integer used as a seed +*/ +void init_genrand(unsigned int seed) { + // Initialize constants: + random.mat1 = MAT1; + random.mat2 = MAT2; + random.tmat = TMAT; + + // Initialize state: + random.state[0] = seed; + random.state[1] = random.mat1; + random.state[2] = random.mat2; + random.state[3] = random.tmat; + + // Initialize state array: + for (int i = 1; i < MIN_LOOP; i++) { + random.state[i & 3] ^= i + (unsigned int)1812433253 + * (random.state[(i - 1) & 3] + ^ (random.state[(i - 1) & 3] >> 30)); + } + + // Generate initial values: + for (int i = 0; i < PRE_LOOP; i++) { + tinymt32_next_state(); + } +} + +/** +* This function initializes the internal state array with a 32-bit +* unsigned integer seed. +* +* ## Notes +* +* - See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. +* +* @param init_key an array of 32-bit unsigned integers +* @param key_length the length of the array +*/ +void init_by_array(const unsigned int *init_key, int key_length) { + const unsigned int lag = 1; + const unsigned int mid = 1; + const unsigned int size = 4; + unsigned int i, j; + unsigned int count; + unsigned int r; + unsigned int * st = &random.state[0]; + + st[0] = 0; + st[1] = random.mat1; + st[2] = random.mat2; + st[3] = random.tmat; + if (key_length + 1 > MIN_LOOP) { + count = (unsigned int)key_length + 1; + } else { + count = MIN_LOOP; + } + r = ini_func1(st[0] ^ st[mid % size] + ^ st[(size - 1) % size]); + st[mid % size] += r; + r += (unsigned int)key_length; + st[(mid + lag) % size] += r; + st[0] = r; + count--; + for (i = 1, j = 0; (j < count) && (j < (unsigned int)key_length); j++) { + r = ini_func1(st[i % size] + ^ st[(i + mid) % size] + ^ st[(i + size - 1) % size]); + st[(i + mid) % size] += r; + r += init_key[j * 2] + i; + st[(i + mid + lag) % size] += r; + st[i % size] = r; + i = (i + 1) % size; + } + for (; j < count; j++) { + r = ini_func1(st[i % size] + ^ st[(i + mid) % size] + ^ st[(i + size - 1) % size]); + st[(i + mid) % size] += r; + r += i; + st[(i + mid + lag) % size] += r; + st[i % size] = r; + i = (i + 1) % size; + } + for (j = 0; j < size; j++) { + r = ini_func2(st[i % size] + + st[(i + mid) % size] + + st[(i + size - 1) % size]); + st[(i + mid) % size] ^= r; + r -= i; + st[(i + mid + lag) % size] ^= r; + st[i % size] = r; + i = (i + 1) % size; + } + + for (i = 0; i < PRE_LOOP; i++) { + tinymt32_next_state(); + } +} + +/** +* This function generates and returns 32-bit unsigned integer. +* +* ## Notes +* +* - Generates a random number on interval [0,0xffffffff]. +* +* @return 32-bit unsigned integer r (0 <= r < 2^32) +*/ +unsigned int genrand_int32() { + tinymt32_next_state(); + return tinymt32_temper(); +} + +/** +* Generates a random number on the interval `[0,1)` with 53-bit resolution. +* +* ## Notes +* +* - Added by Isaku Wada (2002/01/09). +* +* @return random number +*/ +double genrand_res53() { + unsigned long a = genrand_int32()>>5; + unsigned long b = genrand_int32()>>6; + return ( (a*67108864.0)+b ) * (1.0/9007199254740992.0); +} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/uint32_array_seed.json b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/uint32_array_seed.json new file mode 100644 index 000000000000..967c4460ae27 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/uint32_array_seed.json @@ -0,0 +1 @@ +{"expected":[3211122912,3130178971,1393930717,3685266295,4254824414,618938108,3162876411,2036897886,2403752038,1950023525,4134075375,2778504273,794948422,775764703,1252668376,2797359246,1975511539,4073103956,2756974134,3462104707,1545568538,115291492,119092002,3947724677,4267194564,2057109049,112199536,1206820085,1759684685,809616330,1758172198,3531990490,2517276625,597525531,478291240,2996288837,3760177871,2653344095,2007304455,1063112752,1526332788,3185659901,29197526,1274099578,2159816417,3828290361,784802239,1258990978,2254931599,3572288428,175492404,3532640289,2689431411,2464375184,1308554821,802501590,3307640800,2305535022,3227033168,2963233118,2752126025,3719024285,686086998,1536076336,1821259459,2761308564,1278641822,4024613928,1120815119,3257561686,2781380072,1944140725,2364570664,3545366030,288985417,3114708740,2889152005,2436350533,2965530695,2781742349,1819534164,1650898108,2427647438,1959287763,1498222639,386640563,3533703238,498494111,2790609729,856926134,1332236498,1310146714,2381398541,4134775114,1233044326,1429785407,3138588079,522938153,2865599700,3637293982,3069151394,2115146530,347107414,1674915521,1931095291,3262761200,3171580467,3590785176,106207463,3647779521,3289578594,517501227,1248485247,4037640914,1702380676,2319807161,3041974798,955447704,3767015119,2256659875,715468844,4036161439,1180896395,3806671783,4056417544,4204010125,3015920524,1967668842,3484530089,318205859,3265441838,4015184237,4270885126,559278023,1772236825,3955784508,2884376533,3092696525,4007142391,2448083997,815251714,374061980,775138326,1605976703,3199396661,3965695601,3207462275,727453443,411430230,4254625568,3072583174,1434594238,3643286413,1331009765,3257194749,2375912543,1247130051,3075633309,1239918798,1380391591,3980402129,622501190,943595017,532072319,3604036923,4007038836,254050693,1987001107,4152121939,10295746,2103149340,3937456107,4293186814,2078660045,4053316617,2308288443,2403778030,822131958,491125839,3799019288,2537099702,889019570,3388872910,2776729724,2722714358,599116790,1065630041,1846264836,1612321860,1141553614,2688689536,904767936,2736528322,2244559131,1764237719,2779489580,229755165,1605738819,4147914639,1878726940,3041322303,2505117398,1175494157,219674203,595039291,987618951,3816846076,2083613696,2211526277,2856750527,108700139,4082482052,538291558,824662901,4003934610,3406093159,848612363,2483089491,1970901262,2907518199,3623285264,2784781118,2594212728,816957947,1959207079,62331484,1949200484,3105529111,3436718154,778254550,1050718476,3003549926,358759053,2285110471,3995313566,4007424290,3054001193,2418081034,2919987200,2670770365,2457792445,394419077,3207558078,129745386,3434028352,372740224,1414068290,12731557,3263100152,1290061134,1235660137,3601023854,1311127384,1164860938,2310384825,679419225,1470055364,4053456904,2743824881,3321669665,531108515,4249480553,909843227,3905572537,2035861603,4155117782,58095743,2023901710,4107243276,1181668305,1506000238,2483300082,2652235257,4026628762,2684779688,3264605230,1263872400,1766685703,1322821130,3257578566,1967369949,1870060610,1187567740,244813036,495533072,3199510969,922121657,346816932,4264076063,2561922298,1056405193,2776909285,3638633220,1318274297,4112490045,2662802020,278092074,2700134107,4030820922,2276472246,1542084362,2294146527,1210951775,364045897,1885892184,3302244543,4178800136,257068319,3628416953,599264543,3603870245,1459353811,1673649483,401365670,1635666095,740829966,265477802,2548824730,1834614152,324856951,845259770,999769789,3666390069,2622619939,1464506088,1452294740,2035646011,1870892636,794216957,1820084938,2242703194,3032719985,2876967586,976745808,302420825,2028946029,1831085884,3527624661,1223600926,1524206054,593094919,798648627,1396471394,3168513364,1835163501,2129005036,669636548,1422681145,1727966510,3261320546,2421901040,4044164671,3862806377,90256314,1728124938,382124569,75196143,61286894,3376725917,922900844,1199541594,2407811065,466507543,2395977148,3206611620,1937789189,2198938092,210590338,2768895318,1444474212,3571358512,306559329,660928205,3965151574,2644194830,2546137419,3908490028,3438616304,1325164245,2505143802,2475921909,2804911983,1885042060,3861718795,4140982116,1374028258,1134827463,3549930235,2129289689,2437480962,3644752317,197980087,2213564414,4274712319,610924785,3877026176,2383312077,2836656781,1600270523,2619345983,1013927254,89092542,2801271132,670972347,2634941041,2665727238,3597242,1477554271,3818738375,2390951551,2735120286,1306571153,2684959559,3682641195,29558405,1166907798,3711074345,311332928,4280503431,279819283,3492486067,2407151552,876268784,2338107866,2445871599,743276128,2873309038,190982035,1645096038,1948351220,1293190876,881783421,2410774054,2559683655,2436849490,947801240,3311887596,4158507019,2352904451,3466165362,2123052066,3703052282,3053568816,1310303897,2619543503,2331557515,1034025185,632918760,2127440311,1783353822,2468792030,1181711762,812865947,3712472199,1304235305,361253328,2388970323,1108749602,2362602302,1297053809,3644670156,1399368654,662234114,4085060455,1111687654,3100398514,231133633,2634190054,396852323,2059937093,2734016960,3847406990,4285210964,3188128976,2803238951,2094447054,2860832173,3913665117,1074758497,3048568197,2874106009,343881,151211494,3366116558,1274325594,3871583494,1206721894,3861443496,1692919195,286231342,4016145073,171426105,2446219527,648526391,2854086014,3513416104,3542570561,2726328155,1473523343,1432359617,2249371184,2539130178,3366236599,3559147580,160692395,1386147821,2604251962,2066811552,1055099975,3244935430,2116070128,2869620171,3033377898,4189380767,850436451,994186841,328144237,695766086,3318589450,2794328013,1238454265,3095683984,3193502364,2261826567,1574506575,2712830588,3226104648,1431709565,3322940397,3571809184,1594036891,2624431181,2720297776,3708897179,1412032231,2581730435,263088760,2863465916,3602158512,327078150,3734228767,2232788067,1368405149,846856352,3023160927,433217956,2336367370,1264772859,1487292205,2139498380,166373358,3631153437,171361206,1454653166,2951121046,438353827,322486596,671052993,350114713,1173526096,2591700906,4258492153,1455772395,175483689,1855479641,3207119688,613040330,2837042621,527749251,2455082060,3352442531,2284462878,2013341599,2647196406,1184663199,1456245291,2546611968,979824129,1797150733,112175325,1835381201,4234468284,512725141,4213935850,2288502539,622154144,4196760592,3412379789,669729184,3928837461,147471196,3050270540,521795140,1661731048,416321965,2920630784,814627365,1522292049,3651051616,1728067031,3059057532,2352142912,343632007,1659639873,4067673053,1484238688,1555980284,4097016027,1733229162,745291595,847214436,2088431184,3482401902,548239475,782882567,3554863024,146161653,3976971188,1982313577,1133116091,2402195905,1812938997,1533052856,3995182335,959259307,355685222,1750066044,1417688422,3308872544,1620872494,410615270,942618975,429207039,2214621717,1732193379,241297208,802083194,2499634844,3714381665,2099012272,2573843557,336962578,1318834948,1610919583,3006717065,2974926618,4050462541,2387237497,1405205517,814088690,4099905832,3102598603,3269486742,222683585,23631625,3031315388,1345537740,1078877932,3991809670,1106373666,483867895,632783050,4136721787,931050426,2994843170,735557568,12489360,323232997,2426475027,2118341357,2381558635,1098531532,3446102466,2528637469,3355259142,2314476593,3303584550,1880373261,1110429663,16371032,4242810748,791511286,1252100872,3224338024,2918824406,4239140905,2408283952,2723514782,2898586949,1459052115,3054228339,1640376767,3376266812,517056608,2505876928,1508626424,104208817,2630412525,2694195638,2661303194,3604205903,1233951733,3712619774,2363589931,384466364,1525924259,1344453805,3018532677,766779898,617895069,4120549821,3593541696,3811395267,4256946959,1313319127,1343246817,652010096,675435259,1375190925,1570045151,1337522945,2378378463,3691078861,3596531514,4232950825,1359958497,925575452,2469985956,1299643095,4285508400,2266900802,2322605372,2359446014,3981560627,4200265798,265774474,3175117499,1027414086,2096844726,2192833790,381465438,1704339540,2812909679,438993183,285289553,1743462481,2652226903,1711282483,4036521741,3960376752,1239991615,391046127,1406635737,562859382,4269044852,1928378188,3887953413,2838242801,542587764,3991704040,3660679657,2788183846,1295047084,3904368355,3932287849,3476999550,3921085113,1364805653,384320863,978236612,45317336,2209672665,3158702771,4128058836,4272906953,2197723953,3575441074,487354365,982376841,934895688,631989558,1175378779,3093903339,2056909440,2351695240,3409738489,887990816,4070029336,234777589,2220831203,3129618261,2164612396,2685352249,2882792537,1505452184,1812670190,4138991213,1915320654,648205866,855894693,2321805329,2600797499,3299536466,1561983142,1354728740,3525597239,2555700552,2038625161,3629769933,2832189710,1630705525,3082060640,3116084788,3367495344,997000393,2684270138,4092107470,597305495,1774051869,536653992,3953337226,2492481586,3109183450,3819964594,2613482189,750470414,464563278,65544938,3667068756,1259656616,3891941800,2303171343,450507571,1602496036,4060362997,3557084950,257798824,2303872535,2525008790,2704979461,871119947,761796832,1729654928,717919625,2621767100,395994439,3549957381,3995596137,466674278,30336420,3898689619,189115221,1499187778,1193407351,26308164,2047638551,1222560830,2115068903,206944516,1014170331,2639192428,3533904256,2183485988,2973834832,17364558,4107528379,266205294,4100212956,764093311,2299765563,751407425,3959001227,1127014483,241836912,4282416195,283209456,1414993906,4211500509,1256824271,4210265650,4235000193,3826098817,3723597345,3110200165,2417690581,559919454,3393397435,2524072570,2902784484,1369432397,3967712258,1709571018,2502343487,3543232900,1047597215,4074391238,370153979,776487206,673964798,4943578,1893040880,3149180641,473657273,3430699928,482272922,885129394,3159742114,2067183350,1676958123,2854837802,2446884809,1340241784,1077931726,1879474146,608575017,3238701105,158732318,119436018,3763184303,493861319,1179813957,1661338770,2499760737,3881360560,2420508563,3688429543,357051656,4055100358,1073278692,3588155728,3019353916,1187522892,1806516015,3216465980,2032385870,3818544007,3440020017,1339799119,1005000100,1741089325,157409559,40251567,3722931722,4216823265,525070075,1124252326,1383040693,661344013,1337788808,2337132146,977931401,4251320065,406309885,2717163581,2134507774,3472079292,2976181113,882993263,2079676980,3534345510,489047634,1441310643,602590129,2531509603,2246826749,2138077202,2793888541,485511540,3458205787,1392644691,3495556558,4049943739,3858426644,174861928,3214260294,1728516181,1117999371,1019112548,3405537424,2109007471,3830152608,145784744,1015432406,2224746549,3324260053,2235638424,1472886536,844492784,4085953613,2517337716,1928601261,1816881033,1979654109,1002213722,2427191394,3431731411,1063899970,3126318629,150496300,3321088995]} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/uint32_integer_seed.json b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/uint32_integer_seed.json new file mode 100644 index 000000000000..668587581eae --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/fixtures/c/uint32_integer_seed.json @@ -0,0 +1 @@ +{"expected":[2682965004,3700004639,172287182,1209377361,1950771355,1962758782,1320885223,2611857057,2966906914,3677362116,1985270536,3998547518,1667106768,1479106666,1969787912,646799609,260046449,454340285,4216334425,225225306,1861631923,1430695998,939255597,1891652552,3071180741,2255169613,2767281466,400440158,58209374,3953087282,86555575,4181547368,796611501,2916762885,1976307058,270284597,3685125958,1218192879,430929107,1637857921,1200964516,3721843125,342260892,3961655913,476440488,352905305,3823385331,2713310788,2825927563,1537151855,2672327499,932707164,2677929777,1193951785,1457488,451756902,2756268941,1141639061,3383502502,3411978300,1828753622,2103666018,4033990549,2842600558,1965449204,2732200904,2205593839,91302511,2503722280,2161222346,2221567512,1252134545,542410104,593828730,3794977632,4208530024,1674465247,2411880053,1799057800,1648203150,2464038806,440531688,4088526081,3505211307,115304871,575470560,1346824147,4015727598,3782569777,17041071,3432201801,2674105288,2144647004,1484701287,439842713,430475710,3897483690,2720755702,2318550989,1078800772,1534194243,3952492314,1208768537,218289510,1735980208,3593639877,2182696798,2788055331,3270670551,1193441845,712325779,925287279,709136232,2425528947,40991326,979680473,1286260569,2280409004,4220550671,3342463749,1123660979,4255194014,2407969515,2012781718,385195058,2361781636,309638134,2020548446,1698135254,2551347387,2710714022,2670933729,3371519785,3017956203,725569020,2733249762,1853882383,4009880749,3468330690,754781822,3853615573,51321454,695275350,2149123867,1492159365,3178602210,3402926557,4047664740,582980458,3508879738,3438052339,3108210104,2592947652,2787190338,1769356689,3618685834,1216130249,806582443,3900075112,2002333794,1139242321,458209750,3510318622,1642045074,3080138362,3757008371,2344297421,3948939682,4162793702,2798792077,1823076315,1158181841,3285829975,1078175377,3143228499,2684652507,3531691014,849204693,3739511704,617407356,1138951452,620222935,2311662333,1728178903,347202585,3943123653,3164499354,3032808808,2200704610,262842344,1725936779,2284803559,457242842,1420698019,2698861304,3482463450,2366078075,3174740292,2393571377,973197868,3630138752,3146228229,3113461762,1193793455,2648813008,2828940989,2852380089,1032430690,1615249750,1503685559,522417246,1941623716,2597133436,2786290599,3407673931,3408628093,4078090331,3845218986,1943658992,1789365273,995968210,2077620069,3110201257,2940651192,3179726438,704719241,3060322289,764963356,3722663928,3848987280,3652445082,912295178,3209633629,1422632824,529799623,2836565069,3892355907,1630319748,1738842623,3785714846,367711023,2052823516,91991540,3217524833,3193949330,3467591631,2819557694,575265444,907238574,296886342,314641255,61627959,547231053,463989718,2681431832,1789193097,1455403796,4023233598,3771090552,1950332982,2002169277,1612102975,1425507746,1081950191,401838025,3903601068,3731891997,3545152767,2618461107,3438929214,2798048525,3133455257,4093432479,1111243847,830883827,360921950,10405717,2659993630,1533433662,1928380294,872054417,3427287196,2639337109,3912054298,1426802847,751819471,1638943129,469655878,698542751,2612139171,4137493567,927749511,3697480964,450759704,1080777644,2188437694,1999242309,2160192958,2262815370,2426431164,1329504173,2575523412,786148856,1013571925,3970866885,637789025,3496489047,4282629681,2453172446,4105642753,3729027177,421332975,421561121,3475678604,4108680657,4148196170,1687307254,197715254,3813942792,4158478733,2380381982,1843979079,2173669177,706248079,1809092973,2759210719,2070524045,1041526566,3543668467,3727031295,3383635822,2542758458,3202055342,748000735,1164003857,3199575511,3587322170,3276031281,1441774690,3025278785,1013430197,219705172,260354049,3638847085,3732975221,2075511820,1823898577,1405728050,1312124123,3927392200,1416185259,3028389094,4036278130,2288764134,1317879631,3853786800,2202239932,3699124394,97695135,618895295,1217269589,1365647088,272974145,2310963744,2761981322,2893311993,2412319250,4042457536,3327808681,2311267525,1853375380,357110070,1553667151,202727930,4244996986,2659537525,460930387,4084099650,2343110923,2466810187,3173908186,2847958879,636137572,1525901479,4139170504,3411197009,2317198816,3548739424,3622360827,1899006223,2570632587,2920935294,1983753006,720827124,1553415501,3837786472,1033590684,1229842420,3088386383,3309701273,1730823665,2375528787,963173743,506996549,1309150713,650496835,2996581137,563936537,1171896569,3086511338,1265096199,2558546103,499583661,2813324210,4183895036,4107433804,3750823656,44823103,3773278772,2757986803,3328972217,1870568905,3842776115,7906219,3958559683,139821713,279603265,2485109402,1437503166,4196021557,1509019853,741748301,2264284914,4057907412,2812437843,1717546081,2600302780,89720201,1935094227,2539694075,1966326906,4128305931,3337815470,3878565153,4282162975,4132309336,372791865,2521811998,725509640,2466786703,633569354,1456934237,2681996002,2553226395,4117905577,3878636258,529845098,1593984522,3065750540,3322650935,984186564,1417218411,889985682,2111905316,2159231045,3533610915,1018444338,968187975,3937766607,2465363567,2862119286,1656984937,1926935487,2563370046,4194982167,1013547826,4133286359,1351085966,2009896621,1888654029,1668665836,357127042,1302383487,3253176094,814335437,2818797024,4003229601,535433143,4084960001,2457115222,3169340333,1430626830,1512866347,3661969049,223110368,2323594191,3297823982,3062810750,2015204464,1050107728,2926565941,1242537172,1617524090,3632382064,1403222777,4108205247,4175475602,393337395,2063877979,1854790479,585887860,1397734755,1681586756,3248259118,616408556,1473557523,1290172712,2231248995,1483340452,1733497337,912031623,1523100367,616539044,1505981640,3842085588,2884309672,2175161330,3434185113,1278404307,3733031772,576307179,1157195754,829280206,3014695294,2638658708,4123401860,3667060568,1127891799,3783348575,1800868005,1648258616,1272049778,1307067311,2066774343,1976976160,3057184063,1659869081,931411492,136644010,514234549,173179779,2900659026,209389777,302590458,3701643053,794602550,3999775201,246452553,1540252958,207064772,3099745684,2076522987,3286570721,1321605603,1222797592,282582293,3545127091,3088877290,2640764256,1944096919,2468986119,710857902,2339600598,1327210439,3797542368,3324411436,3688062769,1644232309,1894226518,3359137534,2291385305,1910792223,3978572549,1315302960,1149679976,1074292112,1310370076,2594473686,339124689,1187770830,1561057780,3804055765,968698290,329568788,4194444535,1724909563,1724809673,1661155152,1287789311,3922652436,4129750981,3313995804,1320190086,1792298138,3772751430,54460144,3468868011,2122735870,879355462,550689999,2932116138,2430073632,3822112762,3231218256,771100395,368297267,896728583,331441395,3199429413,864344021,2474815904,526741767,1535824408,3768427700,960399973,1667450839,2646375084,2533479943,3368518087,878237663,322267934,3384300385,1322027239,3969464517,4122683204,3572955456,439338026,2000740283,2129899187,748868866,2473512753,1544867910,1880050907,3587768629,2564280105,1974748676,1776537018,2040035436,2942277572,3572425750,1626480970,1023021778,545810781,1428366754,1167764214,1306558289,3692687576,3286833974,521385578,3129601906,3943655578,2251639648,1950900215,1767386303,2998978193,3218824650,3708984789,1209847331,4109189500,72664814,816689210,3462558381,530307664,3857756414,2017440145,2778416140,1033167097,78015618,1305322091,836777199,4132611690,3549237539,2272556139,796537073,673055509,2300207609,3873275393,1696363619,589064932,3881927660,3041803088,4112894454,2157363190,2215095402,1193898985,1128717074,3111144368,1012528552,2296188882,722697639,687285165,296093449,3988820414,1836861714,4251343016,2932429668,615142046,21358536,1246384209,199286953,496813863,7121559,568063093,1396248631,359626551,1674514386,4232232584,4056200219,2505013078,2555176396,608067253,3951421274,972565864,926258784,2171848512,2541069857,2458722294,318947902,95155199,518350177,1702366384,402903684,3975947044,2955314947,784719567,3286650265,2373279635,2387629360,3166792179,2052608865,745282840,2805227114,1246725000,1925938122,3332756721,4061185072,2773183686,3045960601,951584168,2967167327,2513471407,1991804536,1617687514,2638797823,3610287316,2957191362,1183007950,1337967427,1315914540,616062647,2880921088,3426711643,3096650637,1700623587,559668814,2290374494,2523175062,1357798267,4115356808,1341020341,3113158389,811500203,2440936714,1688055208,3859428058,2200046427,2304330482,3063518403,3659382658,3884565493,1485233448,733578882,940365981,1482488878,3856704658,1797970738,17704757,688537402,3201053723,1014609061,3770542242,3537139966,2661731062,1039981043,300047183,1771847896,1167449872,3231231318,3437456927,4108022748,2016716832,1323741449,722674626,2216662918,126992476,2961241206,28891598,2626867541,3777607394,961079008,4058999902,1263504512,1168659282,3102911808,2668925896,81331653,2379356020,2860077078,4077994078,2470301860,2422600320,815966137,235562374,3126945132,2514463142,2985423759,1453399559,2072811320,404590032,2360133989,261962247,390406186,1855407258,2237294961,1252413692,1472519307,3906181771,1016086619,4291536861,3836381145,252626505,3853765304,1348510887,2737492957,1006891751,1470918088,1758112533,3124038977,3731507090,1201345488,3246001323,2800743123,1901074496,3719821675,2856845084,4248634432,3704014281,1677650129,4158482975,345939431,3701504331,2871831504,1640033979,3452614695,4074611623,2247951658,1965603796,473358757,1417564599,2654406041,2752072910,3543742729,802437465,3041457019,393252417,1513549544,3571577728,3896500987,2399012310,2100395360,2095041924,2435705266,60458639,3523328688,1664509850,3795031715,1858510961,4044674310,1124127231,3331441198,392415409,1754407197,3375951393,464437140,4157126762,2791764650,222282517,2625846043,3311853064,4205076596,1080010913,1591630961,1140543148,817642074,3682305927,1675180417,4188161753,844116464,3962726683,3879776522,3337652886,1708963026,3526242519,2531879230,3984180110,3036101873,1430157662,453698309,549777564,1404150054,2036498577,2793415355,981145806,2206829360,3796732581,3181677194,127715522,2060495188,3925780786,683047641,3588781470,979324270,1751747026,3863994163,2535512088,3979154652,1874251360,2499153912,3599012642,3990117054,2454969135,316816538,2697693664,2209567043,38461748,2980932387,3617573728,1268847742,497926422,560775250,775964659,2135058409,2675145427,1343842897,563442914,2616399383,2478389427,4089963423,3849742788,1380021215,2881315828,2533937151,4258881094,3532810943,274311518,1473130729,1905833607,628452856,1387725564,3270104667,3682880870,4227280636,2701113703,153751130,2399673565,424290875,775222941,1610148891,228290957,3051218361,3994806232,537313647,809382313,3535612521,4247723887,3437661901,2532458181,3762684353,3148867716,1645083419,2522183626,395048624,1990935983,3588425796,2080588123,3976028613,3334366134,2600874132,1365646861,4285037041]} diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/test.factory.js b/lib/node_modules/@stdlib/random/base/tinymt32/test/test.factory.js new file mode 100644 index 000000000000..aadb8c2a1b3f --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/test.factory.js @@ -0,0 +1,1218 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var ENV = require( '@stdlib/process/env' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); +var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' ); +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var kstest = require( '@stdlib/stats/kstest' ); +var gcopy = require( '@stdlib/blas/base/gcopy' ); +var minstd = require( '@stdlib/random/base/minstd-shuffle' ); +var typedarray2json = require( '@stdlib/array/to-json' ); +var factory = require( './../lib/factory.js' ); + + +// VARIABLES // + +var opts = { + 'skip': ( ENV.TEST_MODE === 'coverage' ) +}; + + +// FIXTURES // + +var UINT32_INTEGER_SEED = require( './fixtures/c/uint32_integer_seed.json' ); +var UINT32_ARRAY_SEED = require( './fixtures/c/uint32_array_seed.json' ); +var FLOAT64_INTEGER_SEED = require( './fixtures/c/float64_integer_seed.json' ); +var FLOAT64_ARRAY_SEED = require( './fixtures/c/float64_array_seed.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof factory, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided an options argument which is not an object, the factory function throws an error', function test( t ) { + var values; + var i; + + values = [ + '5', + 3, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory( value ); + }; + } +}); + +tape( 'if provided a `copy` option which is not a boolean, the factory function throws an error', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory({ + 'copy': value + }); + }; + } +}); + +tape( 'if provided a `seed` which is not a positive integer, the factory function throws an error', function test( t ) { + var values; + var i; + + values = [ + '5', + 3.14, + 0.0, + -5.0, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory({ + 'seed': value + }); + }; + } +}); + +tape( 'the function throws a range error if provided a `seed` greater than the maximum unsigned 32-bit integer', function test( t ) { + var values; + var i; + + values = [ + UINT32_MAX + 1, + UINT32_MAX + 2, + UINT32_MAX + 3 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws a range error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory({ + 'seed': value + }); + }; + } +}); + +tape( 'if provided a `state` option which is not a Uint32Array, the factory function throws an error', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + factory({ + 'state': value + }); + }; + } +}); + +tape( 'the function returns a pseudorandom number generator (no options)', function test( t ) { + var tinymt32; + var v; + var i; + + tinymt32 = factory(); + for ( i = 0; i < 1e3; i++ ) { + v = tinymt32(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( isNonNegativeInteger( v ), true, 'returns a nonnegative integer' ); + t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32-1 (inclusive)' ); + } + t.end(); +}); + +tape( 'the function returns a seeded pseudorandom number generator', function test( t ) { + var tinymt32a; + var tinymt32b; + var seed; + var v1; + var v2; + var i; + + seed = minstd(); + + tinymt32a = factory({ + 'seed': seed + }); + tinymt32b = factory({ + 'seed': seed + }); + + t.notEqual( tinymt32a, tinymt32b, 'separate generators' ); + + for ( i = 0; i < 1e3; i++ ) { + v1 = tinymt32a(); + v2 = tinymt32b(); + t.equal( v1, v2, 'both return same number' ); + } + t.end(); +}); + +tape( 'attached to the returned function is the generator name', function test( t ) { + var tinymt32 = factory(); + t.equal( tinymt32.NAME, 'tinymt32', 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the minimum possible generated number', function test( t ) { + var tinymt32 = factory(); + t.equal( tinymt32.MIN, 0, 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the maximum possible generated number', function test( t ) { + var tinymt32 = factory(); + t.equal( tinymt32.MAX, UINT32_MAX, 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator seed', function test( t ) { + var tinymt32; + var actual; + + tinymt32 = factory({ + 'seed': 12345 + }); + actual = tinymt32.seed; + + t.equal( isUint32Array( actual ), true, 'has property' ); + t.equal( actual.length, 1, 'has expected length' ); + t.equal( actual[ 0 ], 12345, 'equal to provided seed' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator state', function test( t ) { + var tinymt32 = factory(); + t.equal( isUint32Array( tinymt32.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator state length', function test( t ) { + var tinymt32 = factory(); + t.equal( typeof tinymt32.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator state size', function test( t ) { + var tinymt32 = factory(); + t.equal( typeof tinymt32.byteLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is a method to serialize the generator as a JSON object', function test( t ) { + var tinymt32; + var o; + + tinymt32 = factory(); + t.equal( typeof tinymt32.toJSON, 'function', 'has method' ); + + o = tinymt32.toJSON(); + t.equal( o.type, 'PRNG', 'has property' ); + t.equal( o.name, tinymt32.NAME, 'has property' ); + t.deepEqual( o.state, typedarray2json( tinymt32.state ), 'has property' ); + t.deepEqual( o.params, [], 'has property' ); + + t.end(); +}); + +tape( 'attached to the returned function is a `normalized` method for generating pseudorandom numbers strictly between 0 (inclusive) and 1 (exclusive)', function test( t ) { + var tinymt32; + var v; + var i; + + tinymt32 = factory(); + for ( i = 0; i < 1e3; i++ ) { + v = tinymt32.normalized(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( v >= 0.0 && v < 1.0, true, 'returns a number between 0 (inclusive) and 1 (exclusive)' ); + } + t.end(); +}); + +tape( 'the `normalized` method returns pseudorandom numbers drawn from a uniform distribution', opts, function test( t ) { + var threshold; + var count; + var npass; + var N; + var x; + + threshold = 0.10; + + x = []; + N = 500; + + count = -1; + npass = 0; + + gof(); + + function gof() { + var rejected; + var tinymt32; + var pValue; + var bool; + var i; + var j; + + for ( i = 0; i < 1e3; i++ ) { + x.push( 0 ); + } + count += 1; + rejected = 0; + for ( i = 0; i < N; i++ ) { + tinymt32 = factory(); + t.ok( true, 'seed: '+tinymt32.seed ); + for ( j = 0; j < x.length; j++ ) { + x[ j ] = tinymt32.normalized(); + if ( x[ j ] < 0.0 || x[ j ] > 1.0 ) { + t.ok( false, 'returned a number outside support: '+x[ j ] ); + } + } + // Test using Kolmogorov-Smirnov goodness-of-fit test: + pValue = kstest( x, 'uniform', 0.0, 1.0 ).pValue; + t.equal( typeof pValue, 'number', 'returns a p-value: '+pValue ); + if ( pValue < 0.05 ) { + rejected += 1; + } + } + // Account for small sample size and few repeats... + bool = ( rejected / N < threshold ); + + // If we succeed the first time, we are done... + if ( count === 0 && bool ) { + return done( bool, rejected ); + } + // Retry mode... + if ( bool ) { + npass += 1; + } + // Retry twice... + if ( count < 2 ) { + return gof(); + } + // Both retries must succeed for test to pass: + bool = ( npass >= 2 ); + return done( bool, rejected ); + } + + function done( bool, rejected ) { + t.ok( bool, 'null hypothesis (i.e., that numbers are drawn from Uniform(0,1)) is rejected in less than '+(threshold*100)+'% of cases ('+rejected+' of '+N+'). Repeats: '+npass+' of '+count+'.' ); + t.end(); + } +}); + +tape( 'the function returns pseudorandom numbers from a TinyMT32 PRNG (integer seed)', function test( t ) { + var expected; + var rand; + var i; + + expected = UINT32_INTEGER_SEED.expected; + rand = factory({ + 'seed': 1234 + }); + for ( i = 0; i < expected.length; i++ ) { + t.equal( rand(), expected[ i ], 'returns expected value for iteration '+i ); + } + t.end(); +}); + +tape( 'the `normalized` method returns pseudorandom double-precision floating-point numbers from a TinyMT32 PRNG (integer seed)', function test( t ) { + var expected; + var opts; + var rand; + var i; + + expected = FLOAT64_INTEGER_SEED.expected; + + opts = { + 'seed': 1234 + }; + rand = factory( opts ).normalized; + for ( i = 0; i < expected.length; i++ ) { + t.equal( rand(), expected[ i ], 'returns expected value for iteration '+i ); + } + t.end(); +}); + +tape( 'the function returns pseudorandom numbers from a TinyMT32 PRNG (array seed)', function test( t ) { + var expected; + var rand; + var i; + + expected = UINT32_ARRAY_SEED.expected; + rand = factory({ + 'seed': [ 0x123, 0x234, 0x345, 0x456 ] + }); + for ( i = 0; i < expected.length; i++ ) { + t.equal( rand(), expected[ i ], 'returns expected value for iteration '+i ); + } + t.end(); +}); + +tape( 'the `normalized` method returns pseudorandom double-precision floating-point numbers from a TinyMT32 PRNG (array seed)', function test( t ) { + var expected; + var opts; + var rand; + var i; + + expected = FLOAT64_ARRAY_SEED.expected; + + opts = { + 'seed': [ 0x123, 0x234, 0x345, 0x456 ] + }; + rand = factory( opts ).normalized; + for ( i = 0; i < expected.length; i++ ) { + t.equal( rand(), expected[ i ], 'returns expected value for iteration '+i ); + } + t.end(); +}); + +tape( 'the function supports specifying the generator state', function test( t ) { + var tinymt32; + var state; + var arr; + var i; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + + // Create another PRNG using the captured state: + tinymt32 = factory({ + 'state': state + }); + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( tinymt32(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the returned function supports setting the generator state', function test( t ) { + var tinymt32; + var state; + var arr; + var i; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + // Set the state: + tinymt32.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( tinymt32(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the returned function supports setting the generator state (normalized)', function test( t ) { + var tinymt32; + var state; + var arr; + var i; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32.normalized(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32.normalized() ); + } + // Set the state: + tinymt32.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( tinymt32.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator name', function test( t ) { + var tinymt32 = factory(); + t.equal( tinymt32.normalized.NAME, 'tinymt32', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the minimum possible generated number', function test( t ) { + var tinymt32 = factory(); + t.equal( tinymt32.normalized.MIN, 0.0, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the maximum possible generated number', function test( t ) { + var tinymt32 = factory(); + t.equal( tinymt32.normalized.MAX, FLOAT64_MAX_SAFE_INTEGER/(FLOAT64_MAX_SAFE_INTEGER+1), 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator seed', function test( t ) { + var tinymt32; + var actual; + + tinymt32 = factory({ + 'seed': 12345 + }); + actual = tinymt32.normalized.seed; + + t.equal( isUint32Array( actual ), true, 'has property' ); + t.equal( actual.length, 1, 'has expected length' ); + t.equal( actual[ 0 ], 12345, 'equal to provided seed' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state', function test( t ) { + var tinymt32 = factory(); + t.equal( isUint32Array( tinymt32.normalized.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state length', function test( t ) { + var tinymt32 = factory(); + t.equal( typeof tinymt32.normalized.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state size', function test( t ) { + var tinymt32 = factory(); + t.equal( typeof tinymt32.normalized.byteLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is a method to serialize the generator as a JSON object', function test( t ) { + var tinymt32; + var o; + + tinymt32 = factory(); + t.equal( typeof tinymt32.normalized.toJSON, 'function', 'has method' ); + + o = tinymt32.normalized.toJSON(); + t.equal( o.type, 'PRNG', 'has property' ); + t.equal( o.name, tinymt32.normalized.NAME, 'has property' ); + t.deepEqual( o.state, typedarray2json( tinymt32.normalized.state ), 'has property' ); + + t.end(); +}); + +tape( 'if the `state` property is set to a value other than a Uint32Array, an error is thrown', function test( t ) { + var tinymt32; + var values; + var i; + + tinymt32 = factory(); + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when set to '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + tinymt32.state = value; + }; + } +}); + +tape( 'if the `state` property is set to a Uint32Array having an unexpected length, an error is thrown', function test( t ) { + var tinymt32; + var values; + var i; + + tinymt32 = factory(); + + values = [ + new Uint32Array( 0 ), + new Uint32Array( 10 ), + new Uint32Array( 20 ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when set to '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + tinymt32.state = value; + }; + } +}); + +tape( 'if the `state` property is set to a Uint32Array containing an invalid version, an error is thrown', function test( t ) { + var tinymt32; + var values; + var v; + var i; + + tinymt32 = factory(); + + values = []; + + v = new Uint32Array( 8 ); + v[ 0 ] = 0; // version + values.push( v ); + + v = new Uint32Array( 8 ); + v[ 0 ] = 2; // version + values.push( v ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + tinymt32.state = value; + }; + } +}); + +tape( 'if the `state` property is set to a Uint32Array containing an invalid number of sections, an error is thrown', function test( t ) { + var tinymt32; + var values; + var v; + var i; + + tinymt32 = factory(); + + values = []; + + v = new Uint32Array( 8 ); + v[ 0 ] = 1; // version + v[ 1 ] = 0; // num sections + values.push( v ); + + v = new Uint32Array( 8 ); + v[ 0 ] = 1; // version + v[ 1 ] = 5; // num sections + values.push( v ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + tinymt32.state = value; + }; + } +}); + +tape( 'if the `state` property is set to a value other than a Uint32Array, an error is thrown (normalized)', function test( t ) { + var tinymt32; + var values; + var i; + + tinymt32 = factory(); + + values = [ + '3', + 3, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when set to '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + tinymt32.normalized.state = value; + }; + } +}); + +tape( 'if the `state` property is set to a Uint32Array having an unexpected length, an error is thrown (normalized)', function test( t ) { + var tinymt32; + var values; + var i; + + tinymt32 = factory(); + + values = [ + new Uint32Array( 0 ), + new Uint32Array( 10 ), + new Uint32Array( 20 ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), RangeError, 'throws an error when set to '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + tinymt32.normalized.state = value; + }; + } +}); + +tape( 'the function supports specifying a shared generator state', function test( t ) { + var tinymt32; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + var j; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Uint32Array( state ); + + // Create PRNGs using the captured state: + rand1 = factory({ + 'state': shared, + 'copy': false + }); + rand2 = factory({ + 'state': shared, + 'copy': false + }); + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + t.end(); +}); + +tape( 'the function supports specifying a shared generator state (normalized)', function test( t ) { + var tinymt32; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + var j; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32.normalized(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32.normalized() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Uint32Array( state ); + + // Create PRNGs using the captured state: + rand1 = factory({ + 'state': shared, + 'copy': false + }); + rand2 = factory({ + 'state': shared, + 'copy': false + }); + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1.normalized(); + v2 = rand2.normalized(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + t.end(); +}); + +tape( 'the function returns a pseudorandom number generator (options; no seed)', function test( t ) { + var tinymt32; + var v; + var i; + + tinymt32 = factory( {} ); + for ( i = 0; i < 1e3; i++ ) { + v = tinymt32(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( isNonNegativeInteger( v ), true, 'returns a nonnegative integer' ); + t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32-1 (inclusive)' ); + } + t.end(); +}); + +tape( 'the returned function supports setting the generator state to a state array having a different length', function test( t ) { + var tinymt32; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + + // Seed length: 2 + tinymt32 = factory({ + 'seed': [ 1234, 5678 ] + }); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Uint32Array( state ); + + // Create PRNGs having seed lengths equal to 1: + rand1 = factory({ + 'seed': [ 6789 ] + }); + rand2 = factory({ + 'seed': [ 4321 ] + }); + + // Move to future states... + for ( i = 0; i < 100; i++ ) { + v1 = rand1(); + v2 = rand2(); + } + + // Reset the PRNG states: + rand1.state = shared; + rand2.state = shared; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + v1 = rand1(); + t.equal( v1, arr[ i ], 'returns expected value. i: '+i+'.' ); + v2 = rand2(); + t.equal( v2, arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the returned function supports setting a shared generator state (same length)', function test( t ) { + var tinymt32; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + var j; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Uint32Array( state ); + + // Create PRNGs using the captured state: + rand1 = factory({ + 'state': shared, + 'copy': false + }); + rand2 = factory({ + 'state': shared, + 'copy': false + }); + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + v2 = rand2(); + } + + // Reset the (shared) state: + rand1.state = new Uint32Array( state ); + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + t.end(); +}); + +tape( 'the returned function supports setting a shared generator state (different length)', function test( t ) { + var tinymt32; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var s; + var i; + var j; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Uint32Array( state ); + + // Create PRNGs using the captured state: + rand1 = factory({ + 'state': shared, + 'copy': false + }); + rand2 = factory({ + 'state': shared, + 'copy': false + }); + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + v2 = rand2(); + } + + // Reset the (*previously* shared) state: + s = new Uint32Array( state.length+1 ); + gcopy( state.length, state, 1, s, 1 ); + s[ s.length-3 ] = 2; + s[ s.length-1 ] = 1234; + rand1.state = s; + + // Attempt to replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + + // `rand1()` state is not affected by `rand2()`: + t.equal( v1, arr[ i ], 'returns expected value. i: '+i+'.' ); + + // `rand2()` state was never reset: + t.notEqual( v2, arr[ j+1 ], 'does not return expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + + // Reset the (*previously* shared) state: + rand2.state = s; + + // Reset to a shared state: + shared = new Uint32Array( state ); + rand1.state = shared; + rand2.state = shared; + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + t.end(); +}); + +tape( 'the returned function supports setting a shared generator state (no initial shared state)', function test( t ) { + var tinymt32; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + var j; + + tinymt32 = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Uint32Array( state ); + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + + // Create PRNGs using the captured state: + rand1 = factory({ + 'copy': false + }); + rand2 = factory({ + 'copy': false + }); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + v1 = rand1(); + v2 = rand2(); + } + + // Reset to a shared state: + rand1.state = shared; + rand2.state = shared; + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Uint32Array( state ); + + // Reset the (shared) state: + rand1.state = shared; + rand2.state = shared; + + // Replay previously generated values... + j = 0; + for ( i = 0; i < 50; i++ ) { + v1 = rand1(); + v2 = rand2(); + t.equal( v1, arr[ j ], 'returns expected value. i: '+j+'.' ); + t.equal( v2, arr[ j+1 ], 'returns expected value. i: '+(j+1)+'.' ); + j += 2; // stride + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/random/base/tinymt32/test/test.js b/lib/node_modules/@stdlib/random/base/tinymt32/test/test.js new file mode 100644 index 000000000000..fd4c32522c58 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/tinymt32/test/test.js @@ -0,0 +1,241 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); +var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' ); +var tinymt32 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof tinymt32, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is a method to generate normalized pseudorandom numbers', function test( t ) { + t.equal( typeof tinymt32.normalized, 'function', 'has method' ); + t.end(); +}); + +tape( 'attached to the main export is a method to generate TinyMT32 pseudorandom number generator', function test( t ) { + t.equal( typeof tinymt32.factory, 'function', 'has method' ); + t.end(); +}); + +tape( 'attached to the main export is a method to serialize a pseudorandom number generator as JSON', function test( t ) { + t.equal( typeof tinymt32.toJSON, 'function', 'has method' ); + t.end(); +}); + +tape( 'attached to the main export is the generator name', function test( t ) { + t.equal( tinymt32.NAME, 'tinymt32', 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the minimum possible generated number', function test( t ) { + t.equal( tinymt32.MIN, 0, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the maximum possible generated number', function test( t ) { + t.equal( tinymt32.MAX, UINT32_MAX, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator seed', function test( t ) { + t.equal( isUint32Array( tinymt32.seed ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator seed length', function test( t ) { + t.equal( typeof tinymt32.seedLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator state', function test( t ) { + t.equal( isUint32Array( tinymt32.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator state length', function test( t ) { + t.equal( typeof tinymt32.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator state size', function test( t ) { + t.equal( typeof tinymt32.byteLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'the function returns pseudorandom integers between 0 and 2^32-1 (inclusive)', function test( t ) { + var v; + var i; + for ( i = 0; i < 1e3; i++ ) { + v = tinymt32(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( isNonNegativeInteger( v ), true, 'returns a positive integer' ); + t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32-1 (inclusive)' ); + } + t.end(); +}); + +tape( 'the `normalized` method returns pseudorandom numbers strictly between 0 (inclusive) and 1 (exclusive)', function test( t ) { + var v; + var i; + for ( i = 0; i < 1e3; i++ ) { + v = tinymt32.normalized(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( v >= 0.0 && v < 1.0, true, 'returns a number between 0 (inclusive) and 1 (exclusive)' ); + } + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator name', function test( t ) { + t.equal( tinymt32.normalized.NAME, 'tinymt32', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the minimum possible generated number', function test( t ) { + t.equal( tinymt32.normalized.MIN, 0.0, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the maximum possible generated number', function test( t ) { + t.equal( tinymt32.normalized.MAX, FLOAT64_MAX_SAFE_INTEGER / ( FLOAT64_MAX_SAFE_INTEGER + 1 ), 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator seed', function test( t ) { + t.equal( isUint32Array( tinymt32.normalized.seed ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator seed length', function test( t ) { + t.equal( typeof tinymt32.normalized.seedLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state', function test( t ) { + t.equal( isUint32Array( tinymt32.normalized.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state length', function test( t ) { + t.equal( typeof tinymt32.normalized.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state size', function test( t ) { + t.equal( typeof tinymt32.normalized.byteLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is a method to serialize a pseudorandom number generator as JSON', function test( t ) { + t.equal( typeof tinymt32.normalized.toJSON, 'function', 'has method' ); + t.end(); +}); + +tape( 'the generator supports setting the generator state', function test( t ) { + var state; + var arr; + var i; + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32() ); + } + // Set the state: + tinymt32.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( tinymt32(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the generator supports setting the generator state (normalized)', function test( t ) { + var state; + var arr; + var i; + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32.normalized(); + } + // Capture the current state: + state = tinymt32.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32.normalized() ); + } + // Set the state: + tinymt32.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( tinymt32.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the generator supports setting the generator state (normalized)', function test( t ) { + var state; + var arr; + var i; + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + tinymt32.normalized(); + } + // Capture the current state: + state = tinymt32.normalized.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( tinymt32.normalized() ); + } + // Set the state: + tinymt32.normalized.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( tinymt32.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/types/index.d.ts b/lib/node_modules/@stdlib/types/index.d.ts index 8e01ad12ad4d..96f0a72ae85f 100644 --- a/lib/node_modules/@stdlib/types/index.d.ts +++ b/lib/node_modules/@stdlib/types/index.d.ts @@ -4256,6 +4256,25 @@ declare module '@stdlib/types/random' { */ type PRNGStateMT19937 = Uint32Array; + /** + * A pseudorandom number generator (PRNG) seed for the 32-bit Tiny Mersenne Twister (TinyMT32) PRNG. + * + * @example + * const s: PRNGSeedTinyMT32 = 12345; + * + * @example + * const s: PRNGSeedTinyMT32 = [ 12345, 67891 ]; + */ + type PRNGSeedTinyMT32 = number | ArrayLike; + + /** + * A pseudorandom number generator (PRNG) state for the 32-bit Tiny Mersenne Twister (TinyMT32) PRNG. + * + * @example + * const s: PRNGStateTinyMT32 = new Uint32Array( 4 ); + */ + type PRNGStateTinyMT32 = Uint32Array; + /** * A pseudorandom number generator (PRNG) seed for the MINSTD PRNG. * diff --git a/tools/docs/jsdoc/typedefs/random.js b/tools/docs/jsdoc/typedefs/random.js index 1aaaddb2ae3a..14e89db4efc3 100644 --- a/tools/docs/jsdoc/typedefs/random.js +++ b/tools/docs/jsdoc/typedefs/random.js @@ -1,3 +1,21 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + /** * A pseudorandom number generator (PRNG). * @@ -27,3 +45,15 @@ * * @type {Int32Array} PRNGStateMINSTD */ + +/** +* A pseudorandom number generator (PRNG) seed for the 32-bit Tiny Mersenne Twister (TinyMT32) PRNG. +* +* @type {(uinteger32|Collection)} PRNGSeedTinymt32 +*/ + +/** +* A pseudorandom number generator (PRNG) state for the 32-bit Tiny Mersenne Twister (TinyMT32) PRNG. +* +* @type {Uint32Array} PRNGStateTinymt32 +*/