From f38e07c903ec0a226281e4ff499f60be47e4c038 Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Wed, 2 Apr 2025 06:37:27 +0600 Subject: [PATCH 1/9] chore: initial commit --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/random/base/pcg32/README.md | 345 ++++ .../base/pcg32/benchmark/benchmark.factory.js | 104 ++ .../random/base/pcg32/benchmark/benchmark.js | 67 + .../base/pcg32/benchmark/benchmark.to_json.js | 48 + .../random/base/pcg32/benchmark/c/Makefile | 146 ++ .../random/base/pcg32/benchmark/c/benchmark.c | 373 ++++ .../@stdlib/random/base/pcg32/docs/repl.txt | 188 ++ .../random/base/pcg32/docs/types/index.d.ts | 197 ++ .../random/base/pcg32/docs/types/test.ts | 106 ++ .../random/base/pcg32/examples/c/Makefile | 146 ++ .../random/base/pcg32/examples/c/example.c | 75 + .../random/base/pcg32/examples/index.js | 47 + .../pcg32/include/stdlib/random/base/minstd.h | 69 + .../@stdlib/random/base/pcg32/lib/factory.js | 416 +++++ .../@stdlib/random/base/pcg32/lib/index.js | 57 + .../@stdlib/random/base/pcg32/lib/main.js | 102 + .../random/base/pcg32/lib/rand_int32.js | 52 + .../@stdlib/random/base/pcg32/manifest.json | 40 + .../@stdlib/random/base/pcg32/package.json | 77 + .../@stdlib/random/base/pcg32/src/main.c | 430 +++++ .../random/base/pcg32/test/test.factory.js | 1634 +++++++++++++++++ .../@stdlib/random/base/pcg32/test/test.js | 240 +++ 22 files changed, 4959 insertions(+) create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/README.md create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/examples/index.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/minstd.h create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/lib/index.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/lib/main.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/lib/rand_int32.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/manifest.json create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/package.json create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/src/main.c create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js create mode 100644 lib/node_modules/@stdlib/random/base/pcg32/test/test.js diff --git a/lib/node_modules/@stdlib/random/base/pcg32/README.md b/lib/node_modules/@stdlib/random/base/pcg32/README.md new file mode 100644 index 000000000000..f4ceec90e71c --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/README.md @@ -0,0 +1,345 @@ + + +# MINSTD + +> A linear congruential pseudorandom number generator ([LCG][lcg]) based on Park and Miller. + +
+ +## Usage + +```javascript +var minstd = require( '@stdlib/random/base/minstd' ); +``` + +#### minstd() + +Returns a pseudorandom integer on the interval `[1, 2147483646]`. + +```javascript +var r = minstd(); +// returns +``` + +#### minstd.normalized() + +Returns a pseudorandom number on the interval `[0,1)`. + +```javascript +var r = minstd.normalized(); +// returns +``` + +#### minstd.factory( \[options] ) + +Returns a linear congruential pseudorandom number generator ([LCG][lcg]). + +```javascript +var rand = minstd.factory(); +``` + +The function accepts the following `options`: + +- **seed**: pseudorandom number generator seed. +- **state**: an [`Int32Array`][@stdlib/array/int32] 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 an `integer` on the interval `[1, 2147483646]` + +```javascript +var rand = minstd.factory({ + 'seed': 1234 +}); + +var r = rand(); +// returns 20739838 +``` + +or, for arbitrary length seeds, an array-like `object` containing signed 32-bit integers + +```javascript +var Int32Array = require( '@stdlib/array/int32' ); + +var rand = minstd.factory({ + 'seed': new Int32Array( [ 1234 ] ) +}); + +var r = rand(); +// returns 20739838 +``` + +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 = minstd(); +} + +// Create a new PRNG initialized to the current state of `minstd`: +var rand = minstd.factory({ + 'state': minstd.state +}); + +// Test that the generated pseudorandom numbers are the same: +var bool = ( rand() === minstd() ); +// returns true +``` + +#### minstd.NAME + +The generator name. + +```javascript +var str = minstd.NAME; +// returns 'minstd' +``` + +#### minstd.MIN + +Minimum possible value. + +```javascript +var min = minstd.MIN; +// returns 1 +``` + +#### minstd.MAX + +Maximum possible value. + +```javascript +var max = minstd.MAX; +// returns 2147483646 +``` + +#### minstd.seed + +The value used to seed `minstd()`. + +```javascript +// Generate pseudorandom values... +var r; +var i; +for ( i = 0; i < 100; i++ ) { + r = minstd(); +} + +// Generate the same pseudorandom values... +var rand = minstd.factory({ + 'seed': minstd.seed +}); +for ( i = 0; i < 100; i++ ) { + r = rand(); +} +``` + +#### minstd.seedLength + +Length of generator seed. + +```javascript +var len = minstd.seedLength; +// returns +``` + +#### minstd.state + +Writable property for getting and setting the generator state. + +```javascript +var r = minstd(); +// returns + +r = minstd(); +// returns + +// ... + +// Get the current state: +var state = minstd.state; +// returns + +r = minstd(); +// returns + +r = minstd(); +// returns + +// Reset the state: +minstd.state = state; + +// Replay the last two pseudorandom numbers: +r = minstd(); +// returns + +r = minstd(); +// returns + +// ... +``` + +#### minstd.stateLength + +Length of generator state. + +```javascript +var len = minstd.stateLength; +// returns +``` + +#### minstd.byteLength + +Size (in bytes) of generator state. + +```javascript +var sz = minstd.byteLength; +// returns +``` + +#### minstd.toJSON() + +Serializes the pseudorandom number generator as a JSON object. + +```javascript +var o = minstd.toJSON(); +// returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } +``` + +
+ + + +
+ +## Notes + +- The generator has a period of approximately `2.1e9` (see [Numerical Recipes in C, 2nd Edition](#references), p. 279). +- An [LCG][lcg] is fast and uses little memory. On the other hand, because the generator is a simple [linear congruential generator][lcg], the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. For more on the advantages and disadvantages of [LCGs][lcg], see [Wikipedia][pros-cons]. +- 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). + +
+ + + +
+ +## Examples + + + +```javascript +var minstd = require( '@stdlib/random/base/minstd' ); + +// Generate pseudorandom numbers... +var i; +for ( i = 0; i < 100; i++ ) { + console.log( minstd() ); +} + +// Create a new pseudorandom number generator... +var seed = 1234; +var rand = minstd.factory({ + 'seed': seed +}); +for ( i = 0; i < 100; i++ ) { + console.log( rand() ); +} + +// Create another pseudorandom number generator using a previous seed... +rand = minstd.factory({ + 'seed': minstd.seed +}); +for ( i = 0; i < 100; i++ ) { + console.log( rand() ); +} +``` + +
+ + + +* * * + +
+ +## References + +- Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042][@park:1988]. +- Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press. + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js new file mode 100644 index 000000000000..ca16eef6c11a --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js @@ -0,0 +1,104 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 Int32Array = require( '@stdlib/array/int32' ); +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 = 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(); +}); + +bench( pkg+':factory:seed=', function benchmark( b ) { + var seed; + var opts; + var f; + var i; + + opts = {}; + + seed = new Int32Array( 10 ); + for ( i = 0; i < seed.length; i++ ) { + seed[ i ] = 123 + i; + } + 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/pcg32/benchmark/benchmark.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js new file mode 100644 index 000000000000..829f67d30c8c --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 minstd = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = minstd(); + 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 = minstd.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/pcg32/benchmark/benchmark.to_json.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js new file mode 100644 index 000000000000..d3124e9b9ec6 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js @@ -0,0 +1,48 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 rand = require( './../lib' ); + + +// MAIN // + +bench( pkg+':toJSON', function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = rand.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/pcg32/benchmark/c/Makefile b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile new file mode 100644 index 000000000000..c12ea1918c97 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2018 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/pcg32/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c new file mode 100644 index 000000000000..95854406f4e0 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c @@ -0,0 +1,373 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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/minstd.h" +#include "stdlib/random/base/shared.h" +#include +#include +#include +#include +#include +#include + +#define NAME "base/minstd" +#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 ) { + double elapsed; + double t; + int i; + + struct BasePRNGObject *obj; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + obj = stdlib_base_random_minstd_allocate( i+1 ); + if ( obj == NULL || obj->prng->min != 1 ) { + printf( "unexpected result\n" ); + break; + } + if ( i < ITERATIONS-1 ) { + stdlib_base_random_minstd_free( obj ); + } + } + elapsed = tic() - t; + + if ( obj == NULL || obj->prng->min != 1 ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_minstd_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark2( void ) { + double elapsed; + int8_t status; + uint64_t max; + uint64_t v; + double t; + int i; + + struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + 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_minstd_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark3( void ) { + double elapsed; + int8_t status; + double v; + double t; + int i; + + struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + 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_minstd_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark4( void ) { + double elapsed; + int8_t status; + int32_t v; + double t; + int i; + + struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + // NOTE: this is likely to be optimized away by a modern compiler, making this benchmark meaningless. + status = stdlib_base_random_minstd_seed( obj, &v ); + if ( status != 0 || v != 12345 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + + if ( status != 0 || v != 12345 ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_minstd_free( obj ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark5( void ) { + double elapsed; + void *state; + double t; + int i; + + struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + state = stdlib_base_random_minstd_state( obj ); + if ( state == NULL ) { + printf( "unexpected result\n" ); + break; + } + if ( i < ITERATIONS-1 ) { + free( state ); + } + } + elapsed = tic() - t; + + if ( state == NULL ) { + printf( "unexpected result\n" ); + } + stdlib_base_random_minstd_free( obj ); + free( state ); + + return elapsed; +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark6( void ) { + stdlib_base_random_minstd_state_t *states[2]; + double elapsed; + int8_t status; + double t; + int i; + + struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + if ( obj == NULL ) { + printf( "unable to allocate memory\n" ); + exit( 1 ); + } + for ( i = 0; i < 2; i++ ) { + states[i] = (stdlib_base_random_minstd_state_t *)malloc( sizeof( stdlib_base_random_minstd_state_t ) ); + states[i]->seed = 12345; + states[i]->state = 12345; + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + status = stdlib_base_random_minstd_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_minstd_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/pcg32/docs/repl.txt b/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt new file mode 100644 index 000000000000..2b114f22b965 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt @@ -0,0 +1,188 @@ + +{{alias}}() + Returns a pseudorandom integer on the interval `[1, 2147483646]`. + + This pseudorandom number generator (PRNG) is a linear congruential + pseudorandom number generator (LCG) based on Park and Miller. + + The generator has a period of approximately `2.1e9`. + + An LCG is fast and uses little memory. On the other hand, because the + generator is a simple LCG, the generator has recognized shortcomings. By + today's PRNG standards, the generator's period is relatively short. More + importantly, the "randomness quality" of the generator's output is lacking. + These defects make the generator unsuitable, for example, in Monte Carlo + simulations and in cryptographic applications. + + Returns + ------- + r: integer + Pseudorandom number. + + Examples + -------- + > var r = {{alias}}(); + + +{{alias}}.normalized() + Returns a pseudorandom number on the interval `[0,1)`. + + Returns + ------- + r: number + Pseudorandom number. + + Examples + -------- + > var r = {{alias}}.normalized(); + + +{{alias}}.factory( [options] ) + Returns a linear congruential pseudorandom number generator (LCG). + + Parameters + ---------- + options: Object (optional) + Options. + + options.seed: integer|ArrayLikeObject (optional) + Pseudorandom number generator seed. The seed may be either a positive + signed 32-bit integer on the interval `[1, 2147483646]` or, for + arbitrary length seeds, an array-like object containing signed 32-bit + integers. + + options.state: Int32Array (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() + 20739838 + + +{{alias}}.NAME + Generator name. + + Examples + -------- + > var str = {{alias}}.NAME + 'minstd' + + +{{alias}}.MIN + Minimum possible value. + + Examples + -------- + > var v = {{alias}}.MIN + 1 + + +{{alias}}.MAX + Maximum possible value. + + Examples + -------- + > var v = {{alias}}.MAX + 2147483646 + + +{{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 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/pcg32/docs/types/index.d.ts b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts new file mode 100644 index 000000000000..132fb796ce69 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts @@ -0,0 +1,197 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 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.PRNGSeedMINSTD; + + /** + * Pseudorandom number generator state. + */ + state?: random.PRNGStateMINSTD; + + /** + * 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.PRNGSeedMINSTD; + + /** + * PRNG seed length. + */ + readonly seedLength: number; + + /** + * PRNG state. + */ + state: random.PRNGStateMINSTD; + + /** + * 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 `[1, 2147483646]`. +*/ +interface NullaryFunction extends PRNG { + /** + * Returns a pseudorandom integer on the interval `[1, 2147483646]`. + * + * @returns pseudorandom number + */ + (): number; +} + +/** +* Interface for generating pseudorandom integers on the interval `[1, 2147483646]`. +*/ +interface Random extends PRNG { + /** + * Returns a pseudorandom integer on the interval `[1, 2147483646]`. + * + * ## Notes + * + * - This pseudorandom number generator (PRNG) is a linear congruential pseudorandom number generator (LCG) based on Park and Miller. + * - The generator has a period of approximately `2.1e9`. + * - An LCG is fast and uses little memory. On the other hand, because the generator is a simple LCG, the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. + * + * @returns pseudorandom number + * + * @example + * var v = minstd(); + * // returns + */ + (): number; + + /** + * Returns a pseudorandom number on the interval `[0,1)`. + * + * @returns pseudorandom number + * + * @example + * var r = minstd.normalized(); + * // returns + */ + normalized(): number; + + /** + * Returns a linear congruential pseudorandom number generator (LCG). + * + * @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 = minstd.factory(); + * var v = rand(); + * // returns + * + * @example + * var rand = minstd.factory({ + * 'seed': 12345 + * }); + * var v = rand(); + * // returns + */ + factory( options?: Options ): NullaryFunction; +} + +/** +* Returns a pseudorandom integer on the interval `[1, 2147483646]`. +* +* ## Notes +* +* - This pseudorandom number generator (PRNG) is a linear congruential pseudorandom number generator (LCG) based on Park and Miller. +* - The generator has a period of approximately `2.1e9`. +* - An LCG is fast and uses little memory. On the other hand, because the generator is a simple LCG, the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. +* +* @returns pseudorandom number +* +* @example +* var v = minstd(); +* // returns +* +* @example +* var v = minstd.normalized(); +* // returns +* +* @example +* var rand = minstd.factory({ +* 'seed': 12345 +* }); +* var v = rand(); +* // returns +*/ +declare var minstd: Random; + + +// EXPORTS // + +export = minstd; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts new file mode 100644 index 000000000000..860129c751ba --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts @@ -0,0 +1,106 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2019 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 minstd = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + minstd(); // $ExpectType number +} + +// The compiler throws an error if the function is provided any arguments... +{ + minstd( true ); // $ExpectError + minstd( 2, 3 ); // $ExpectError +} + +// Attached to main export is a `normalized` method which returns a number... +{ + minstd.normalized(); // $ExpectType number +} + +// The compiler throws an error if the `normalized` method is provided any number of arguments... +{ + minstd.normalized( true ); // $ExpectError + minstd.normalized( 123 ); // $ExpectError + minstd.normalized( 'abc' ); // $ExpectError +} + +// Attached to main export is a `factory` method which returns a function... +{ + minstd.factory(); // $ExpectType NullaryFunction + minstd.factory( { 'copy': false } ); // $ExpectType NullaryFunction +} + +// The `factory` method returns a function which returns a number... +{ + const fcn = minstd.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 = minstd.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... +{ + minstd.factory( null ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided a `seed` option which is not a valid seed... +{ + minstd.factory( { 'seed': true } ); // $ExpectError + minstd.factory( { 'seed': 'abc' } ); // $ExpectError + minstd.factory( { 'seed': null } ); // $ExpectError + minstd.factory( { 'seed': [ 'a' ] } ); // $ExpectError + minstd.factory( { 'seed': {} } ); // $ExpectError + minstd.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... +{ + minstd.factory( { 'state': 123 } ); // $ExpectError + minstd.factory( { 'state': 'abc' } ); // $ExpectError + minstd.factory( { 'state': null } ); // $ExpectError + minstd.factory( { 'state': [] } ); // $ExpectError + minstd.factory( { 'state': {} } ); // $ExpectError + minstd.factory( { 'state': true } ); // $ExpectError + minstd.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... +{ + minstd.factory( { 'copy': 123 } ); // $ExpectError + minstd.factory( { 'copy': 'abc' } ); // $ExpectError + minstd.factory( { 'copy': null } ); // $ExpectError + minstd.factory( { 'copy': [] } ); // $ExpectError + minstd.factory( { 'copy': {} } ); // $ExpectError + minstd.factory( { 'copy': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `factory` method is provided more than one argument... +{ + minstd.factory( {}, 2 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile new file mode 100644 index 000000000000..95c166477a67 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2018 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/pcg32/examples/c/example.c b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c new file mode 100644 index 000000000000..9a6aa701aced --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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/minstd.h" +#include "stdlib/random/base/shared.h" +#include +#include +#include + +int main( void ) { + int8_t status; + int32_t seed; + uint64_t v; + int32_t i; + double d; + + // Create a PRNG... + struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + if ( obj == NULL ) { + fprintf( stderr, "Error allocating memory.\n" ); + exit( 1 ); + } + + status = stdlib_base_random_minstd_seed( obj, &seed ); + if ( status != 0 ) { + printf( "Unable to retrieve the PRNG seed.\n" ); + exit( 1 ); + } + printf( "seed = %d\n", seed ); + + 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_minstd_free( obj ); +} diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js b/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js new file mode 100644 index 000000000000..45785eafd857 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js @@ -0,0 +1,47 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 minstd = require( './../lib' ); + +// Generate pseudorandom numbers... +console.log( '\nseed: %d', minstd.seed[ 0 ] ); +var i; +for ( i = 0; i < 100; i++ ) { + console.log( minstd() ); +} + +// Create a new pseudorandom number generator... +var seed = 1234; +var rand = minstd.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 = minstd.factory({ + 'seed': minstd.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/pcg32/include/stdlib/random/base/minstd.h b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/minstd.h new file mode 100644 index 000000000000..d17ac50471d8 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/minstd.h @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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_MINSTD_H +#define STDLIB_RANDOM_BASE_MINSTD_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; + uint32_t state; +} stdlib_base_random_minstd_state_t; + +/** +* Returns a pointer to a dynamically allocated PRNG. +*/ +struct BasePRNGObject * stdlib_base_random_minstd_allocate( const int32_t seed ); + +/** +* Frees a PRNG's allocated memory. +*/ +void stdlib_base_random_minstd_free( struct BasePRNGObject *obj ); + +/** +* Returns a PRNG seed. +*/ +int8_t stdlib_base_random_minstd_seed( const struct BasePRNGObject *obj, int32_t *out ); + +/** +* Returns a copy of the current PRNG state. +*/ +void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ); + +/** +* Sets the PRNG state. +*/ +int8_t stdlib_base_random_minstd_set( struct BasePRNGObject *obj, const void *state ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_RANDOM_BASE_MINSTD_H diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js new file mode 100644 index 000000000000..7d58d7a8a340 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js @@ -0,0 +1,416 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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. +*/ + +/* eslint-disable max-len */ + +'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 isInt32Array = require( '@stdlib/assert/is-int32array' ); +var format = require( '@stdlib/string/format' ); +var INT32_MAX = require( '@stdlib/constants/int32/max' ); +var Int32Array = require( '@stdlib/array/int32' ); +var gcopy = require( '@stdlib/blas/base/gcopy' ); +var typedarray2json = require( '@stdlib/array/to-json' ); +var randint32 = require( './rand_int32.js' ); + + +// VARIABLES // + +var NORMALIZATION_CONSTANT = (INT32_MAX - 1)|0; // asm type annotation +var MAX_SEED = (INT32_MAX - 1)|0; // asm type annotation +var A = 16807|0; // asm type annotation + +// 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 | seed_length | ...seed | + +// Define the index offset of the seed section in the state array: +var SEED_SECTION_OFFSET = 4; // | version | num_sections | state_length | ...state | seed_length | ...seed | + +// Define the length of the "fixed" length portion of the state array: +var STATE_FIXED_LENGTH = 5; // 1 (version) + 1 (num_sections) + 1 (state_length) + 1 (state) + 1 (seed_length) + + +// FUNCTIONS // + +/** +* Verifies state array integrity. +* +* @private +* @param {Int32Array} 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'; + } + // The state array must have a minimum length... + if ( state.length < STATE_FIXED_LENGTH+1 ) { + return new RangeError( format( 'invalid %s. State array has insufficient length.', s1 ) ); + } + // The first element of the state array must equal the supported state array schema version... + 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 ] ) ); + } + // The second element of the state array must contain the number of sections... + 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 ] ) ); + } + // The length of the "state" section must equal `1`... + if ( state[ STATE_SECTION_OFFSET ] !== 1 ) { + return new RangeError( format( 'invalid %s. State array has an incompatible state length. Expected: `%u`. Actual: `%u`.', s1, 1, state[ STATE_SECTION_OFFSET ] ) ); + } + // The length of the "seed" section much match the empirical length... + 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; +} + + +// MAIN // + +/** +* Returns a linear congruential pseudorandom number generator (LCG) based on Park and Miller. +* +* @param {Options} [options] - options +* @param {PRNGSeedMINSTD} [options.seed] - pseudorandom number generator seed +* @param {PRNGStateMINSTD} [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 signed 32-bit integer or an array-like object containing integers less than the maximum signed 32-bit integer +* @throws {RangeError} a numeric seed must be a positive integer less than the maximum signed 32-bit integer +* @throws {TypeError} state must be an `Int32Array` +* @throws {Error} must provide a valid state +* @throws {TypeError} `copy` option must be a boolean +* @returns {PRNG} LCG PRNG +* +* @example +* var minstd = factory(); +* +* var v = minstd(); +* // returns +* +* @example +* // Return a seeded LCG: +* var minstd = factory({ +* 'seed': 1234 +* }); +* +* var v = minstd(); +* // returns 20739838 +*/ +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 ( !isInt32Array( state ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an Int32Array. Option: `%s`.', 'state', state ) ); + } + err = verifyState( state, true ); + if ( err ) { + throw err; + } + if ( opts.copy === false ) { + STATE = state; + } else { + STATE = new Int32Array( state.length ); + gcopy( state.length, state, 1, STATE, 1 ); + } + // Create a state "view": + state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + + // Create a seed "view": + seed = new Int32Array( 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 > MAX_SEED ) { + throw new RangeError( format( 'invalid option. `%s` option must be a positive integer less than the maximum signed 32-bit integer. Option: `%u`.', 'seed', seed ) ); + } + seed |= 0; // asm type annotation + } else if ( isCollection( seed ) && seed.length > 0 ) { + slen = seed.length; + STATE = new Int32Array( STATE_FIXED_LENGTH+slen ); + + // Initialize sections: + STATE[ 0 ] = STATE_ARRAY_VERSION; + STATE[ 1 ] = NUM_STATE_SECTIONS; + STATE[ STATE_SECTION_OFFSET ] = 1; + STATE[ SEED_SECTION_OFFSET ] = slen; + + // Copy the provided seed array to prevent external mutation, as mutation would lead to an inability to reproduce PRNG values according to the PRNG's stated seed: + gcopy.ndarray( slen, seed, 1, 0, STATE, 1, SEED_SECTION_OFFSET+1 ); + + // Create a state "view": + state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + + // Create a seed "view": + seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), slen ); + + // Initialize the internal PRNG state: + state[ 0 ] = seed[ 0 ]; + } else { + throw new TypeError( format( 'invalid option. `%s` option must be either a positive integer less than the maximum signed 32-bit integer or an array-like object containing integer values less than the maximum signed 32-bit integer. Option: `%s`.', 'seed', seed ) ); + } + } else { + seed = randint32()|0; // asm type annotation + } + } + } else { + seed = randint32()|0; // asm type annotation + } + if ( state === void 0 ) { + STATE = new Int32Array( STATE_FIXED_LENGTH+1 ); + + // Initialize sections: + STATE[ 0 ] = STATE_ARRAY_VERSION; + STATE[ 1 ] = NUM_STATE_SECTIONS; + STATE[ STATE_SECTION_OFFSET ] = 1; + STATE[ SEED_SECTION_OFFSET ] = 1; + STATE[ SEED_SECTION_OFFSET+1 ] = seed; + + // Create a state "view": + state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + + // Create a seed "view": + seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + + // Initialize the internal PRNG state: + state[ 0 ] = seed[ 0 ]; + } + setReadOnly( minstd, 'NAME', 'minstd' ); + setReadOnlyAccessor( minstd, 'seed', getSeed ); + setReadOnlyAccessor( minstd, 'seedLength', getSeedLength ); + setReadWriteAccessor( minstd, 'state', getState, setState ); + setReadOnlyAccessor( minstd, 'stateLength', getStateLength ); + setReadOnlyAccessor( minstd, 'byteLength', getStateSize ); + setReadOnly( minstd, 'toJSON', toJSON ); + setReadOnly( minstd, 'MIN', 1 ); + setReadOnly( minstd, 'MAX', INT32_MAX-1 ); + setReadOnly( minstd, 'normalized', normalized ); + + setReadOnly( normalized, 'NAME', minstd.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', (minstd.MIN-1.0) / NORMALIZATION_CONSTANT ); + setReadOnly( normalized, 'MAX', (minstd.MAX-1.0) / NORMALIZATION_CONSTANT ); + + return minstd; + + /** + * Returns the PRNG seed. + * + * @private + * @returns {PRNGSeedMINSTD} seed + */ + function getSeed() { + var len = STATE[ SEED_SECTION_OFFSET ]; + return gcopy( len, seed, 1, new Int32Array( len ), 1 ); + } + + /** + * Returns the PRNG seed length. + * + * @private + * @returns {PositiveInteger} seed length + */ + function getSeedLength() { + return STATE[ SEED_SECTION_OFFSET ]; + } + + /** + * 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 PRNG state. + * + * ## Notes + * + * - The PRNG state array is comprised of a preamble followed by `2` sections: + * + * 0. preamble (version + number of sections) + * 1. internal PRNG state + * 2. PRNG seed + * + * - The first element of the PRNG state array preamble is the state array schema version. + * + * - The second element of the PRNG state array preamble is the number of state array sections (i.e., `2`). + * + * - The first element of each section following the preamble specifies the section length. The remaining section elements comprise the section contents. + * + * @private + * @returns {PRNGStateMINSTD} current state + */ + function getState() { + var len = STATE.length; + return gcopy( len, STATE, 1, new Int32Array( len ), 1 ); + } + + /** + * 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 {PRNGStateMINSTD} s - generator state + * @throws {TypeError} must provide an `Int32Array` + * @throws {Error} must provide a valid state + */ + function setState( s ) { + var err; + if ( !isInt32Array( s ) ) { + throw new TypeError( format( 'invalid argument. Must provide an Int32Array. 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 Int32Array( s.length ); // reallocate + } + gcopy( s.length, s, 1, STATE, 1 ); + } + // Create a new state "view": + state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + + // Create a new seed "view": + seed = new Int32Array( 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 = minstd.NAME; + out.state = typedarray2json( STATE ); + out.params = []; + return out; + } + + /** + * Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\). + * + * @private + * @returns {integer32} pseudorandom integer + */ + function minstd() { + var s = state[ 0 ]|0; // asm type annotation + s = ( (A*s)%INT32_MAX )|0; // asm type annotation + state[ 0 ] = s; + return s|0; // asm type annotation + } + + /** + * Generates a pseudorandom number on the interval \\( [0,1) \\). + * + * @private + * @returns {number} pseudorandom number + */ + function normalized() { + return (minstd()-1) / NORMALIZATION_CONSTANT; + } +} + + +// EXPORTS // + +module.exports = factory; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js new file mode 100644 index 000000000000..4e6abeea302d --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 linear congruential pseudorandom number generator (LCG) based on Park and Miller. +* +* @module @stdlib/random/base/pcg32 +* +* @example +* var minstd = require( '@stdlib/random/base/pcg32' ); +* +* var v = minstd(); +* // returns +* +* @example +* var factory = require( '@stdlib/random/base/pcg32' ).factory; +* +* var minstd = factory({ +* 'seed': 1234 +* }); +* +* var v = minstd(); +* // returns 20739838 +*/ + +// 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/pcg32/lib/main.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js new file mode 100644 index 000000000000..f31a9faaa960 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js @@ -0,0 +1,102 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 randint32 = require( './rand_int32.js' ); + + +// MAIN // + +/** +* Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\). +* +* ## Method +* +* Linear congruential generators (LCGs) use the recurrence relation +* +* ```tex +* X_{n+1} = ( a \cdot X_n + c ) \operatorname{mod}(m) +* ``` +* +* where the modulus \\( m \\) is a prime number or power of a prime number and \\( a \\) is a primitive root modulo \\( m \\). +* +* +* +* For an LCG to be a Lehmer RNG, the seed \\( X_0 \\) must be coprime to \\( m \\). +* +* +* +* In this implementation, the constants \\( a \\), \\( c \\), and \\( m \\) have the values +* +* ```tex +* \begin{align*} +* a &= 7^5 = 16807 \\ +* c &= 0 \\ +* m &= 2^{31} - 1 = 2147483647 +* \end{align*} +* ``` +* +* +* +* The constant \\( m \\) is a Mersenne prime (modulo \\(31\\)). +* +* +* +* +* +* The constant \\( a \\) is a primitive root (modulo \\(31\\)). +* +* +* +* Accordingly, the maximum possible product is +* +* ```tex +* 16807 \cdot (m - 1) \approx 2^{46} +* ``` +* +* The values for \\( a \\), \\( c \\), and \\( m \\) are taken from Park and Miller, "Random Number Generators: Good Ones Are Hard To Find". Park's and Miller's article is also the basis for a recipe in the second edition of _Numerical Recipes in C_. +* +* ## Notes +* +* - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279). +* +* ## References +* +* - Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042](http://dx.doi.org/10.1145/63039.63042). +* - Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press. +* +* @function minstd +* @type {PRNG} +* @returns {PositiveInteger} pseudorandom integer +* +* @example +* var v = minstd(); +* // returns +*/ +var minstd = factory({ + 'seed': randint32() +}); + + +// EXPORTS // + +module.exports = minstd; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_int32.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_int32.js new file mode 100644 index 000000000000..925d4d6e299d --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_int32.js @@ -0,0 +1,52 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 INT32_MAX = require( '@stdlib/constants/int32/max' ); +var floor = require( '@stdlib/math/base/special/floor' ); + + +// VARIABLES // + +var MAX = INT32_MAX - 1; + + +// MAIN // + +/** +* Returns a pseudorandom integer on the interval \\([1, 2^{31}-1)\\). +* +* @private +* @returns {PositiveInteger} pseudorandom integer +* +* @example +* var v = randint32(); +* // returns +*/ +function randint32() { + var v = floor( 1.0 + (MAX*Math.random()) ); // eslint-disable-line stdlib/no-builtin-math + return v|0; // asm type annotation +} + + +// EXPORTS // + +module.exports = randint32; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/manifest.json b/lib/node_modules/@stdlib/random/base/pcg32/manifest.json new file mode 100644 index 000000000000..e91236537092 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/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/pcg32/package.json b/lib/node_modules/@stdlib/random/base/pcg32/package.json new file mode 100644 index 000000000000..db780495952b --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/package.json @@ -0,0 +1,77 @@ +{ + "name": "@stdlib/random/base/minstd", + "version": "0.0.0", + "description": "A linear congruential pseudorandom number generator (LCG) based on Park and Miller.", + "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", + "lcg", + "minstd", + "lehmer", + "park", + "miller", + "park-miller", + "seed", + "seedable" + ] +} diff --git a/lib/node_modules/@stdlib/random/base/pcg32/src/main.c b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c new file mode 100644 index 000000000000..59c1acf5fbe5 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c @@ -0,0 +1,430 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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/minstd.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 minstd_free( struct BasePRNGObject *obj ); + +// Define the LCG multiplier: +static const uint32_t A = 16807; + +// Define the maximum signed 32-bit integer: 2147483647 => 0x7fffffff => 01111111111111111111111111111111 +static const uint32_t MAX_INT32 = 0x7fffffff; + +// Define the normalization constant: +static const double NORMALIZATION_CONSTANT = 2147483646.0; // MAX_INT32 - 1 + +/** +* MINSTD PRNG. +* +*/ +static const struct BasePRNG minstd_prng = { + "minstd", // name + (uint64_t)1, // min + (uint64_t)MAX_INT32-1, // max: (2^{31}-1) - 1 + 0.0, // min (normalized) + (MAX_INT32-2) / NORMALIZATION_CONSTANT, // max (normalized): (MAX-1)/MAX + sizeof( stdlib_base_random_minstd_state_t ), // state_size + &next, // next() + &normalized, // normalized() + &minstd_free // free() +}; + +/** +* Returns a pseudorandom integer. +* +* ## Notes +* +* - The function returns `-1` if unable to generate a pseudorandom integer and `0` otherwise. +* +* @param obj PRNG object +* @param out output address +* @return status code +*/ +static inline int8_t next( struct BasePRNGObject *obj, uint64_t *out ) { + if ( obj == NULL || obj->prng != &minstd_prng ) { + return -1; + } + // Retrieve the state object: + stdlib_base_random_minstd_state_t *so = (stdlib_base_random_minstd_state_t *)( obj->state ); + + // Retrieve the current state: + uint32_t state = so->state; + + // Explicitly cast to 64-bit to handle integer overflow: + state = (A*(uint64_t)state) % MAX_INT32; + + // Update the PRNG state: + so->state = state; + + // Set the output value: + *out = (uint64_t)state; + + return 0; +} + +/** +* Returns a pseudorandom double-precision floating-point number on the interval `[0,1)`. +* +* ## Notes +* +* - The function returns `-1` if unable to generate a pseudorandom number and `0` otherwise. +* +* @param obj PRNG object +* @param out output address +* @return status code +*/ +static inline int8_t normalized( struct BasePRNGObject *obj, double *out ) { + uint64_t state; + int8_t status = next( obj, &state ); + if ( status != 0 ) { + return -1; + } + // Note: casting `state` to a double here is fine, as `state` will never exceed the maximum "safe" double-precision floating-point number: + *out = ((double)state-1.0) / NORMALIZATION_CONSTANT; + + return 0; +} + +/** +* Frees a PRNG's allocated memory. +* +* @param obj PRNG object +*/ +static inline void minstd_free( struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &minstd_prng ) { + return; + } + free( obj->state ); + free( obj ); +} + +/** +* Returns a pointer to a dynamically allocated PRNG. +* +* ## Notes +* +* - The user is responsible for freeing the allocated memory. +* - A provided `seed` is mapped to the interval `[1,2147483646]`. +* +* @param seed PRNG seed +* @return pointer to a dynamically allocated PRNG or, if unable to allocate memory, a null pointer +* +* @example +* #include "stdlib/random/base/minstd.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* 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_minstd_free( obj ); +*/ +struct BasePRNGObject * stdlib_base_random_minstd_allocate( const int32_t seed ) { + uint32_t iseed; + + struct BasePRNGObject *obj = malloc( sizeof( struct BasePRNGObject ) ); + if ( obj == NULL ) { + return NULL; + } + stdlib_base_random_minstd_state_t *state = malloc( sizeof( stdlib_base_random_minstd_state_t ) ); + if ( state == NULL ) { + free( obj ); // prevent memory leaks + return NULL; + } + // Ensure that the provided seed is within allowed bounds... + if ( seed == 0 ) { + iseed = 1; + } else if ( seed == MAX_INT32 ) { + iseed = MAX_INT32 - 1; + } else if ( seed < 0 ) { + iseed = -seed; + } else { + iseed = seed; + } + state->seed = (uint32_t)iseed; + state->state = (uint32_t)iseed; + + obj->prng = &minstd_prng; + obj->state = state; + + return obj; +} + +/** +* Frees a PRNG's allocated memory. +* +* @param obj PRNG object +* +* @example +* #include "stdlib/random/base/minstd.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* 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_minstd_free( obj ); +*/ +void stdlib_base_random_minstd_free( struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &minstd_prng ) { + return; + } + obj->prng->free( obj ); +} + +/** +* Returns a PRNG seed. +* +* ## Notes +* +* - The function returns `-1` if unable to resolve a PRNG seed and `0` otherwise. +* +* @param obj PRNG object +* @param out output address +* @return status code +* +* @example +* #include "stdlib/random/base/minstd.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* int32_t seed; +* int8_t status = stdlib_base_random_minstd_seed( obj, &seed ); +* 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_minstd_free( obj ); +*/ +int8_t stdlib_base_random_minstd_seed( const struct BasePRNGObject *obj, int32_t *out ) { + if ( obj == NULL || obj->prng != &minstd_prng ) { + return -1; + } + // Retrieve the MINSTD state object: + const stdlib_base_random_minstd_state_t *state = (stdlib_base_random_minstd_state_t *)( obj->state ); + + // Set the output value: + *out = (int32_t)( state->seed ); + + 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/minstd.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* if ( obj == NULL ) { +* fprintf( stderr, "Error allocating memory.\n" ); +* exit( 1 ); +* } +* +* void *state = stdlib_base_random_minstd_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_minstd_free( obj ); +* free( state ); +*/ +void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &minstd_prng ) { + return NULL; + } + void *state = malloc( obj->prng->state_size ); + if ( state == NULL ) { + return NULL; + } + memcpy( state, obj->state, obj->prng->state_size ); + return 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/minstd.h" +* #include "stdlib/random/base/shared.h" +* #include +* #include +* #include +* +* // Create a PRNG: +* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* 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... +* void *state = stdlib_base_random_minstd_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_minstd_set( obj, 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_minstd_free( obj ); +* free( state ); +*/ +int8_t stdlib_base_random_minstd_set( struct BasePRNGObject *obj, const void *state ) { + if ( obj == NULL || state == NULL || obj->prng != &minstd_prng ) { + return -1; + } + memcpy( obj->state, state, obj->prng->state_size ); + return 0; +} diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js new file mode 100644 index 000000000000..69898110a1ef --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js @@ -0,0 +1,1634 @@ +/* eslint-disable max-lines */ + +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 INT32_MAX = require( '@stdlib/constants/int32/max' ); +var now = require( '@stdlib/time/now' ); +var isPositiveInteger = require( '@stdlib/math/base/assert/is-positive-integer' ); +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var Int32Array = require( '@stdlib/array/int32' ); +var kstest = require( '@stdlib/stats/kstest' ); +var gcopy = require( '@stdlib/blas/base/gcopy' ); +var typedarray2json = require( '@stdlib/array/to-json' ); +var factory = require( './../lib/factory.js' ); + + +// VARIABLES // + +var opts = { + 'skip': ( ENV.TEST_MODE === 'coverage' ) +}; + + +// 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 or a non-empty array-like object, 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 or equal to the maximum signed 32-bit integer', function test( t ) { + var values; + var i; + + values = [ + INT32_MAX, + INT32_MAX + 1, + INT32_MAX + 2 + ]; + + 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 an Int32Array, 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( 'if provided a `state` option having an insufficient length, the factory function throws an error', function test( t ) { + var values; + var i; + + values = [ + new Int32Array( 0 ), + new Int32Array( 1 ) + ]; + + 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() { + factory({ + 'state': value + }); + }; + } +}); + +tape( 'if provided a `state` option containing an unsupported version, the factory function throws an error', function test( t ) { + var values; + var v; + var i; + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + factory({ + 'state': value + }); + }; + } +}); + +tape( 'if provided a `state` option containing an unsupported number of sections, the factory function throws an error', function test( t ) { + var values; + var v; + var i; + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 1; // version + v[ 1 ] = 3; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 1; // version + v[ 1 ] = 3; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + factory({ + 'state': value + }); + }; + } +}); + +tape( 'if provided a `state` option containing an unsupported state length, the factory function throws an error', function test( t ) { + var values; + var v; + var i; + + values = []; + + v = new Int32Array( 7 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 2; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // state + v[ 5 ] = 1; // seed length + v[ 6 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 8 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 2; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // state + v[ 5 ] = 1; // seed length + v[ 6 ] = 123; // seed + v[ 7 ] = 456; // seed + 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() { + factory({ + 'state': value + }); + }; + } +}); + +tape( 'if provided a `state` option containing an incompatible seed length, the factory function throws an error', function test( t ) { + var values; + var v; + var i; + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 9; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 9; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + factory({ + 'state': value + }); + }; + } +}); + +tape( 'the function returns a pseudorandom number generator (no options)', function test( t ) { + var minstd; + var v; + var i; + + minstd = factory(); + for ( i = 0; i < 1e3; i++ ) { + v = minstd(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); + t.equal( v >= 1 && v <= INT32_MAX-1, true, 'returns an integer between 1 and 2^31-1 (inclusive)' ); + } + t.end(); +}); + +tape( 'the function returns a pseudorandom number generator (options; no seed)', function test( t ) { + var minstd; + var v; + var i; + + minstd = factory( {} ); + for ( i = 0; i < 1e3; i++ ) { + v = minstd(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); + t.equal( v >= 1 && v <= INT32_MAX-1, true, 'returns an integer between 1 and 2^31-1 (inclusive)' ); + } + t.end(); +}); + +tape( 'the function returns a seeded pseudorandom number generator (integer seed)', function test( t ) { + var minstd1; + var minstd2; + var seed; + var v1; + var v2; + var i; + + seed = now(); + + minstd1 = factory({ + 'seed': seed + }); + minstd2 = factory({ + 'seed': seed + }); + + t.notEqual( minstd1, minstd2, 'separate generators' ); + + for ( i = 0; i < 1e3; i++ ) { + v1 = minstd1(); + v2 = minstd2(); + t.equal( v1, v2, 'both return same number' ); + } + t.end(); +}); + +tape( 'the function returns a seeded pseudorandom number generator (array seed)', function test( t ) { + var minstd1; + var minstd2; + var seed; + var v1; + var v2; + var i; + + seed = [ now() ]; + + minstd1 = factory({ + 'seed': seed + }); + minstd2 = factory({ + 'seed': seed + }); + + t.notEqual( minstd1, minstd2, 'separate generators' ); + + for ( i = 0; i < 1e3; i++ ) { + v1 = minstd1(); + v2 = minstd2(); + t.equal( v1, v2, 'both return same number' ); + } + t.end(); +}); + +tape( 'attached to the returned function is the generator name', function test( t ) { + var minstd = factory(); + t.equal( minstd.NAME, 'minstd', 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the minimum possible generated number', function test( t ) { + var minstd = factory(); + t.equal( minstd.MIN, 1, 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the maximum possible generated number', function test( t ) { + var minstd = factory(); + t.equal( minstd.MAX, INT32_MAX-1, 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator seed (integer seed)', function test( t ) { + var minstd; + var actual; + + minstd = factory({ + 'seed': 12345 + }); + actual = minstd.seed; + + t.equal( isInt32Array( 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 seed (array seed)', function test( t ) { + var minstd; + var actual; + var seed; + var i; + + seed = [ 12345 ]; + + minstd = factory({ + 'seed': seed + }); + actual = minstd.seed; + + t.equal( isInt32Array( actual ), true, 'has property' ); + for ( i = 0; i < seed.length; i++ ) { + t.equal( actual[ i ], seed[ i ], 'returns expected value for word '+i ); + } + t.end(); +}); + +tape( 'attached to the returned function is the generator seed length', function test( t ) { + var minstd = factory(); + t.equal( typeof minstd.seedLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator state', function test( t ) { + var minstd = factory(); + t.equal( isInt32Array( minstd.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator state length', function test( t ) { + var minstd = factory(); + t.equal( typeof minstd.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the returned function is the generator state size', function test( t ) { + var minstd = factory(); + t.equal( typeof minstd.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 minstd; + var o; + + minstd = factory(); + t.equal( typeof minstd.toJSON, 'function', 'has method' ); + + o = minstd.toJSON(); + t.equal( o.type, 'PRNG', 'has property' ); + t.equal( o.name, minstd.NAME, 'has property' ); + t.deepEqual( o.state, typedarray2json( minstd.state ), 'has property' ); + t.deepEqual( o.params, [], 'has property' ); + + t.end(); +}); + +tape( 'if the `state` property is set to a value other than an Int32Array, an error is thrown', function test( t ) { + var minstd; + var values; + var i; + + minstd = factory(); + + values = [ + '3', + 3, + -3, + 3.14, + 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 set to '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + minstd.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an unsupported version, an error is thrown', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + minstd.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an unsupported number of sections, an error is thrown', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 1; // version + v[ 1 ] = 3; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 1; // version + v[ 1 ] = 3; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + minstd.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an unsupported state length, an error is thrown', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 7 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 2; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // state + v[ 5 ] = 1; // seed length + v[ 6 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 8 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 2; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // state + v[ 5 ] = 1; // seed length + v[ 6 ] = 123; // seed + v[ 7 ] = 456; // seed + 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() { + minstd.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an incompatible seed length, an error is thrown', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 9; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 1; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 9; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + minstd.state = value; + }; + } +}); + +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 minstd; + var v; + var i; + + minstd = factory(); + for ( i = 0; i < 1e3; i++ ) { + v = minstd.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 ) { + var minstd = factory(); + t.equal( minstd.normalized.NAME, 'minstd', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the minimum possible generated number', function test( t ) { + var minstd = factory(); + t.equal( minstd.normalized.MIN, 0.0, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the maximum possible generated number', function test( t ) { + var minstd = factory(); + t.equal( minstd.normalized.MAX, (INT32_MAX-2.0)/(INT32_MAX-1.0), 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator seed (integer seed)', function test( t ) { + var minstd; + var actual; + + minstd = factory({ + 'seed': 12345 + }); + actual = minstd.normalized.seed; + + t.equal( isInt32Array( 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 seed (array seed)', function test( t ) { + var minstd; + var actual; + var seed; + var i; + + seed = [ 12345 ]; + minstd = factory({ + 'seed': seed + }); + actual = minstd.normalized.seed; + + t.equal( isInt32Array( actual ), true, 'has property' ); + for ( i = 0; i < seed.length; i++ ) { + t.equal( actual[ i ], seed[ i ], 'returns expected value for word '+i ); + } + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator seed length', function test( t ) { + var minstd = factory(); + t.equal( typeof minstd.normalized.seedLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state', function test( t ) { + var minstd = factory(); + t.equal( isInt32Array( minstd.normalized.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state length', function test( t ) { + var minstd = factory(); + t.equal( typeof minstd.normalized.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state size', function test( t ) { + var minstd = factory(); + t.equal( typeof minstd.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 minstd; + var o; + + minstd = factory(); + t.equal( typeof minstd.normalized.toJSON, 'function', 'has method' ); + + o = minstd.normalized.toJSON(); + t.equal( o.type, 'PRNG', 'has property' ); + t.equal( o.name, minstd.normalized.NAME, 'has property' ); + t.deepEqual( o.state, typedarray2json( minstd.normalized.state ), 'has property' ); + + t.end(); +}); + +tape( 'if the `state` property is set to a value other than an Int32Array, an error is thrown (normalized)', function test( t ) { + var minstd; + var values; + var i; + + minstd = factory(); + + values = [ + '3', + -3, + 3, + 3.14, + 0, + 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() { + minstd.normalized.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array having an unexpected length, an error is thrown (normalized)', function test( t ) { + var minstd; + var values; + var i; + + minstd = factory(); + + values = [ + new Int32Array( 0 ), + new Int32Array( 1 ) + ]; + 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() { + minstd.normalized.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an unsupported version, an error is thrown (normalized)', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + minstd.normalized.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an unsupported number of sections, an error is thrown (normalized)', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 0; // version + v[ 1 ] = 3; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 0; // version + v[ 1 ] = 3; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + minstd.normalized.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an unsupported state length, an error is thrown (normalized)', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 7 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 2; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // state + v[ 5 ] = 1; // seed length + v[ 6 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 8 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 2; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 1; // state + v[ 5 ] = 1; // seed length + v[ 6 ] = 123; // seed + v[ 7 ] = 456; // seed + 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() { + minstd.normalized.state = value; + }; + } +}); + +tape( 'if the `state` property is set to an Int32Array containing an incompatible seed length, an error is thrown (normalized)', function test( t ) { + var minstd; + var values; + var v; + var i; + + minstd = factory(); + + values = []; + + v = new Int32Array( 6 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 9; // seed length + v[ 5 ] = 123; // seed + values.push( v ); + + v = new Int32Array( 7 ); + v[ 0 ] = 0; // version + v[ 1 ] = 2; // number of sections + v[ 2 ] = 1; // state length + v[ 3 ] = 1; // state + v[ 4 ] = 9; // seed length + v[ 5 ] = 123; // seed + v[ 6 ] = 456; // seed + 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() { + minstd.normalized.state = value; + }; + } +}); + +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 = new Array( 1e3 ); // eslint-disable-line stdlib/no-new-array + N = 500; + + count = -1; + npass = 0; + + gof(); + + function gof() { + var rejected; + var pValue; + var minstd; + var bool; + var i; + var j; + + count += 1; + rejected = 0; + for ( i = 0; i < N; i++ ) { + minstd = factory(); + t.ok( true, 'seed: '+minstd.seed ); + for ( j = 0; j < x.length; j++ ) { + x[ j ] = minstd.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 supports specifying the generator state', function test( t ) { + var minstd; + var state; + var arr; + var i; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + + // Create another PRNG using the captured state: + minstd = factory({ + 'state': state + }); + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( minstd(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the function supports specifying a shared generator state', function test( t ) { + var minstd; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + var j; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Int32Array( 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 returned function supports setting the generator state', function test( t ) { + var minstd; + var state; + var arr; + var i; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + // Set the state: + minstd.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( minstd(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the returned function supports setting the generator state to a state array having a different length', function test( t ) { + var minstd; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + + // Seed length: 2 + minstd = factory({ + 'seed': [ 1234, 5678 ] + }); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Int32Array( 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 the generator state (normalized)', function test( t ) { + var minstd; + var state; + var arr; + var i; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd.normalized(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd.normalized() ); + } + // Set the state: + minstd.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( minstd.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); + +tape( 'the returned function supports setting the generator state (normalized)', function test( t ) { + var minstd; + var state; + var arr; + var i; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd.normalized(); + } + // Capture the current state: + state = minstd.normalized.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd.normalized() ); + } + // Set the state: + minstd.normalized.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( minstd.normalized(), 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 minstd; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + var j; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Int32Array( 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 Int32Array( 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 minstd; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var s; + var i; + var j; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Int32Array( 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 Int32Array( 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 Int32Array( 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 minstd; + var shared; + var state; + var rand1; + var rand2; + var arr; + var v1; + var v2; + var i; + var j; + + minstd = factory(); + + // Move to a future state... + for ( i = 0; i < 100; i++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: + shared = new Int32Array( state ); + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + + // 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 Int32Array( 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/pcg32/test/test.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js new file mode 100644 index 000000000000..8313ba2acbe2 --- /dev/null +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js @@ -0,0 +1,240 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2018 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 INT32_MAX = require( '@stdlib/constants/int32/max' ); +var isPositiveInteger = require( '@stdlib/math/base/assert/is-positive-integer' ); +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var minstd = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minstd, '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 minstd.normalized, 'function', 'has method' ); + t.end(); +}); + +tape( 'attached to the main export is a method to generate linear congruential pseudorandom number generator', function test( t ) { + t.equal( typeof minstd.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 minstd.toJSON, 'function', 'has method' ); + t.end(); +}); + +tape( 'attached to the main export is the generator name', function test( t ) { + t.equal( minstd.NAME, 'minstd', 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the minimum possible generated number', function test( t ) { + t.equal( minstd.MIN, 1, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the maximum possible generated number', function test( t ) { + t.equal( minstd.MAX, INT32_MAX-1, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator seed', function test( t ) { + t.equal( isInt32Array( minstd.seed ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator seed length', function test( t ) { + t.equal( typeof minstd.seedLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator state', function test( t ) { + t.equal( isInt32Array( minstd.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator state length', function test( t ) { + t.equal( typeof minstd.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the main export is the generator state size', function test( t ) { + t.equal( typeof minstd.byteLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'the function returns pseudorandom integers strictly between 0 and 2^31-1 (inclusive)', function test( t ) { + var v; + var i; + for ( i = 0; i < 1e3; i++ ) { + v = minstd(); + t.equal( typeof v, 'number', 'returns a number' ); + t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); + t.equal( v >= 1 && v <= INT32_MAX-1, true, 'returns an integer between 1 and 2^31-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 = minstd.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( minstd.normalized.NAME, 'minstd', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the minimum possible generated number', function test( t ) { + t.equal( minstd.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( minstd.normalized.MAX, (INT32_MAX-2.0)/(INT32_MAX-1.0), 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator seed', function test( t ) { + t.equal( isInt32Array( minstd.normalized.seed ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator seed length', function test( t ) { + t.equal( typeof minstd.normalized.seedLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state', function test( t ) { + t.equal( isInt32Array( minstd.normalized.state ), true, 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state length', function test( t ) { + t.equal( typeof minstd.normalized.stateLength, 'number', 'has property' ); + t.end(); +}); + +tape( 'attached to the `normalized` method is the generator state size', function test( t ) { + t.equal( typeof minstd.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 minstd.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++ ) { + minstd(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd() ); + } + // Set the state: + minstd.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( minstd(), 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++ ) { + minstd.normalized(); + } + // Capture the current state: + state = minstd.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd.normalized() ); + } + // Set the state: + minstd.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( minstd.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++ ) { + minstd.normalized(); + } + // Capture the current state: + state = minstd.normalized.state; + + // Move to a future state... + arr = []; + for ( i = 0; i < 100; i++ ) { + arr.push( minstd.normalized() ); + } + // Set the state: + minstd.normalized.state = state; + + // Replay previously generated values... + for ( i = 0; i < 100; i++ ) { + t.equal( minstd.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + } + t.end(); +}); From 0f29c235287e606277b25b1467b92e0412b34266 Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Fri, 4 Apr 2025 04:19:30 +0600 Subject: [PATCH 2/9] feat: implement an initial version of pcg32 prng --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/random/base/pcg32/lib/factory.js | 185 ++++++---- .../@stdlib/random/base/pcg32/lib/main.js | 4 +- .../lib/{rand_int32.js => rand_uint32.js} | 14 +- .../random/base/pcg32/test/test.factory.js | 317 +++++++----------- .../@stdlib/random/base/pcg32/test/test.js | 22 +- 5 files changed, 274 insertions(+), 268 deletions(-) rename lib/node_modules/@stdlib/random/base/pcg32/lib/{rand_int32.js => rand_uint32.js} (77%) diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js index 7d58d7a8a340..b146decaf8e6 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js @@ -30,20 +30,30 @@ 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 isInt32Array = require( '@stdlib/assert/is-int32array' ); +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); var format = require( '@stdlib/string/format' ); -var INT32_MAX = require( '@stdlib/constants/int32/max' ); -var Int32Array = require( '@stdlib/array/int32' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); +var Uint32Array = require( '@stdlib/array/uint32' ); var gcopy = require( '@stdlib/blas/base/gcopy' ); var typedarray2json = require( '@stdlib/array/to-json' ); -var randint32 = require( './rand_int32.js' ); +var umul = require( '@stdlib/number/uint32/base/mul' ); +var umuldw = require( '@stdlib/number/uint32/base/muldw' ); +var randuint32 = require( './rand_uint32.js' ); // VARIABLES // -var NORMALIZATION_CONSTANT = (INT32_MAX - 1)|0; // asm type annotation -var MAX_SEED = (INT32_MAX - 1)|0; // asm type annotation -var A = 16807|0; // asm type annotation +var NORMALIZATION_CONSTANT = UINT32_MAX + 1; +var MAX_SEED = UINT32_MAX >>> 0; // asm type annotation + +// LCG multiplier +var MULTIPLIER = [ 0x4C957F2D >>> 0, 0x5851F42D >>> 0 ]; // asm type annotation + +// LCG increment, controls the stream. #TODO: Make user definable like seed +var INCREMENT = [ 0x0000DEAD >>> 0, 0xBEEF0000 >>> 0 ]; // asm type annotation + +// Define the size of state as a multiple of 32-bits +var N = 2; // Define the state array schema version: var STATE_ARRAY_VERSION = 1; // NOTE: anytime the state array schema changes, this value should be incremented!!! @@ -55,10 +65,10 @@ var NUM_STATE_SECTIONS = 2; // state, seed var STATE_SECTION_OFFSET = 2; // | version | num_sections | state_length | ...state | seed_length | ...seed | // Define the index offset of the seed section in the state array: -var SEED_SECTION_OFFSET = 4; // | version | num_sections | state_length | ...state | seed_length | ...seed | +var SEED_SECTION_OFFSET = 3 + N; // | version | num_sections | state_length | ...state | seed_length | ...seed | // Define the length of the "fixed" length portion of the state array: -var STATE_FIXED_LENGTH = 5; // 1 (version) + 1 (num_sections) + 1 (state_length) + 1 (state) + 1 (seed_length) +var STATE_FIXED_LENGTH = 4 + N; // 1 (version) + 1 (num_sections) + 1 (state_length) + 2 (state) + 1 (seed_length) // FUNCTIONS // @@ -67,7 +77,7 @@ var STATE_FIXED_LENGTH = 5; // 1 (version) + 1 (num_sections) + 1 (state_length) * Verifies state array integrity. * * @private -* @param {Int32Array} state - state array +* @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` */ @@ -90,11 +100,11 @@ function verifyState( state, FLG ) { 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 ] ) ); } - // The length of the "state" section must equal `1`... - if ( state[ STATE_SECTION_OFFSET ] !== 1 ) { - return new RangeError( format( 'invalid %s. State array has an incompatible state length. Expected: `%u`. Actual: `%u`.', s1, 1, state[ STATE_SECTION_OFFSET ] ) ); + // The length of the "state" section must equal `N`... + if ( state[ STATE_SECTION_OFFSET ] !== N ) { + return new RangeError( format( 'invalid %s. State array has an incompatible state length. Expected: `%u`. Actual: `%u`.', s1, 2, state[ STATE_SECTION_OFFSET ] ) ); } - // The length of the "seed" section much match the empirical length... + // The length of the "seed" section must match the empirical length... 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 ] ) ); } @@ -112,9 +122,9 @@ function verifyState( state, FLG ) { * @param {PRNGStateMINSTD} [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 signed 32-bit integer or an array-like object containing integers less than the maximum signed 32-bit integer -* @throws {RangeError} a numeric seed must be a positive integer less than the maximum signed 32-bit integer -* @throws {TypeError} state must be an `Int32Array` +* @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 an `Uint32Array` * @throws {Error} must provide a valid state * @throws {TypeError} `copy` option must be a boolean * @returns {PRNG} LCG PRNG @@ -132,7 +142,7 @@ function verifyState( state, FLG ) { * }); * * var v = minstd(); -* // returns 20739838 +* // returns 2557507945 */ function factory( options ) { var STATE; @@ -156,8 +166,8 @@ function factory( options ) { if ( hasOwnProp( options, 'state' ) ) { state = options.state; opts.state = true; - if ( !isInt32Array( state ) ) { - throw new TypeError( format( 'invalid option. `%s` option must be an Int32Array. Option: `%s`.', 'state', state ) ); + if ( !isUint32Array( state ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an Uint32Array. Option: `%s`.', 'state', state ) ); } err = verifyState( state, true ); if ( err ) { @@ -166,14 +176,14 @@ function factory( options ) { if ( opts.copy === false ) { STATE = state; } else { - STATE = new Int32Array( state.length ); + STATE = new Uint32Array( state.length ); gcopy( state.length, state, 1, STATE, 1 ); } // Create a state "view": - state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); // Create a seed "view": - seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), state[ SEED_SECTION_OFFSET ] ); + 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 ) { @@ -182,59 +192,67 @@ function factory( options ) { opts.seed = true; if ( isPositiveInteger( seed ) ) { if ( seed > MAX_SEED ) { - throw new RangeError( format( 'invalid option. `%s` option must be a positive integer less than the maximum signed 32-bit integer. Option: `%u`.', 'seed', seed ) ); + 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 ) ); } - seed |= 0; // asm type annotation + seed >>>= 0; // asm type annotation } else if ( isCollection( seed ) && seed.length > 0 ) { slen = seed.length; - STATE = new Int32Array( STATE_FIXED_LENGTH+slen ); + STATE = new Uint32Array( STATE_FIXED_LENGTH+slen ); // Initialize sections: STATE[ 0 ] = STATE_ARRAY_VERSION; STATE[ 1 ] = NUM_STATE_SECTIONS; - STATE[ STATE_SECTION_OFFSET ] = 1; + STATE[ STATE_SECTION_OFFSET ] = N; STATE[ SEED_SECTION_OFFSET ] = slen; // Copy the provided seed array to prevent external mutation, as mutation would lead to an inability to reproduce PRNG values according to the PRNG's stated seed: gcopy.ndarray( slen, seed, 1, 0, STATE, 1, SEED_SECTION_OFFSET+1 ); // Create a state "view": - state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); // Create a seed "view": - seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), slen ); + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), slen ); // Initialize the internal PRNG state: - state[ 0 ] = seed[ 0 ]; + minstd(); + state.set( addUint64( state, [ + seed[ 0 ], + ( slen > 1 ) ? seed[ 1 ] : 0 + ]) ); + minstd(); } else { - throw new TypeError( format( 'invalid option. `%s` option must be either a positive integer less than the maximum signed 32-bit integer or an array-like object containing integer values less than the maximum signed 32-bit integer. Option: `%s`.', 'seed', seed ) ); + 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 = randint32()|0; // asm type annotation + seed = randuint32() >>> 0; // asm type annotation } } } else { - seed = randint32()|0; // asm type annotation + seed = randuint32() >>> 0; // asm type annotation } if ( state === void 0 ) { - STATE = new Int32Array( STATE_FIXED_LENGTH+1 ); + STATE = new Uint32Array( STATE_FIXED_LENGTH+1 ); // Initialize sections: STATE[ 0 ] = STATE_ARRAY_VERSION; STATE[ 1 ] = NUM_STATE_SECTIONS; - STATE[ STATE_SECTION_OFFSET ] = 1; + STATE[ STATE_SECTION_OFFSET ] = N; STATE[ SEED_SECTION_OFFSET ] = 1; STATE[ SEED_SECTION_OFFSET+1 ] = seed; // Create a state "view": - state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); // Create a seed "view": - seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); // Initialize the internal PRNG state: - state[ 0 ] = seed[ 0 ]; + minstd(); + state.set( addUint64( state, [ seed[ 0 ], 0 ] ) ); + minstd(); } + setReadOnly( minstd, 'NAME', 'minstd' ); setReadOnlyAccessor( minstd, 'seed', getSeed ); setReadOnlyAccessor( minstd, 'seedLength', getSeedLength ); @@ -242,8 +260,8 @@ function factory( options ) { setReadOnlyAccessor( minstd, 'stateLength', getStateLength ); setReadOnlyAccessor( minstd, 'byteLength', getStateSize ); setReadOnly( minstd, 'toJSON', toJSON ); - setReadOnly( minstd, 'MIN', 1 ); - setReadOnly( minstd, 'MAX', INT32_MAX-1 ); + setReadOnly( minstd, 'MIN', 0 ); + setReadOnly( minstd, 'MAX', UINT32_MAX ); setReadOnly( minstd, 'normalized', normalized ); setReadOnly( normalized, 'NAME', minstd.NAME ); @@ -253,8 +271,8 @@ function factory( options ) { setReadOnlyAccessor( normalized, 'stateLength', getStateLength ); setReadOnlyAccessor( normalized, 'byteLength', getStateSize ); setReadOnly( normalized, 'toJSON', toJSON ); - setReadOnly( normalized, 'MIN', (minstd.MIN-1.0) / NORMALIZATION_CONSTANT ); - setReadOnly( normalized, 'MAX', (minstd.MAX-1.0) / NORMALIZATION_CONSTANT ); + setReadOnly( normalized, 'MIN', minstd.MIN / NORMALIZATION_CONSTANT ); + setReadOnly( normalized, 'MAX', minstd.MAX / NORMALIZATION_CONSTANT ); return minstd; @@ -266,7 +284,7 @@ function factory( options ) { */ function getSeed() { var len = STATE[ SEED_SECTION_OFFSET ]; - return gcopy( len, seed, 1, new Int32Array( len ), 1 ); + return gcopy( len, seed, 1, new Uint32Array( len ), 1 ); } /** @@ -321,7 +339,7 @@ function factory( options ) { */ function getState() { var len = STATE.length; - return gcopy( len, STATE, 1, new Int32Array( len ), 1 ); + return gcopy( len, STATE, 1, new Uint32Array( len ), 1 ); } /** @@ -334,13 +352,13 @@ function factory( options ) { * * @private * @param {PRNGStateMINSTD} s - generator state - * @throws {TypeError} must provide an `Int32Array` + * @throws {TypeError} must provide an `Uint32Array` * @throws {Error} must provide a valid state */ function setState( s ) { var err; - if ( !isInt32Array( s ) ) { - throw new TypeError( format( 'invalid argument. Must provide an Int32Array. Value: `%s`.', s ) ); + if ( !isUint32Array( s ) ) { + throw new TypeError( format( 'invalid argument. Must provide an Uint32Array. Value: `%s`.', s ) ); } err = verifyState( s, false ); if ( err ) { @@ -356,15 +374,15 @@ function factory( options ) { } else { // Check if we can reuse allocated memory... if ( s.length !== STATE.length ) { - STATE = new Int32Array( s.length ); // reallocate + STATE = new Uint32Array( s.length ); // reallocate } gcopy( s.length, s, 1, STATE, 1 ); } // Create a new state "view": - state = new Int32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); + state = new Uint32Array( STATE.buffer, STATE.byteOffset+((STATE_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), N ); // Create a new seed "view": - seed = new Int32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), STATE[ SEED_SECTION_OFFSET ] ); + seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), STATE[ SEED_SECTION_OFFSET ] ); } /** @@ -387,16 +405,69 @@ function factory( options ) { } /** - * Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\). + * Generates a pseudorandom integer on the interval \\( [1,2^{32}) \\). * * @private - * @returns {integer32} pseudorandom integer + * @returns {uinteger32} pseudorandom integer */ function minstd() { - var s = state[ 0 ]|0; // asm type annotation - s = ( (A*s)%INT32_MAX )|0; // asm type annotation - state[ 0 ] = s; - return s|0; // asm type annotation + var xorshifted = [ 0, 0 ]; + var oldstate = [ state[ 0 ], state[ 1 ] ]; + var rot; + + state.set( addUint64( multUint64( oldstate, MULTIPLIER ), INCREMENT ) ); + + xorshifted[0] = ( ( oldstate[0] >>> 18 ) | ( oldstate[1] << 14 ) ) >>> 0; + xorshifted[1] = ( oldstate[1] >>> 18 ) >>> 0; + + xorshifted[0] ^= oldstate[0]; + xorshifted[1] ^= oldstate[1]; + + xorshifted[0] = ( ( xorshifted[0] >>> 27 ) | ( xorshifted[1] << 5 ) ) >>> 0; + xorshifted[1] = ( xorshifted[1] >>> 27 ) >>> 0; + + rot = oldstate[1] >>> 27; + return ( (xorshifted[0] >>> rot) | ( xorshifted[0] << ( -rot & 31 ) ) ) >>> 0; + } + + /** + * Adds two 64-bit unsigned integers. + * + * @private + * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] + * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] + * @returns {ArrayLike} 64-bit unsigned integer sum as [ low, high ] + */ + function addUint64( a, b ) { + var la = a[0] >>> 0; + var ha = a[1] >>> 0; + var lb = b[0] >>> 0; + var hb = b[1] >>> 0; + + var ls = ( la + lb ) >>> 0; + var hs = ( ha + hb + ( ls < la ) ) >>> 0; + return [ ls >>> 0, hs >>> 0 ]; + } + + /** + * Multiplies two 64-bit unsigned integers. + * + * @private + * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] + * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] + * @returns {ArrayLike} 64-bit unsigned integer product as [ low, high ] + */ + function multUint64( a, b ) { + var la = a[0] >>> 0; + var ha = a[1] >>> 0; + var lb = b[0] >>> 0; + var hb = b[1] >>> 0; + var m1 = umul( la, hb ) >>> 0; + var m2 = umul( ha, lb ) >>> 0; + var p = umuldw( la, lb ); + p = [ p[ 1 ], p[ 0 ] ]; // swap to make little-endian + p[ 1 ] = ( p[ 1 ] + m1 + m2 ) >>> 0; + return p; } /** @@ -406,7 +477,7 @@ function factory( options ) { * @returns {number} pseudorandom number */ function normalized() { - return (minstd()-1) / NORMALIZATION_CONSTANT; + return minstd() / NORMALIZATION_CONSTANT; } } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js index f31a9faaa960..e5c5b29bed11 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js @@ -21,7 +21,7 @@ // MODULES // var factory = require( './factory.js' ); -var randint32 = require( './rand_int32.js' ); +var randuint32 = require( './rand_uint32.js' ); // MAIN // @@ -93,7 +93,7 @@ var randint32 = require( './rand_int32.js' ); * // returns */ var minstd = factory({ - 'seed': randint32() + 'seed': randuint32() }); diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_int32.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js similarity index 77% rename from lib/node_modules/@stdlib/random/base/pcg32/lib/rand_int32.js rename to lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js index 925d4d6e299d..dce9581b3159 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_int32.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js @@ -20,33 +20,33 @@ // MODULES // -var INT32_MAX = require( '@stdlib/constants/int32/max' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); var floor = require( '@stdlib/math/base/special/floor' ); // VARIABLES // -var MAX = INT32_MAX - 1; +var MAX = UINT32_MAX - 1; // MAIN // /** -* Returns a pseudorandom integer on the interval \\([1, 2^{31}-1)\\). +* Returns a pseudorandom integer on the interval \\( [1, 2^{32}-1) \\). * * @private * @returns {PositiveInteger} pseudorandom integer * * @example -* var v = randint32(); +* var v = randuint32(); * // returns */ -function randint32() { +function randuint32() { var v = floor( 1.0 + (MAX*Math.random()) ); // eslint-disable-line stdlib/no-builtin-math - return v|0; // asm type annotation + return v >>> 0; // asm type annotation } // EXPORTS // -module.exports = randint32; +module.exports = randuint32; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js index 69898110a1ef..fbfe8edd8af9 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js @@ -24,14 +24,15 @@ var tape = require( 'tape' ); var ENV = require( '@stdlib/process/env' ); -var INT32_MAX = require( '@stdlib/constants/int32/max' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); var now = require( '@stdlib/time/now' ); var isPositiveInteger = require( '@stdlib/math/base/assert/is-positive-integer' ); -var isInt32Array = require( '@stdlib/assert/is-int32array' ); -var Int32Array = require( '@stdlib/array/int32' ); +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 typedarray2json = require( '@stdlib/array/to-json' ); +var zeros = require( '@stdlib/array/base/zeros' ); var factory = require( './../lib/factory.js' ); @@ -140,14 +141,14 @@ tape( 'if provided a `seed` which is not a positive integer or a non-empty array } }); -tape( 'the function throws a range error if provided a `seed` greater than or equal to the maximum signed 32-bit integer', function test( t ) { +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 = [ - INT32_MAX, - INT32_MAX + 1, - INT32_MAX + 2 + UINT32_MAX + 1, + UINT32_MAX + 2, + UINT32_MAX + 3 ]; for ( i = 0; i < values.length; i++ ) { @@ -164,7 +165,7 @@ tape( 'the function throws a range error if provided a `seed` greater than or eq } }); -tape( 'if provided a `state` option which is not an Int32Array, the factory function throws an error', function test( t ) { +tape( 'if provided a `state` option which is not an Uint32Array, the factory function throws an error', function test( t ) { var values; var i; @@ -200,8 +201,8 @@ tape( 'if provided a `state` option having an insufficient length, the factory f var i; values = [ - new Int32Array( 0 ), - new Int32Array( 1 ) + new Uint32Array( 0 ), + new Uint32Array( 1 ) ]; for ( i = 0; i < values.length; i++ ) { @@ -225,23 +226,18 @@ tape( 'if provided a `state` option containing an unsupported version, the facto values = []; - v = new Int32Array( 6 ); + v = new Uint32Array( 7 ); v[ 0 ] = 0; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 1; // seed length values.push( v ); - v = new Int32Array( 7 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 8 ); + v[ 0 ] = 2; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -265,23 +261,18 @@ tape( 'if provided a `state` option containing an unsupported number of sections values = []; - v = new Int32Array( 6 ); + v = new Uint32Array( 7 ); v[ 0 ] = 1; // version - v[ 1 ] = 3; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed + v[ 1 ] = 1; // number of sections + v[ 2 ] = 2; // state length + v[ 5 ] = 1; // seed length values.push( v ); - v = new Int32Array( 7 ); + v = new Uint32Array( 8 ); v[ 0 ] = 1; // version v[ 1 ] = 3; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -305,25 +296,18 @@ tape( 'if provided a `state` option containing an unsupported state length, the values = []; - v = new Int32Array( 7 ); + v = new Uint32Array( 7 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 2; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // state + v[ 2 ] = 1; // state length v[ 5 ] = 1; // seed length - v[ 6 ] = 123; // seed values.push( v ); - v = new Int32Array( 8 ); + v = new Uint32Array( 8 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 2; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // state - v[ 5 ] = 1; // seed length - v[ 6 ] = 123; // seed - v[ 7 ] = 456; // seed + v[ 2 ] = 3; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -347,23 +331,18 @@ tape( 'if provided a `state` option containing an incompatible seed length, the values = []; - v = new Int32Array( 6 ); + v = new Uint32Array( 7 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 9; // seed length - v[ 5 ] = 123; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 0; // seed length values.push( v ); - v = new Int32Array( 7 ); + v = new Uint32Array( 8 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 9; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 3; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -390,7 +369,7 @@ tape( 'the function returns a pseudorandom number generator (no options)', funct v = minstd(); t.equal( typeof v, 'number', 'returns a number' ); t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); - t.equal( v >= 1 && v <= INT32_MAX-1, true, 'returns an integer between 1 and 2^31-1 (inclusive)' ); + t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32 (inclusive)' ); } t.end(); }); @@ -405,7 +384,7 @@ tape( 'the function returns a pseudorandom number generator (options; no seed)', v = minstd(); t.equal( typeof v, 'number', 'returns a number' ); t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); - t.equal( v >= 1 && v <= INT32_MAX-1, true, 'returns an integer between 1 and 2^31-1 (inclusive)' ); + t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32 (inclusive)' ); } t.end(); }); @@ -472,13 +451,13 @@ tape( 'attached to the returned function is the generator name', function test( tape( 'attached to the returned function is the minimum possible generated number', function test( t ) { var minstd = factory(); - t.equal( minstd.MIN, 1, 'has property' ); + t.equal( minstd.MIN, 0, 'has property' ); t.end(); }); tape( 'attached to the returned function is the maximum possible generated number', function test( t ) { var minstd = factory(); - t.equal( minstd.MAX, INT32_MAX-1, 'has property' ); + t.equal( minstd.MAX, UINT32_MAX, 'has property' ); t.end(); }); @@ -491,7 +470,7 @@ tape( 'attached to the returned function is the generator seed (integer seed)', }); actual = minstd.seed; - t.equal( isInt32Array( actual ), true, 'has property' ); + 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(); @@ -510,7 +489,7 @@ tape( 'attached to the returned function is the generator seed (array seed)', fu }); actual = minstd.seed; - t.equal( isInt32Array( actual ), true, 'has property' ); + t.equal( isUint32Array( actual ), true, 'has property' ); for ( i = 0; i < seed.length; i++ ) { t.equal( actual[ i ], seed[ i ], 'returns expected value for word '+i ); } @@ -525,7 +504,7 @@ tape( 'attached to the returned function is the generator seed length', function tape( 'attached to the returned function is the generator state', function test( t ) { var minstd = factory(); - t.equal( isInt32Array( minstd.state ), true, 'has property' ); + t.equal( isUint32Array( minstd.state ), true, 'has property' ); t.end(); }); @@ -557,7 +536,7 @@ tape( 'attached to the returned function is a method to serialize the generator t.end(); }); -tape( 'if the `state` property is set to a value other than an Int32Array, an error is thrown', function test( t ) { +tape( 'if the `state` property is set to a value other than an Uint32Array, an error is thrown', function test( t ) { var minstd; var values; var i; @@ -591,7 +570,7 @@ tape( 'if the `state` property is set to a value other than an Int32Array, an er } }); -tape( 'if the `state` property is set to an Int32Array containing an unsupported version, an error is thrown', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an unsupported version, an error is thrown', function test( t ) { var minstd; var values; var v; @@ -601,23 +580,18 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported values = []; - v = new Int32Array( 6 ); + v = new Uint32Array( 7 ); v[ 0 ] = 0; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 1; // seed length values.push( v ); - v = new Int32Array( 7 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 8 ); + v[ 0 ] = 2; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -632,7 +606,7 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported } }); -tape( 'if the `state` property is set to an Int32Array containing an unsupported number of sections, an error is thrown', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an unsupported number of sections, an error is thrown', function test( t ) { var minstd; var values; var v; @@ -642,23 +616,18 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported values = []; - v = new Int32Array( 6 ); + v = new Uint32Array( 7 ); v[ 0 ] = 1; // version - v[ 1 ] = 3; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed + v[ 1 ] = 1; // number of sections + v[ 2 ] = 2; // state length + v[ 5 ] = 1; // seed length values.push( v ); - v = new Int32Array( 7 ); + v = new Uint32Array( 8 ); v[ 0 ] = 1; // version v[ 1 ] = 3; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -673,7 +642,7 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported } }); -tape( 'if the `state` property is set to an Int32Array containing an unsupported state length, an error is thrown', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an unsupported state length, an error is thrown', function test( t ) { var minstd; var values; var v; @@ -683,25 +652,18 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported values = []; - v = new Int32Array( 7 ); + v = new Uint32Array( 7 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 2; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // state + v[ 2 ] = 1; // state length v[ 5 ] = 1; // seed length - v[ 6 ] = 123; // seed values.push( v ); - v = new Int32Array( 8 ); + v = new Uint32Array( 8 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 2; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // state - v[ 5 ] = 1; // seed length - v[ 6 ] = 123; // seed - v[ 7 ] = 456; // seed + v[ 2 ] = 3; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -716,7 +678,7 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported } }); -tape( 'if the `state` property is set to an Int32Array containing an incompatible seed length, an error is thrown', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an incompatible seed length, an error is thrown', function test( t ) { var minstd; var values; var v; @@ -726,23 +688,18 @@ tape( 'if the `state` property is set to an Int32Array containing an incompatibl values = []; - v = new Int32Array( 6 ); + v = new Uint32Array( 7 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 9; // seed length - v[ 5 ] = 123; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 0; // seed length values.push( v ); - v = new Int32Array( 7 ); + v = new Uint32Array( 8 ); v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 9; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 3; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -785,7 +742,7 @@ tape( 'attached to the `normalized` method is the minimum possible generated num tape( 'attached to the `normalized` method is the maximum possible generated number', function test( t ) { var minstd = factory(); - t.equal( minstd.normalized.MAX, (INT32_MAX-2.0)/(INT32_MAX-1.0), 'has property' ); + t.equal( minstd.normalized.MAX, UINT32_MAX / ( UINT32_MAX + 1.0 ), 'has property' ); t.end(); }); @@ -798,7 +755,7 @@ tape( 'attached to the `normalized` method is the generator seed (integer seed)' }); actual = minstd.normalized.seed; - t.equal( isInt32Array( actual ), true, 'has property' ); + 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(); @@ -816,7 +773,7 @@ tape( 'attached to the `normalized` method is the generator seed (array seed)', }); actual = minstd.normalized.seed; - t.equal( isInt32Array( actual ), true, 'has property' ); + t.equal( isUint32Array( actual ), true, 'has property' ); for ( i = 0; i < seed.length; i++ ) { t.equal( actual[ i ], seed[ i ], 'returns expected value for word '+i ); } @@ -831,7 +788,7 @@ tape( 'attached to the `normalized` method is the generator seed length', functi tape( 'attached to the `normalized` method is the generator state', function test( t ) { var minstd = factory(); - t.equal( isInt32Array( minstd.normalized.state ), true, 'has property' ); + t.equal( isUint32Array( minstd.normalized.state ), true, 'has property' ); t.end(); }); @@ -862,7 +819,7 @@ tape( 'attached to the `normalized` method is a method to serialize the generato t.end(); }); -tape( 'if the `state` property is set to a value other than an Int32Array, an error is thrown (normalized)', function test( t ) { +tape( 'if the `state` property is set to a value other than an Uint32Array, an error is thrown (normalized)', function test( t ) { var minstd; var values; var i; @@ -896,7 +853,7 @@ tape( 'if the `state` property is set to a value other than an Int32Array, an er } }); -tape( 'if the `state` property is set to an Int32Array having an unexpected length, an error is thrown (normalized)', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array having an unexpected length, an error is thrown (normalized)', function test( t ) { var minstd; var values; var i; @@ -904,8 +861,8 @@ tape( 'if the `state` property is set to an Int32Array having an unexpected leng minstd = factory(); values = [ - new Int32Array( 0 ), - new Int32Array( 1 ) + new Uint32Array( 0 ), + new Uint32Array( 1 ) ]; for ( i = 0; i < values.length; i++ ) { t.throws( badValue( values[i] ), RangeError, 'throws an error when set to '+values[i] ); @@ -919,7 +876,7 @@ tape( 'if the `state` property is set to an Int32Array having an unexpected leng } }); -tape( 'if the `state` property is set to an Int32Array containing an unsupported version, an error is thrown (normalized)', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an unsupported version, an error is thrown (normalized)', function test( t ) { var minstd; var values; var v; @@ -929,23 +886,18 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported values = []; - v = new Int32Array( 6 ); + v = new Uint32Array( 7 ); v[ 0 ] = 0; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 1; // seed length values.push( v ); - v = new Int32Array( 7 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 8 ); + v[ 0 ] = 2; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -960,7 +912,7 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported } }); -tape( 'if the `state` property is set to an Int32Array containing an unsupported number of sections, an error is thrown (normalized)', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an unsupported number of sections, an error is thrown (normalized)', function test( t ) { var minstd; var values; var v; @@ -970,23 +922,18 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported values = []; - v = new Int32Array( 6 ); - v[ 0 ] = 0; // version - v[ 1 ] = 3; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed + v = new Uint32Array( 7 ); + v[ 0 ] = 1; // version + v[ 1 ] = 1; // number of sections + v[ 2 ] = 2; // state length + v[ 5 ] = 1; // seed length values.push( v ); - v = new Int32Array( 7 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 8 ); + v[ 0 ] = 1; // version v[ 1 ] = 3; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -1001,7 +948,7 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported } }); -tape( 'if the `state` property is set to an Int32Array containing an unsupported state length, an error is thrown (normalized)', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an unsupported state length, an error is thrown (normalized)', function test( t ) { var minstd; var values; var v; @@ -1011,25 +958,18 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported values = []; - v = new Int32Array( 7 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 7 ); + v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 2; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // state + v[ 2 ] = 1; // state length v[ 5 ] = 1; // seed length - v[ 6 ] = 123; // seed values.push( v ); - v = new Int32Array( 8 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 8 ); + v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 2; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 1; // state - v[ 5 ] = 1; // seed length - v[ 6 ] = 123; // seed - v[ 7 ] = 456; // seed + v[ 2 ] = 3; // state length + v[ 5 ] = 2; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -1044,7 +984,7 @@ tape( 'if the `state` property is set to an Int32Array containing an unsupported } }); -tape( 'if the `state` property is set to an Int32Array containing an incompatible seed length, an error is thrown (normalized)', function test( t ) { +tape( 'if the `state` property is set to an Uint32Array containing an incompatible seed length, an error is thrown (normalized)', function test( t ) { var minstd; var values; var v; @@ -1054,23 +994,18 @@ tape( 'if the `state` property is set to an Int32Array containing an incompatibl values = []; - v = new Int32Array( 6 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 7 ); + v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 9; // seed length - v[ 5 ] = 123; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 0; // seed length values.push( v ); - v = new Int32Array( 7 ); - v[ 0 ] = 0; // version + v = new Uint32Array( 8 ); + v[ 0 ] = 1; // version v[ 1 ] = 2; // number of sections - v[ 2 ] = 1; // state length - v[ 3 ] = 1; // state - v[ 4 ] = 9; // seed length - v[ 5 ] = 123; // seed - v[ 6 ] = 456; // seed + v[ 2 ] = 2; // state length + v[ 5 ] = 3; // seed length values.push( v ); for ( i = 0; i < values.length; i++ ) { @@ -1094,7 +1029,7 @@ tape( 'the `normalized` method returns pseudorandom numbers drawn from a uniform threshold = 0.10; - x = new Array( 1e3 ); // eslint-disable-line stdlib/no-new-array + x = zeros( 1e3 ); N = 500; count = -1; @@ -1215,7 +1150,7 @@ tape( 'the function supports specifying a shared generator state', function test } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: - shared = new Int32Array( state ); + shared = new Uint32Array( state ); // Create PRNGs using the captured state: rand1 = factory({ @@ -1299,7 +1234,7 @@ tape( 'the returned function supports setting the generator state to a state arr } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: - shared = new Int32Array( state ); + shared = new Uint32Array( state ); // Create PRNGs having seed lengths equal to 1: rand1 = factory({ @@ -1417,7 +1352,7 @@ tape( 'the returned function supports setting a shared generator state (same len } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: - shared = new Int32Array( state ); + shared = new Uint32Array( state ); // Create PRNGs using the captured state: rand1 = factory({ @@ -1445,7 +1380,7 @@ tape( 'the returned function supports setting a shared generator state (same len } // Reset the (shared) state: - rand1.state = new Int32Array( state ); + rand1.state = new Uint32Array( state ); // Replay previously generated values... j = 0; @@ -1488,7 +1423,7 @@ tape( 'the returned function supports setting a shared generator state (differen } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: - shared = new Int32Array( state ); + shared = new Uint32Array( state ); // Create PRNGs using the captured state: rand1 = factory({ @@ -1516,7 +1451,7 @@ tape( 'the returned function supports setting a shared generator state (differen } // Reset the (*previously* shared) state: - s = new Int32Array( state.length+1 ); + s = new Uint32Array( state.length+1 ); gcopy( state.length, state, 1, s, 1 ); s[ s.length-3 ] = 2; s[ s.length-1 ] = 1234; @@ -1540,7 +1475,7 @@ tape( 'the returned function supports setting a shared generator state (differen rand2.state = s; // Reset to a shared state: - shared = new Int32Array( state ); + shared = new Uint32Array( state ); rand1.state = shared; rand2.state = shared; @@ -1578,7 +1513,7 @@ tape( 'the returned function supports setting a shared generator state (no initi state = minstd.state; // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: - shared = new Int32Array( state ); + shared = new Uint32Array( state ); // Move to a future state... arr = []; @@ -1615,7 +1550,7 @@ tape( 'the returned function supports setting a shared generator state (no initi } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: - shared = new Int32Array( state ); + shared = new Uint32Array( state ); // Reset the (shared) state: rand1.state = shared; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js index 8313ba2acbe2..90b809a9d169 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js @@ -21,9 +21,9 @@ // MODULES // var tape = require( 'tape' ); -var INT32_MAX = require( '@stdlib/constants/int32/max' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); var isPositiveInteger = require( '@stdlib/math/base/assert/is-positive-integer' ); -var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var isUint32Array = require( '@stdlib/assert/is-uint32array' ); var minstd = require( './../lib' ); @@ -56,17 +56,17 @@ tape( 'attached to the main export is the generator name', function test( t ) { }); tape( 'attached to the main export is the minimum possible generated number', function test( t ) { - t.equal( minstd.MIN, 1, 'has property' ); + t.equal( minstd.MIN, 0, 'has property' ); t.end(); }); tape( 'attached to the main export is the maximum possible generated number', function test( t ) { - t.equal( minstd.MAX, INT32_MAX-1, 'has property' ); + t.equal( minstd.MAX, UINT32_MAX, 'has property' ); t.end(); }); tape( 'attached to the main export is the generator seed', function test( t ) { - t.equal( isInt32Array( minstd.seed ), true, 'has property' ); + t.equal( isUint32Array( minstd.seed ), true, 'has property' ); t.end(); }); @@ -76,7 +76,7 @@ tape( 'attached to the main export is the generator seed length', function test( }); tape( 'attached to the main export is the generator state', function test( t ) { - t.equal( isInt32Array( minstd.state ), true, 'has property' ); + t.equal( isUint32Array( minstd.state ), true, 'has property' ); t.end(); }); @@ -90,14 +90,14 @@ tape( 'attached to the main export is the generator state size', function test( t.end(); }); -tape( 'the function returns pseudorandom integers strictly between 0 and 2^31-1 (inclusive)', function test( t ) { +tape( 'the function returns pseudorandom integers strictly between 0 and 2^32-1 (inclusive)', function test( t ) { var v; var i; for ( i = 0; i < 1e3; i++ ) { v = minstd(); t.equal( typeof v, 'number', 'returns a number' ); t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); - t.equal( v >= 1 && v <= INT32_MAX-1, true, 'returns an integer between 1 and 2^31-1 (inclusive)' ); + t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32-1 (inclusive)' ); } t.end(); }); @@ -124,12 +124,12 @@ tape( 'attached to the `normalized` method is the minimum possible generated num }); tape( 'attached to the `normalized` method is the maximum possible generated number', function test( t ) { - t.equal( minstd.normalized.MAX, (INT32_MAX-2.0)/(INT32_MAX-1.0), 'has property' ); + t.equal( minstd.normalized.MAX, UINT32_MAX / ( UINT32_MAX + 1.0 ), 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator seed', function test( t ) { - t.equal( isInt32Array( minstd.normalized.seed ), true, 'has property' ); + t.equal( isUint32Array( minstd.normalized.seed ), true, 'has property' ); t.end(); }); @@ -139,7 +139,7 @@ tape( 'attached to the `normalized` method is the generator seed length', functi }); tape( 'attached to the `normalized` method is the generator state', function test( t ) { - t.equal( isInt32Array( minstd.normalized.state ), true, 'has property' ); + t.equal( isUint32Array( minstd.normalized.state ), true, 'has property' ); t.end(); }); From 6cd65e91991042282d24a2a00cc0d87dbbd34854 Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Fri, 4 Apr 2025 06:17:36 +0600 Subject: [PATCH 3/9] refactor: replace "minstd" with "pcg32" --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/random/base/pcg32/README.md | 124 +++---- .../random/base/pcg32/benchmark/benchmark.js | 6 +- .../random/base/pcg32/benchmark/c/benchmark.c | 40 +-- .../@stdlib/random/base/pcg32/docs/repl.txt | 10 +- .../random/base/pcg32/docs/types/index.d.ts | 26 +- .../random/base/pcg32/docs/types/test.ts | 66 ++-- .../random/base/pcg32/examples/c/example.c | 8 +- .../random/base/pcg32/examples/index.js | 12 +- .../stdlib/random/base/{minstd.h => pcg32.h} | 18 +- .../@stdlib/random/base/pcg32/lib/factory.js | 74 ++-- .../@stdlib/random/base/pcg32/lib/index.js | 8 +- .../@stdlib/random/base/pcg32/lib/main.js | 8 +- .../@stdlib/random/base/pcg32/package.json | 4 +- .../@stdlib/random/base/pcg32/src/main.c | 86 ++--- .../random/base/pcg32/test/test.factory.js | 330 +++++++++--------- .../@stdlib/random/base/pcg32/test/test.js | 78 ++--- lib/node_modules/@stdlib/types/index.d.ts | 19 + lib/node_modules/@stdlib/types/test.ts | 16 + 18 files changed, 484 insertions(+), 449 deletions(-) rename lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/{minstd.h => pcg32.h} (69%) diff --git a/lib/node_modules/@stdlib/random/base/pcg32/README.md b/lib/node_modules/@stdlib/random/base/pcg32/README.md index f4ceec90e71c..47797d1c8d25 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/README.md +++ b/lib/node_modules/@stdlib/random/base/pcg32/README.md @@ -18,7 +18,7 @@ limitations under the License. --> -# MINSTD +# PCG32 > A linear congruential pseudorandom number generator ([LCG][lcg]) based on Park and Miller. @@ -27,33 +27,33 @@ limitations under the License. ## Usage ```javascript -var minstd = require( '@stdlib/random/base/minstd' ); +var pcg32 = require( '@stdlib/random/base/pcg32' ); ``` -#### minstd() +#### pcg32() Returns a pseudorandom integer on the interval `[1, 2147483646]`. ```javascript -var r = minstd(); +var r = pcg32(); // returns ``` -#### minstd.normalized() +#### pcg32.normalized() Returns a pseudorandom number on the interval `[0,1)`. ```javascript -var r = minstd.normalized(); +var r = pcg32.normalized(); // returns ``` -#### minstd.factory( \[options] ) +#### pcg32.factory( \[options] ) Returns a linear congruential pseudorandom number generator ([LCG][lcg]). ```javascript -var rand = minstd.factory(); +var rand = pcg32.factory(); ``` The function accepts the following `options`: @@ -65,12 +65,12 @@ The function accepts the following `options`: By default, a random integer is used to seed the returned generator. To seed the generator, provide either an `integer` on the interval `[1, 2147483646]` ```javascript -var rand = minstd.factory({ +var rand = pcg32.factory({ 'seed': 1234 }); var r = rand(); -// returns 20739838 +// returns 2557507945 ``` or, for arbitrary length seeds, an array-like `object` containing signed 32-bit integers @@ -78,12 +78,12 @@ or, for arbitrary length seeds, an array-like `object` containing signed 32-bit ```javascript var Int32Array = require( '@stdlib/array/int32' ); -var rand = minstd.factory({ +var rand = pcg32.factory({ 'seed': new Int32Array( [ 1234 ] ) }); var r = rand(); -// returns 20739838 +// returns 2557507945 ``` To return a generator having a specific initial state, set the generator `state` option. @@ -93,136 +93,136 @@ To return a generator having a specific initial state, set the generator `state` var r; var i; for ( i = 0; i < 1000; i++ ) { - r = minstd(); + r = pcg32(); } -// Create a new PRNG initialized to the current state of `minstd`: -var rand = minstd.factory({ - 'state': minstd.state +// Create a new PRNG initialized to the current state of `pcg32`: +var rand = pcg32.factory({ + 'state': pcg32.state }); // Test that the generated pseudorandom numbers are the same: -var bool = ( rand() === minstd() ); +var bool = ( rand() === pcg32() ); // returns true ``` -#### minstd.NAME +#### pcg32.NAME The generator name. ```javascript -var str = minstd.NAME; -// returns 'minstd' +var str = pcg32.NAME; +// returns 'pcg32' ``` -#### minstd.MIN +#### pcg32.MIN Minimum possible value. ```javascript -var min = minstd.MIN; -// returns 1 +var min = pcg32.MIN; +// returns 0 ``` -#### minstd.MAX +#### pcg32.MAX Maximum possible value. ```javascript -var max = minstd.MAX; -// returns 2147483646 +var max = pcg32.MAX; +// returns 4294967295 ``` -#### minstd.seed +#### pcg32.seed -The value used to seed `minstd()`. +The value used to seed `pcg32()`. ```javascript // Generate pseudorandom values... var r; var i; for ( i = 0; i < 100; i++ ) { - r = minstd(); + r = pcg32(); } // Generate the same pseudorandom values... -var rand = minstd.factory({ - 'seed': minstd.seed +var rand = pcg32.factory({ + 'seed': pcg32.seed }); for ( i = 0; i < 100; i++ ) { r = rand(); } ``` -#### minstd.seedLength +#### pcg32.seedLength Length of generator seed. ```javascript -var len = minstd.seedLength; +var len = pcg32.seedLength; // returns ``` -#### minstd.state +#### pcg32.state Writable property for getting and setting the generator state. ```javascript -var r = minstd(); +var r = pcg32(); // returns -r = minstd(); +r = pcg32(); // returns // ... // Get the current state: -var state = minstd.state; -// returns +var state = pcg32.state; +// returns -r = minstd(); +r = pcg32(); // returns -r = minstd(); +r = pcg32(); // returns // Reset the state: -minstd.state = state; +pcg32.state = state; // Replay the last two pseudorandom numbers: -r = minstd(); +r = pcg32(); // returns -r = minstd(); +r = pcg32(); // returns // ... ``` -#### minstd.stateLength +#### pcg32.stateLength Length of generator state. ```javascript -var len = minstd.stateLength; +var len = pcg32.stateLength; // returns ``` -#### minstd.byteLength +#### pcg32.byteLength Size (in bytes) of generator state. ```javascript -var sz = minstd.byteLength; +var sz = pcg32.byteLength; // returns ``` -#### minstd.toJSON() +#### pcg32.toJSON() Serializes the pseudorandom number generator as a JSON object. ```javascript -var o = minstd.toJSON(); +var o = pcg32.toJSON(); // returns { 'type': 'PRNG', 'name': '...', 'state': {...}, 'params': [] } ``` @@ -250,17 +250,17 @@ var o = minstd.toJSON(); ```javascript -var minstd = require( '@stdlib/random/base/minstd' ); +var pcg32 = require( '@stdlib/random/base/pcg32' ); // Generate pseudorandom numbers... var i; for ( i = 0; i < 100; i++ ) { - console.log( minstd() ); + console.log( pcg32() ); } // Create a new pseudorandom number generator... var seed = 1234; -var rand = minstd.factory({ +var rand = pcg32.factory({ 'seed': seed }); for ( i = 0; i < 100; i++ ) { @@ -268,8 +268,8 @@ for ( i = 0; i < 100; i++ ) { } // Create another pseudorandom number generator using a previous seed... -rand = minstd.factory({ - 'seed': minstd.seed +rand = pcg32.factory({ + 'seed': pcg32.seed }); for ( i = 0; i < 100; i++ ) { console.log( rand() ); @@ -301,10 +301,10 @@ for ( i = 0; i < 100; i++ ) { ## See Also -- [`@stdlib/random/array/minstd`][@stdlib/random/array/minstd]: create an array containing pseudorandom numbers generated using a linear congruential pseudorandom number generator (LCG). -- [`@stdlib/random/iter/minstd`][@stdlib/random/iter/minstd]: create an iterator for a linear congruential pseudorandom number generator (LCG) based on Park and Miller. -- [`@stdlib/random/streams/minstd`][@stdlib/random/streams/minstd]: create a readable stream for a linear congruential pseudorandom number generator (LCG) based on Park and Miller. -- [`@stdlib/random/base/minstd-shuffle`][@stdlib/random/base/minstd-shuffle]: A linear congruential pseudorandom number generator (LCG) whose output is shuffled. +- [`@stdlib/random/array/pcg32`][@stdlib/random/array/pcg32]: create an array containing pseudorandom numbers generated using a linear congruential pseudorandom number generator (LCG). +- [`@stdlib/random/iter/pcg32`][@stdlib/random/iter/pcg32]: create an iterator for a linear congruential pseudorandom number generator (LCG) based on Park and Miller. +- [`@stdlib/random/streams/pcg32`][@stdlib/random/streams/pcg32]: create a readable stream for a linear congruential pseudorandom number generator (LCG) based on Park and Miller. +- [`@stdlib/random/base/pcg32-shuffle`][@stdlib/random/base/pcg32-shuffle]: A linear congruential pseudorandom number generator (LCG) whose output is shuffled. - [`@stdlib/random/base/mt19937`][@stdlib/random/base/mt19937]: A 32-bit Mersenne Twister pseudorandom number generator. - [`@stdlib/random/base/randi`][@stdlib/random/base/randi]: pseudorandom numbers having integer values. @@ -326,13 +326,13 @@ for ( i = 0; i < 100; i++ ) { -[@stdlib/random/array/minstd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/array/minstd +[@stdlib/random/array/pcg32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/array/pcg32 -[@stdlib/random/iter/minstd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/iter/minstd +[@stdlib/random/iter/pcg32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/iter/pcg32 -[@stdlib/random/streams/minstd]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/streams/minstd +[@stdlib/random/streams/pcg32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/streams/pcg32 -[@stdlib/random/base/minstd-shuffle]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/minstd-shuffle +[@stdlib/random/base/pcg32-shuffle]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/pcg32-shuffle [@stdlib/random/base/mt19937]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/mt19937 diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js index 829f67d30c8c..6d0008e61766 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js @@ -23,7 +23,7 @@ var bench = require( '@stdlib/bench' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); var pkg = require( './../package.json' ).name; -var minstd = require( './../lib' ); +var pcg32 = require( './../lib' ); // MAIN // @@ -34,7 +34,7 @@ bench( pkg, function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - z = minstd(); + z = pcg32(); if ( isnan( z ) ) { b.fail( 'should not return NaN' ); } @@ -53,7 +53,7 @@ bench( pkg+':normalized', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - z = minstd.normalized(); + z = pcg32.normalized(); if ( isnan( z ) ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c index 95854406f4e0..a6ffb56cfc31 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c @@ -16,7 +16,7 @@ * limitations under the License. */ -#include "stdlib/random/base/minstd.h" +#include "stdlib/random/base/pcg32.h" #include "stdlib/random/base/shared.h" #include #include @@ -25,7 +25,7 @@ #include #include -#define NAME "base/minstd" +#define NAME "base/pcg32" #define ITERATIONS 1000000 #define REPEATS 3 @@ -100,13 +100,13 @@ static double benchmark1( void ) { t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - obj = stdlib_base_random_minstd_allocate( i+1 ); + obj = stdlib_base_random_pcg32_allocate( i+1 ); if ( obj == NULL || obj->prng->min != 1 ) { printf( "unexpected result\n" ); break; } if ( i < ITERATIONS-1 ) { - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); } } elapsed = tic() - t; @@ -114,7 +114,7 @@ static double benchmark1( void ) { if ( obj == NULL || obj->prng->min != 1 ) { printf( "unexpected result\n" ); } - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); return elapsed; } @@ -132,7 +132,7 @@ static double benchmark2( void ) { double t; int i; - struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); if ( obj == NULL ) { printf( "unable to allocate memory\n" ); exit( 1 ); @@ -152,7 +152,7 @@ static double benchmark2( void ) { if ( status != 0 || v > max ) { printf( "unexpected result\n" ); } - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); return elapsed; } @@ -169,7 +169,7 @@ static double benchmark3( void ) { double t; int i; - struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); if ( obj == NULL ) { printf( "unable to allocate memory\n" ); exit( 1 ); @@ -188,7 +188,7 @@ static double benchmark3( void ) { if ( status != 0 || v != v ) { printf( "unexpected result\n" ); } - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); return elapsed; } @@ -205,7 +205,7 @@ static double benchmark4( void ) { double t; int i; - struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); if ( obj == NULL ) { printf( "unable to allocate memory\n" ); exit( 1 ); @@ -214,7 +214,7 @@ static double benchmark4( void ) { t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { // NOTE: this is likely to be optimized away by a modern compiler, making this benchmark meaningless. - status = stdlib_base_random_minstd_seed( obj, &v ); + status = stdlib_base_random_pcg32_seed( obj, &v ); if ( status != 0 || v != 12345 ) { printf( "unexpected result\n" ); break; @@ -225,7 +225,7 @@ static double benchmark4( void ) { if ( status != 0 || v != 12345 ) { printf( "unexpected result\n" ); } - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); return elapsed; } @@ -241,7 +241,7 @@ static double benchmark5( void ) { double t; int i; - struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); if ( obj == NULL ) { printf( "unable to allocate memory\n" ); exit( 1 ); @@ -249,7 +249,7 @@ static double benchmark5( void ) { t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - state = stdlib_base_random_minstd_state( obj ); + state = stdlib_base_random_pcg32_state( obj ); if ( state == NULL ) { printf( "unexpected result\n" ); break; @@ -263,7 +263,7 @@ static double benchmark5( void ) { if ( state == NULL ) { printf( "unexpected result\n" ); } - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); free( state ); return elapsed; @@ -275,26 +275,26 @@ static double benchmark5( void ) { * @return elapsed time in seconds */ static double benchmark6( void ) { - stdlib_base_random_minstd_state_t *states[2]; + stdlib_base_random_pcg32_state_t *states[2]; double elapsed; int8_t status; double t; int i; - struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); if ( obj == NULL ) { printf( "unable to allocate memory\n" ); exit( 1 ); } for ( i = 0; i < 2; i++ ) { - states[i] = (stdlib_base_random_minstd_state_t *)malloc( sizeof( stdlib_base_random_minstd_state_t ) ); + states[i] = (stdlib_base_random_pcg32_state_t *)malloc( sizeof( stdlib_base_random_pcg32_state_t ) ); states[i]->seed = 12345; states[i]->state = 12345; } t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - status = stdlib_base_random_minstd_set( obj, (void *)states[i%2] ); + status = stdlib_base_random_pcg32_set( obj, (void *)states[i%2] ); if ( status != 0 ) { printf( "unexpected result\n" ); break; @@ -305,7 +305,7 @@ static double benchmark6( void ) { if ( status != 0 ) { printf( "unexpected result\n" ); } - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); for ( i = 0; i < 2; i++ ) { free( states[i] ); } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt b/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt index 2b114f22b965..8b3aef05470c 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt @@ -77,7 +77,7 @@ // Provide a seed: > rand = {{alias}}.factory( { 'seed': 1234 } ); > r = rand() - 20739838 + 2557507945 {{alias}}.NAME @@ -86,7 +86,7 @@ Examples -------- > var str = {{alias}}.NAME - 'minstd' + 'pcg32' {{alias}}.MIN @@ -95,7 +95,7 @@ Examples -------- > var v = {{alias}}.MIN - 1 + 0 {{alias}}.MAX @@ -104,7 +104,7 @@ Examples -------- > var v = {{alias}}.MAX - 2147483646 + 4294967295 {{alias}}.seed @@ -137,7 +137,7 @@ // Get the current state: > var state = {{alias}}.state - + > r = {{alias}}() diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts index 132fb796ce69..adc85cd22f65 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts @@ -29,12 +29,12 @@ interface Options { /** * Pseudorandom number generator seed. */ - seed?: random.PRNGSeedMINSTD; + seed?: random.PRNGSeedPCG32; /** * Pseudorandom number generator state. */ - state?: random.PRNGStateMINSTD; + state?: random.PRNGStatePCG32; /** * Specifies whether to copy a provided pseudorandom number generator state. @@ -64,7 +64,7 @@ interface PRNG { /** * PRNG seed. */ - readonly seed: random.PRNGSeedMINSTD; + readonly seed: random.PRNGSeedPCG32; /** * PRNG seed length. @@ -74,7 +74,7 @@ interface PRNG { /** * PRNG state. */ - state: random.PRNGStateMINSTD; + state: random.PRNGStatePCG32; /** * PRNG state length. @@ -122,7 +122,7 @@ interface Random extends PRNG { * @returns pseudorandom number * * @example - * var v = minstd(); + * var v = pcg32(); * // returns */ (): number; @@ -133,7 +133,7 @@ interface Random extends PRNG { * @returns pseudorandom number * * @example - * var r = minstd.normalized(); + * var r = pcg32.normalized(); * // returns */ normalized(): number; @@ -149,12 +149,12 @@ interface Random extends PRNG { * @returns pseudorandom number generator * * @example - * var rand = minstd.factory(); + * var rand = pcg32.factory(); * var v = rand(); * // returns * * @example - * var rand = minstd.factory({ + * var rand = pcg32.factory({ * 'seed': 12345 * }); * var v = rand(); @@ -175,23 +175,23 @@ interface Random extends PRNG { * @returns pseudorandom number * * @example -* var v = minstd(); +* var v = pcg32(); * // returns * * @example -* var v = minstd.normalized(); +* var v = pcg32.normalized(); * // returns * * @example -* var rand = minstd.factory({ +* var rand = pcg32.factory({ * 'seed': 12345 * }); * var v = rand(); * // returns */ -declare var minstd: Random; +declare var pcg32: Random; // EXPORTS // -export = minstd; +export = pcg32; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts index 860129c751ba..382958cac526 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts @@ -16,49 +16,49 @@ * limitations under the License. */ -import minstd = require( './index' ); +import pcg32 = require( './index' ); // TESTS // // The function returns a number... { - minstd(); // $ExpectType number + pcg32(); // $ExpectType number } // The compiler throws an error if the function is provided any arguments... { - minstd( true ); // $ExpectError - minstd( 2, 3 ); // $ExpectError + pcg32( true ); // $ExpectError + pcg32( 2, 3 ); // $ExpectError } // Attached to main export is a `normalized` method which returns a number... { - minstd.normalized(); // $ExpectType number + pcg32.normalized(); // $ExpectType number } // The compiler throws an error if the `normalized` method is provided any number of arguments... { - minstd.normalized( true ); // $ExpectError - minstd.normalized( 123 ); // $ExpectError - minstd.normalized( 'abc' ); // $ExpectError + pcg32.normalized( true ); // $ExpectError + pcg32.normalized( 123 ); // $ExpectError + pcg32.normalized( 'abc' ); // $ExpectError } // Attached to main export is a `factory` method which returns a function... { - minstd.factory(); // $ExpectType NullaryFunction - minstd.factory( { 'copy': false } ); // $ExpectType NullaryFunction + pcg32.factory(); // $ExpectType NullaryFunction + pcg32.factory( { 'copy': false } ); // $ExpectType NullaryFunction } // The `factory` method returns a function which returns a number... { - const fcn = minstd.factory(); + const fcn = pcg32.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 = minstd.factory(); + const fcn = pcg32.factory(); fcn( 1 ); // $ExpectError fcn( 2, 1 ); // $ExpectError fcn( 2, 1, 1 ); // $ExpectError @@ -66,41 +66,41 @@ import minstd = require( './index' ); // The compiler throws an error if the `factory` method is provided an options argument which is not an object... { - minstd.factory( null ); // $ExpectError + pcg32.factory( null ); // $ExpectError } // The compiler throws an error if the `factory` method is provided a `seed` option which is not a valid seed... { - minstd.factory( { 'seed': true } ); // $ExpectError - minstd.factory( { 'seed': 'abc' } ); // $ExpectError - minstd.factory( { 'seed': null } ); // $ExpectError - minstd.factory( { 'seed': [ 'a' ] } ); // $ExpectError - minstd.factory( { 'seed': {} } ); // $ExpectError - minstd.factory( { 'seed': ( x: number ): number => x } ); // $ExpectError + pcg32.factory( { 'seed': true } ); // $ExpectError + pcg32.factory( { 'seed': 'abc' } ); // $ExpectError + pcg32.factory( { 'seed': null } ); // $ExpectError + pcg32.factory( { 'seed': [ 'a' ] } ); // $ExpectError + pcg32.factory( { 'seed': {} } ); // $ExpectError + pcg32.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... { - minstd.factory( { 'state': 123 } ); // $ExpectError - minstd.factory( { 'state': 'abc' } ); // $ExpectError - minstd.factory( { 'state': null } ); // $ExpectError - minstd.factory( { 'state': [] } ); // $ExpectError - minstd.factory( { 'state': {} } ); // $ExpectError - minstd.factory( { 'state': true } ); // $ExpectError - minstd.factory( { 'state': ( x: number ): number => x } ); // $ExpectError + pcg32.factory( { 'state': 123 } ); // $ExpectError + pcg32.factory( { 'state': 'abc' } ); // $ExpectError + pcg32.factory( { 'state': null } ); // $ExpectError + pcg32.factory( { 'state': [] } ); // $ExpectError + pcg32.factory( { 'state': {} } ); // $ExpectError + pcg32.factory( { 'state': true } ); // $ExpectError + pcg32.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... { - minstd.factory( { 'copy': 123 } ); // $ExpectError - minstd.factory( { 'copy': 'abc' } ); // $ExpectError - minstd.factory( { 'copy': null } ); // $ExpectError - minstd.factory( { 'copy': [] } ); // $ExpectError - minstd.factory( { 'copy': {} } ); // $ExpectError - minstd.factory( { 'copy': ( x: number ): number => x } ); // $ExpectError + pcg32.factory( { 'copy': 123 } ); // $ExpectError + pcg32.factory( { 'copy': 'abc' } ); // $ExpectError + pcg32.factory( { 'copy': null } ); // $ExpectError + pcg32.factory( { 'copy': [] } ); // $ExpectError + pcg32.factory( { 'copy': {} } ); // $ExpectError + pcg32.factory( { 'copy': ( x: number ): number => x } ); // $ExpectError } // The compiler throws an error if the `factory` method is provided more than one argument... { - minstd.factory( {}, 2 ); // $ExpectError + pcg32.factory( {}, 2 ); // $ExpectError } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c index 9a6aa701aced..072a4dcc8fa6 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c @@ -16,7 +16,7 @@ * limitations under the License. */ -#include "stdlib/random/base/minstd.h" +#include "stdlib/random/base/pcg32.h" #include "stdlib/random/base/shared.h" #include #include @@ -30,13 +30,13 @@ int main( void ) { double d; // Create a PRNG... - struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); + struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); if ( obj == NULL ) { fprintf( stderr, "Error allocating memory.\n" ); exit( 1 ); } - status = stdlib_base_random_minstd_seed( obj, &seed ); + status = stdlib_base_random_pcg32_seed( obj, &seed ); if ( status != 0 ) { printf( "Unable to retrieve the PRNG seed.\n" ); exit( 1 ); @@ -71,5 +71,5 @@ int main( void ) { printf( "%0.16f\n", d ); } - stdlib_base_random_minstd_free( obj ); + stdlib_base_random_pcg32_free( obj ); } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js b/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js index 45785eafd857..26435e61e150 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js @@ -18,18 +18,18 @@ 'use strict'; -var minstd = require( './../lib' ); +var pcg32 = require( './../lib' ); // Generate pseudorandom numbers... -console.log( '\nseed: %d', minstd.seed[ 0 ] ); +console.log( '\nseed: %d', pcg32.seed[ 0 ] ); var i; for ( i = 0; i < 100; i++ ) { - console.log( minstd() ); + console.log( pcg32() ); } // Create a new pseudorandom number generator... var seed = 1234; -var rand = minstd.factory({ +var rand = pcg32.factory({ 'seed': seed }); console.log( '\nseed: %d', seed ); @@ -38,8 +38,8 @@ for ( i = 0; i < 100; i++ ) { } // Create another pseudorandom number generator using a previous seed... -rand = minstd.factory({ - 'seed': minstd.seed +rand = pcg32.factory({ + 'seed': pcg32.seed }); console.log( '\nseed: %d', rand.seed[ 0 ] ); for ( i = 0; i < 100; i++ ) { diff --git a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/minstd.h b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h similarity index 69% rename from lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/minstd.h rename to lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h index d17ac50471d8..9276204ea2bf 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/minstd.h +++ b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h @@ -16,8 +16,8 @@ * limitations under the License. */ -#ifndef STDLIB_RANDOM_BASE_MINSTD_H -#define STDLIB_RANDOM_BASE_MINSTD_H +#ifndef STDLIB_RANDOM_BASE_PCG32_H +#define STDLIB_RANDOM_BASE_PCG32_H #include "stdlib/random/base/shared.h" #include @@ -35,35 +35,35 @@ extern "C" { typedef struct { uint32_t seed; uint32_t state; -} stdlib_base_random_minstd_state_t; +} stdlib_base_random_pcg32_state_t; /** * Returns a pointer to a dynamically allocated PRNG. */ -struct BasePRNGObject * stdlib_base_random_minstd_allocate( const int32_t seed ); +struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const int32_t seed ); /** * Frees a PRNG's allocated memory. */ -void stdlib_base_random_minstd_free( struct BasePRNGObject *obj ); +void stdlib_base_random_pcg32_free( struct BasePRNGObject *obj ); /** * Returns a PRNG seed. */ -int8_t stdlib_base_random_minstd_seed( const struct BasePRNGObject *obj, int32_t *out ); +int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, int32_t *out ); /** * Returns a copy of the current PRNG state. */ -void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ); +void * stdlib_base_random_pcg32_state( const struct BasePRNGObject *obj ); /** * Sets the PRNG state. */ -int8_t stdlib_base_random_minstd_set( struct BasePRNGObject *obj, const void *state ); +int8_t stdlib_base_random_pcg32_set( struct BasePRNGObject *obj, const void *state ); #ifdef __cplusplus } #endif -#endif // !STDLIB_RANDOM_BASE_MINSTD_H +#endif // !STDLIB_RANDOM_BASE_PCG32_H diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js index b146decaf8e6..fcbfc97441b2 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js @@ -118,8 +118,8 @@ function verifyState( state, FLG ) { * Returns a linear congruential pseudorandom number generator (LCG) based on Park and Miller. * * @param {Options} [options] - options -* @param {PRNGSeedMINSTD} [options.seed] - pseudorandom number generator seed -* @param {PRNGStateMINSTD} [options.state] - pseudorandom number generator state +* @param {PRNGSeedPCG32} [options.seed] - pseudorandom number generator seed +* @param {PRNGStatePCG32} [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 @@ -130,18 +130,18 @@ function verifyState( state, FLG ) { * @returns {PRNG} LCG PRNG * * @example -* var minstd = factory(); +* var pcg32 = factory(); * -* var v = minstd(); +* var v = pcg32(); * // returns * * @example * // Return a seeded LCG: -* var minstd = factory({ +* var pcg32 = factory({ * 'seed': 1234 * }); * -* var v = minstd(); +* var v = pcg32(); * // returns 2557507945 */ function factory( options ) { @@ -215,12 +215,12 @@ function factory( options ) { seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), slen ); // Initialize the internal PRNG state: - minstd(); + pcg32(); state.set( addUint64( state, [ seed[ 0 ], ( slen > 1 ) ? seed[ 1 ] : 0 ]) ); - minstd(); + pcg32(); } 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 ) ); } @@ -248,39 +248,39 @@ function factory( options ) { seed = new Uint32Array( STATE.buffer, STATE.byteOffset+((SEED_SECTION_OFFSET+1)*STATE.BYTES_PER_ELEMENT), 1 ); // Initialize the internal PRNG state: - minstd(); + pcg32(); state.set( addUint64( state, [ seed[ 0 ], 0 ] ) ); - minstd(); + pcg32(); } - setReadOnly( minstd, 'NAME', 'minstd' ); - setReadOnlyAccessor( minstd, 'seed', getSeed ); - setReadOnlyAccessor( minstd, 'seedLength', getSeedLength ); - setReadWriteAccessor( minstd, 'state', getState, setState ); - setReadOnlyAccessor( minstd, 'stateLength', getStateLength ); - setReadOnlyAccessor( minstd, 'byteLength', getStateSize ); - setReadOnly( minstd, 'toJSON', toJSON ); - setReadOnly( minstd, 'MIN', 0 ); - setReadOnly( minstd, 'MAX', UINT32_MAX ); - setReadOnly( minstd, 'normalized', normalized ); - - setReadOnly( normalized, 'NAME', minstd.NAME ); + setReadOnly( pcg32, 'NAME', 'pcg32' ); + setReadOnlyAccessor( pcg32, 'seed', getSeed ); + setReadOnlyAccessor( pcg32, 'seedLength', getSeedLength ); + setReadWriteAccessor( pcg32, 'state', getState, setState ); + setReadOnlyAccessor( pcg32, 'stateLength', getStateLength ); + setReadOnlyAccessor( pcg32, 'byteLength', getStateSize ); + setReadOnly( pcg32, 'toJSON', toJSON ); + setReadOnly( pcg32, 'MIN', 0 ); + setReadOnly( pcg32, 'MAX', UINT32_MAX ); + setReadOnly( pcg32, 'normalized', normalized ); + + setReadOnly( normalized, 'NAME', pcg32.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', minstd.MIN / NORMALIZATION_CONSTANT ); - setReadOnly( normalized, 'MAX', minstd.MAX / NORMALIZATION_CONSTANT ); + setReadOnly( normalized, 'MIN', pcg32.MIN / NORMALIZATION_CONSTANT ); + setReadOnly( normalized, 'MAX', pcg32.MAX / NORMALIZATION_CONSTANT ); - return minstd; + return pcg32; /** * Returns the PRNG seed. * * @private - * @returns {PRNGSeedMINSTD} seed + * @returns {PRNGSeedPCG32} seed */ function getSeed() { var len = STATE[ SEED_SECTION_OFFSET ]; @@ -335,7 +335,7 @@ function factory( options ) { * - The first element of each section following the preamble specifies the section length. The remaining section elements comprise the section contents. * * @private - * @returns {PRNGStateMINSTD} current state + * @returns {PRNGStatePCG32} current state */ function getState() { var len = STATE.length; @@ -351,7 +351,7 @@ function factory( options ) { * - 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 {PRNGStateMINSTD} s - generator state + * @param {PRNGStatePCG32} s - generator state * @throws {TypeError} must provide an `Uint32Array` * @throws {Error} must provide a valid state */ @@ -398,7 +398,7 @@ function factory( options ) { function toJSON() { var out = {}; out.type = 'PRNG'; - out.name = minstd.NAME; + out.name = pcg32.NAME; out.state = typedarray2json( STATE ); out.params = []; return out; @@ -410,7 +410,7 @@ function factory( options ) { * @private * @returns {uinteger32} pseudorandom integer */ - function minstd() { + function pcg32() { var xorshifted = [ 0, 0 ]; var oldstate = [ state[ 0 ], state[ 1 ] ]; var rot; @@ -434,9 +434,9 @@ function factory( options ) { * Adds two 64-bit unsigned integers. * * @private - * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] - * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] - * @returns {ArrayLike} 64-bit unsigned integer sum as [ low, high ] + * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] + * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] + * @returns {ArrayLike} 64-bit unsigned integer sum as [ low, high ] */ function addUint64( a, b ) { var la = a[0] >>> 0; @@ -453,9 +453,9 @@ function factory( options ) { * Multiplies two 64-bit unsigned integers. * * @private - * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] - * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] - * @returns {ArrayLike} 64-bit unsigned integer product as [ low, high ] + * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] + * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] + * @returns {ArrayLike} 64-bit unsigned integer product as [ low, high ] */ function multUint64( a, b ) { var la = a[0] >>> 0; @@ -477,7 +477,7 @@ function factory( options ) { * @returns {number} pseudorandom number */ function normalized() { - return minstd() / NORMALIZATION_CONSTANT; + return pcg32() / NORMALIZATION_CONSTANT; } } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js index 4e6abeea302d..bd32b00ba98a 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js @@ -24,19 +24,19 @@ * @module @stdlib/random/base/pcg32 * * @example -* var minstd = require( '@stdlib/random/base/pcg32' ); +* var pcg32 = require( '@stdlib/random/base/pcg32' ); * -* var v = minstd(); +* var v = pcg32(); * // returns * * @example * var factory = require( '@stdlib/random/base/pcg32' ).factory; * -* var minstd = factory({ +* var pcg32 = factory({ * 'seed': 1234 * }); * -* var v = minstd(); +* var v = pcg32(); * // returns 20739838 */ diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js index e5c5b29bed11..52e288dcb71f 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js @@ -84,19 +84,19 @@ var randuint32 = require( './rand_uint32.js' ); * - Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042](http://dx.doi.org/10.1145/63039.63042). * - Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press. * -* @function minstd +* @function pcg32 * @type {PRNG} * @returns {PositiveInteger} pseudorandom integer * * @example -* var v = minstd(); +* var v = pcg32(); * // returns */ -var minstd = factory({ +var pcg32 = factory({ 'seed': randuint32() }); // EXPORTS // -module.exports = minstd; +module.exports = pcg32; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/package.json b/lib/node_modules/@stdlib/random/base/pcg32/package.json index db780495952b..364c5e1d97d6 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/package.json +++ b/lib/node_modules/@stdlib/random/base/pcg32/package.json @@ -1,5 +1,5 @@ { - "name": "@stdlib/random/base/minstd", + "name": "@stdlib/random/base/pcg32", "version": "0.0.0", "description": "A linear congruential pseudorandom number generator (LCG) based on Park and Miller.", "license": "Apache-2.0", @@ -66,7 +66,7 @@ "uniform", "generator", "lcg", - "minstd", + "pcg32", "lehmer", "park", "miller", diff --git a/lib/node_modules/@stdlib/random/base/pcg32/src/main.c b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c index 59c1acf5fbe5..3ea80c6c676e 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/src/main.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c @@ -16,7 +16,7 @@ * limitations under the License. */ -#include "stdlib/random/base/minstd.h" +#include "stdlib/random/base/pcg32.h" #include "stdlib/random/base/shared.h" #include #include @@ -25,7 +25,7 @@ // 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 minstd_free( struct BasePRNGObject *obj ); +static inline void pcg32_free( struct BasePRNGObject *obj ); // Define the LCG multiplier: static const uint32_t A = 16807; @@ -37,19 +37,19 @@ static const uint32_t MAX_INT32 = 0x7fffffff; static const double NORMALIZATION_CONSTANT = 2147483646.0; // MAX_INT32 - 1 /** -* MINSTD PRNG. +* PCG32 PRNG. * */ -static const struct BasePRNG minstd_prng = { - "minstd", // name +static const struct BasePRNG pcg32_prng = { + "pcg32", // name (uint64_t)1, // min (uint64_t)MAX_INT32-1, // max: (2^{31}-1) - 1 0.0, // min (normalized) (MAX_INT32-2) / NORMALIZATION_CONSTANT, // max (normalized): (MAX-1)/MAX - sizeof( stdlib_base_random_minstd_state_t ), // state_size + sizeof( stdlib_base_random_pcg32_state_t ), // state_size &next, // next() &normalized, // normalized() - &minstd_free // free() + &pcg32_free // free() }; /** @@ -64,11 +64,11 @@ static const struct BasePRNG minstd_prng = { * @return status code */ static inline int8_t next( struct BasePRNGObject *obj, uint64_t *out ) { - if ( obj == NULL || obj->prng != &minstd_prng ) { + if ( obj == NULL || obj->prng != &pcg32_prng ) { return -1; } // Retrieve the state object: - stdlib_base_random_minstd_state_t *so = (stdlib_base_random_minstd_state_t *)( obj->state ); + stdlib_base_random_pcg32_state_t *so = (stdlib_base_random_pcg32_state_t *)( obj->state ); // Retrieve the current state: uint32_t state = so->state; @@ -113,8 +113,8 @@ static inline int8_t normalized( struct BasePRNGObject *obj, double *out ) { * * @param obj PRNG object */ -static inline void minstd_free( struct BasePRNGObject *obj ) { - if ( obj == NULL || obj->prng != &minstd_prng ) { +static inline void pcg32_free( struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &pcg32_prng ) { return; } free( obj->state ); @@ -133,14 +133,14 @@ static inline void minstd_free( struct BasePRNGObject *obj ) { * @return pointer to a dynamically allocated PRNG or, if unable to allocate memory, a null pointer * * @example -* #include "stdlib/random/base/minstd.h" +* #include "stdlib/random/base/pcg32.h" * #include "stdlib/random/base/shared.h" * #include * #include * #include * * // Create a PRNG: -* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); * if ( obj == NULL ) { * fprintf( stderr, "Error allocating memory.\n" ); * exit( 1 ); @@ -164,16 +164,16 @@ static inline void minstd_free( struct BasePRNGObject *obj ) { * // ... * * // Free allocated memory: -* stdlib_base_random_minstd_free( obj ); +* stdlib_base_random_pcg32_free( obj ); */ -struct BasePRNGObject * stdlib_base_random_minstd_allocate( const int32_t seed ) { +struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const int32_t seed ) { uint32_t iseed; struct BasePRNGObject *obj = malloc( sizeof( struct BasePRNGObject ) ); if ( obj == NULL ) { return NULL; } - stdlib_base_random_minstd_state_t *state = malloc( sizeof( stdlib_base_random_minstd_state_t ) ); + stdlib_base_random_pcg32_state_t *state = malloc( sizeof( stdlib_base_random_pcg32_state_t ) ); if ( state == NULL ) { free( obj ); // prevent memory leaks return NULL; @@ -191,7 +191,7 @@ struct BasePRNGObject * stdlib_base_random_minstd_allocate( const int32_t seed ) state->seed = (uint32_t)iseed; state->state = (uint32_t)iseed; - obj->prng = &minstd_prng; + obj->prng = &pcg32_prng; obj->state = state; return obj; @@ -203,14 +203,14 @@ struct BasePRNGObject * stdlib_base_random_minstd_allocate( const int32_t seed ) * @param obj PRNG object * * @example -* #include "stdlib/random/base/minstd.h" +* #include "stdlib/random/base/pcg32.h" * #include "stdlib/random/base/shared.h" * #include * #include * #include * * // Create a PRNG: -* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); * if ( obj == NULL ) { * fprintf( stderr, "Error allocating memory.\n" ); * exit( 1 ); @@ -234,10 +234,10 @@ struct BasePRNGObject * stdlib_base_random_minstd_allocate( const int32_t seed ) * // ... * * // Free allocated memory: -* stdlib_base_random_minstd_free( obj ); +* stdlib_base_random_pcg32_free( obj ); */ -void stdlib_base_random_minstd_free( struct BasePRNGObject *obj ) { - if ( obj == NULL || obj->prng != &minstd_prng ) { +void stdlib_base_random_pcg32_free( struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &pcg32_prng ) { return; } obj->prng->free( obj ); @@ -255,21 +255,21 @@ void stdlib_base_random_minstd_free( struct BasePRNGObject *obj ) { * @return status code * * @example -* #include "stdlib/random/base/minstd.h" +* #include "stdlib/random/base/pcg32.h" * #include "stdlib/random/base/shared.h" * #include * #include * #include * * // Create a PRNG: -* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); * if ( obj == NULL ) { * fprintf( stderr, "Error allocating memory.\n" ); * exit( 1 ); * } * * int32_t seed; -* int8_t status = stdlib_base_random_minstd_seed( obj, &seed ); +* int8_t status = stdlib_base_random_pcg32_seed( obj, &seed ); * if ( status != 0 ) { * fprintf( stderr, "Error encountered when attempting to retrieve the PRNG seed.\n" ); * exit( 1 ); @@ -278,14 +278,14 @@ void stdlib_base_random_minstd_free( struct BasePRNGObject *obj ) { * // Use the seed to, e.g., create another PRNG which will generate the same sequence... * * // Free allocated memory: -* stdlib_base_random_minstd_free( obj ); +* stdlib_base_random_pcg32_free( obj ); */ -int8_t stdlib_base_random_minstd_seed( const struct BasePRNGObject *obj, int32_t *out ) { - if ( obj == NULL || obj->prng != &minstd_prng ) { +int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, int32_t *out ) { + if ( obj == NULL || obj->prng != &pcg32_prng ) { return -1; } - // Retrieve the MINSTD state object: - const stdlib_base_random_minstd_state_t *state = (stdlib_base_random_minstd_state_t *)( obj->state ); + // Retrieve the PCG32 state object: + const stdlib_base_random_pcg32_state_t *state = (stdlib_base_random_pcg32_state_t *)( obj->state ); // Set the output value: *out = (int32_t)( state->seed ); @@ -304,20 +304,20 @@ int8_t stdlib_base_random_minstd_seed( const struct BasePRNGObject *obj, int32_t * @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/minstd.h" +* #include "stdlib/random/base/pcg32.h" * #include "stdlib/random/base/shared.h" * #include * #include * #include * * // Create a PRNG: -* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); * if ( obj == NULL ) { * fprintf( stderr, "Error allocating memory.\n" ); * exit( 1 ); * } * -* void *state = stdlib_base_random_minstd_state( obj ); +* void *state = stdlib_base_random_pcg32_state( obj ); * if ( state == NULL ) { * fprintf( stderr, "Unable to retrieve PRNG state.\n" ); * exit( 1 ); @@ -326,11 +326,11 @@ int8_t stdlib_base_random_minstd_seed( const struct BasePRNGObject *obj, int32_t * // 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_minstd_free( obj ); +* stdlib_base_random_pcg32_free( obj ); * free( state ); */ -void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ) { - if ( obj == NULL || obj->prng != &minstd_prng ) { +void * stdlib_base_random_pcg32_state( const struct BasePRNGObject *obj ) { + if ( obj == NULL || obj->prng != &pcg32_prng ) { return NULL; } void *state = malloc( obj->prng->state_size ); @@ -353,14 +353,14 @@ void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ) { * @return status code * * @example -* #include "stdlib/random/base/minstd.h" +* #include "stdlib/random/base/pcg32.h" * #include "stdlib/random/base/shared.h" * #include * #include * #include * * // Create a PRNG: -* struct BasePRNGObject *obj = stdlib_base_random_minstd_allocate( 12345 ); +* struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); * if ( obj == NULL ) { * fprintf( stderr, "Error allocating memory.\n" ); * exit( 1 ); @@ -384,7 +384,7 @@ void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ) { * // ... * * // Retrieve the current PRNG state... -* void *state = stdlib_base_random_minstd_state( obj ); +* void *state = stdlib_base_random_pcg32_state( obj ); * if ( state == NULL ) { * fprintf( stderr, "Error encountered when attempting to retrieve PRNG state.\n" ); * exit( 1 ); @@ -401,7 +401,7 @@ void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ) { * // ... * * // Reset the PRNG to a previous state... -* status = stdlib_base_random_minstd_set( obj, state ); +* status = stdlib_base_random_pcg32_set( obj, state ); * if ( status != 0 ) { * fprintf( stderr, "Error encountered when attempting to set PRNG state.\n" ); * exit( 1 ); @@ -418,11 +418,11 @@ void * stdlib_base_random_minstd_state( const struct BasePRNGObject *obj ) { * // ... * * // Free allocated memory: -* stdlib_base_random_minstd_free( obj ); +* stdlib_base_random_pcg32_free( obj ); * free( state ); */ -int8_t stdlib_base_random_minstd_set( struct BasePRNGObject *obj, const void *state ) { - if ( obj == NULL || state == NULL || obj->prng != &minstd_prng ) { +int8_t stdlib_base_random_pcg32_set( struct BasePRNGObject *obj, const void *state ) { + if ( obj == NULL || state == NULL || obj->prng != &pcg32_prng ) { return -1; } memcpy( obj->state, state, obj->prng->state_size ); diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js index fbfe8edd8af9..4950d6d19452 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js @@ -360,13 +360,13 @@ tape( 'if provided a `state` option containing an incompatible seed length, the }); tape( 'the function returns a pseudorandom number generator (no options)', function test( t ) { - var minstd; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); for ( i = 0; i < 1e3; i++ ) { - v = minstd(); + v = pcg32(); t.equal( typeof v, 'number', 'returns a number' ); t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32 (inclusive)' ); @@ -375,13 +375,13 @@ tape( 'the function returns a pseudorandom number generator (no options)', funct }); tape( 'the function returns a pseudorandom number generator (options; no seed)', function test( t ) { - var minstd; + var pcg32; var v; var i; - minstd = factory( {} ); + pcg32 = factory( {} ); for ( i = 0; i < 1e3; i++ ) { - v = minstd(); + v = pcg32(); t.equal( typeof v, 'number', 'returns a number' ); t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32 (inclusive)' ); @@ -390,8 +390,8 @@ tape( 'the function returns a pseudorandom number generator (options; no seed)', }); tape( 'the function returns a seeded pseudorandom number generator (integer seed)', function test( t ) { - var minstd1; - var minstd2; + var pcg321; + var pcg322; var seed; var v1; var v2; @@ -399,26 +399,26 @@ tape( 'the function returns a seeded pseudorandom number generator (integer seed seed = now(); - minstd1 = factory({ + pcg321 = factory({ 'seed': seed }); - minstd2 = factory({ + pcg322 = factory({ 'seed': seed }); - t.notEqual( minstd1, minstd2, 'separate generators' ); + t.notEqual( pcg321, pcg322, 'separate generators' ); for ( i = 0; i < 1e3; i++ ) { - v1 = minstd1(); - v2 = minstd2(); + v1 = pcg321(); + v2 = pcg322(); t.equal( v1, v2, 'both return same number' ); } t.end(); }); tape( 'the function returns a seeded pseudorandom number generator (array seed)', function test( t ) { - var minstd1; - var minstd2; + var pcg321; + var pcg322; var seed; var v1; var v2; @@ -426,49 +426,49 @@ tape( 'the function returns a seeded pseudorandom number generator (array seed)' seed = [ now() ]; - minstd1 = factory({ + pcg321 = factory({ 'seed': seed }); - minstd2 = factory({ + pcg322 = factory({ 'seed': seed }); - t.notEqual( minstd1, minstd2, 'separate generators' ); + t.notEqual( pcg321, pcg322, 'separate generators' ); for ( i = 0; i < 1e3; i++ ) { - v1 = minstd1(); - v2 = minstd2(); + v1 = pcg321(); + v2 = pcg322(); t.equal( v1, v2, 'both return same number' ); } t.end(); }); tape( 'attached to the returned function is the generator name', function test( t ) { - var minstd = factory(); - t.equal( minstd.NAME, 'minstd', 'has property' ); + var pcg32 = factory(); + t.equal( pcg32.NAME, 'pcg32', 'has property' ); t.end(); }); tape( 'attached to the returned function is the minimum possible generated number', function test( t ) { - var minstd = factory(); - t.equal( minstd.MIN, 0, 'has property' ); + var pcg32 = factory(); + t.equal( pcg32.MIN, 0, 'has property' ); t.end(); }); tape( 'attached to the returned function is the maximum possible generated number', function test( t ) { - var minstd = factory(); - t.equal( minstd.MAX, UINT32_MAX, 'has property' ); + var pcg32 = factory(); + t.equal( pcg32.MAX, UINT32_MAX, 'has property' ); t.end(); }); tape( 'attached to the returned function is the generator seed (integer seed)', function test( t ) { - var minstd; var actual; + var pcg32; - minstd = factory({ + pcg32 = factory({ 'seed': 12345 }); - actual = minstd.seed; + actual = pcg32.seed; t.equal( isUint32Array( actual ), true, 'has property' ); t.equal( actual.length, 1, 'has expected length' ); @@ -477,17 +477,17 @@ tape( 'attached to the returned function is the generator seed (integer seed)', }); tape( 'attached to the returned function is the generator seed (array seed)', function test( t ) { - var minstd; var actual; + var pcg32; var seed; var i; seed = [ 12345 ]; - minstd = factory({ + pcg32 = factory({ 'seed': seed }); - actual = minstd.seed; + actual = pcg32.seed; t.equal( isUint32Array( actual ), true, 'has property' ); for ( i = 0; i < seed.length; i++ ) { @@ -497,51 +497,51 @@ tape( 'attached to the returned function is the generator seed (array seed)', fu }); tape( 'attached to the returned function is the generator seed length', function test( t ) { - var minstd = factory(); - t.equal( typeof minstd.seedLength, 'number', 'has property' ); + var pcg32 = factory(); + t.equal( typeof pcg32.seedLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the returned function is the generator state', function test( t ) { - var minstd = factory(); - t.equal( isUint32Array( minstd.state ), true, 'has property' ); + var pcg32 = factory(); + t.equal( isUint32Array( pcg32.state ), true, 'has property' ); t.end(); }); tape( 'attached to the returned function is the generator state length', function test( t ) { - var minstd = factory(); - t.equal( typeof minstd.stateLength, 'number', 'has property' ); + var pcg32 = factory(); + t.equal( typeof pcg32.stateLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the returned function is the generator state size', function test( t ) { - var minstd = factory(); - t.equal( typeof minstd.byteLength, 'number', 'has property' ); + var pcg32 = factory(); + t.equal( typeof pcg32.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 minstd; + var pcg32; var o; - minstd = factory(); - t.equal( typeof minstd.toJSON, 'function', 'has method' ); + pcg32 = factory(); + t.equal( typeof pcg32.toJSON, 'function', 'has method' ); - o = minstd.toJSON(); + o = pcg32.toJSON(); t.equal( o.type, 'PRNG', 'has property' ); - t.equal( o.name, minstd.NAME, 'has property' ); - t.deepEqual( o.state, typedarray2json( minstd.state ), 'has property' ); + t.equal( o.name, pcg32.NAME, 'has property' ); + t.deepEqual( o.state, typedarray2json( pcg32.state ), 'has property' ); t.deepEqual( o.params, [], 'has property' ); t.end(); }); tape( 'if the `state` property is set to a value other than an Uint32Array, an error is thrown', function test( t ) { - var minstd; var values; + var pcg32; var i; - minstd = factory(); + pcg32 = factory(); values = [ '3', @@ -565,18 +565,18 @@ tape( 'if the `state` property is set to a value other than an Uint32Array, an e function badValue( value ) { return function badValue() { - minstd.state = value; + pcg32.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an unsupported version, an error is thrown', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -601,18 +601,18 @@ tape( 'if the `state` property is set to an Uint32Array containing an unsupporte function badValue( value ) { return function badValue() { - minstd.state = value; + pcg32.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an unsupported number of sections, an error is thrown', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -637,18 +637,18 @@ tape( 'if the `state` property is set to an Uint32Array containing an unsupporte function badValue( value ) { return function badValue() { - minstd.state = value; + pcg32.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an unsupported state length, an error is thrown', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -673,18 +673,18 @@ tape( 'if the `state` property is set to an Uint32Array containing an unsupporte function badValue( value ) { return function badValue() { - minstd.state = value; + pcg32.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an incompatible seed length, an error is thrown', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -709,19 +709,19 @@ tape( 'if the `state` property is set to an Uint32Array containing an incompatib function badValue( value ) { return function badValue() { - minstd.state = value; + pcg32.state = value; }; } }); 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 minstd; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); for ( i = 0; i < 1e3; i++ ) { - v = minstd.normalized(); + v = pcg32.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)' ); } @@ -729,31 +729,31 @@ tape( 'attached to the returned function is a `normalized` method for generating }); tape( 'attached to the `normalized` method is the generator name', function test( t ) { - var minstd = factory(); - t.equal( minstd.normalized.NAME, 'minstd', 'has property' ); + var pcg32 = factory(); + t.equal( pcg32.normalized.NAME, 'pcg32', 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the minimum possible generated number', function test( t ) { - var minstd = factory(); - t.equal( minstd.normalized.MIN, 0.0, 'has property' ); + var pcg32 = factory(); + t.equal( pcg32.normalized.MIN, 0.0, 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the maximum possible generated number', function test( t ) { - var minstd = factory(); - t.equal( minstd.normalized.MAX, UINT32_MAX / ( UINT32_MAX + 1.0 ), 'has property' ); + var pcg32 = factory(); + t.equal( pcg32.normalized.MAX, UINT32_MAX / ( UINT32_MAX + 1.0 ), 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator seed (integer seed)', function test( t ) { - var minstd; var actual; + var pcg32; - minstd = factory({ + pcg32 = factory({ 'seed': 12345 }); - actual = minstd.normalized.seed; + actual = pcg32.normalized.seed; t.equal( isUint32Array( actual ), true, 'has property' ); t.equal( actual.length, 1, 'has expected length' ); @@ -762,16 +762,16 @@ tape( 'attached to the `normalized` method is the generator seed (integer seed)' }); tape( 'attached to the `normalized` method is the generator seed (array seed)', function test( t ) { - var minstd; var actual; + var pcg32; var seed; var i; seed = [ 12345 ]; - minstd = factory({ + pcg32 = factory({ 'seed': seed }); - actual = minstd.normalized.seed; + actual = pcg32.normalized.seed; t.equal( isUint32Array( actual ), true, 'has property' ); for ( i = 0; i < seed.length; i++ ) { @@ -781,50 +781,50 @@ tape( 'attached to the `normalized` method is the generator seed (array seed)', }); tape( 'attached to the `normalized` method is the generator seed length', function test( t ) { - var minstd = factory(); - t.equal( typeof minstd.normalized.seedLength, 'number', 'has property' ); + var pcg32 = factory(); + t.equal( typeof pcg32.normalized.seedLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator state', function test( t ) { - var minstd = factory(); - t.equal( isUint32Array( minstd.normalized.state ), true, 'has property' ); + var pcg32 = factory(); + t.equal( isUint32Array( pcg32.normalized.state ), true, 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator state length', function test( t ) { - var minstd = factory(); - t.equal( typeof minstd.normalized.stateLength, 'number', 'has property' ); + var pcg32 = factory(); + t.equal( typeof pcg32.normalized.stateLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator state size', function test( t ) { - var minstd = factory(); - t.equal( typeof minstd.normalized.byteLength, 'number', 'has property' ); + var pcg32 = factory(); + t.equal( typeof pcg32.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 minstd; + var pcg32; var o; - minstd = factory(); - t.equal( typeof minstd.normalized.toJSON, 'function', 'has method' ); + pcg32 = factory(); + t.equal( typeof pcg32.normalized.toJSON, 'function', 'has method' ); - o = minstd.normalized.toJSON(); + o = pcg32.normalized.toJSON(); t.equal( o.type, 'PRNG', 'has property' ); - t.equal( o.name, minstd.normalized.NAME, 'has property' ); - t.deepEqual( o.state, typedarray2json( minstd.normalized.state ), 'has property' ); + t.equal( o.name, pcg32.normalized.NAME, 'has property' ); + t.deepEqual( o.state, typedarray2json( pcg32.normalized.state ), 'has property' ); t.end(); }); tape( 'if the `state` property is set to a value other than an Uint32Array, an error is thrown (normalized)', function test( t ) { - var minstd; var values; + var pcg32; var i; - minstd = factory(); + pcg32 = factory(); values = [ '3', @@ -848,17 +848,17 @@ tape( 'if the `state` property is set to a value other than an Uint32Array, an e function badValue( value ) { return function badValue() { - minstd.normalized.state = value; + pcg32.normalized.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array having an unexpected length, an error is thrown (normalized)', function test( t ) { - var minstd; var values; + var pcg32; var i; - minstd = factory(); + pcg32 = factory(); values = [ new Uint32Array( 0 ), @@ -871,18 +871,18 @@ tape( 'if the `state` property is set to an Uint32Array having an unexpected len function badValue( value ) { return function badValue() { - minstd.normalized.state = value; + pcg32.normalized.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an unsupported version, an error is thrown (normalized)', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -907,18 +907,18 @@ tape( 'if the `state` property is set to an Uint32Array containing an unsupporte function badValue( value ) { return function badValue() { - minstd.normalized.state = value; + pcg32.normalized.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an unsupported number of sections, an error is thrown (normalized)', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -943,18 +943,18 @@ tape( 'if the `state` property is set to an Uint32Array containing an unsupporte function badValue( value ) { return function badValue() { - minstd.normalized.state = value; + pcg32.normalized.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an unsupported state length, an error is thrown (normalized)', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -979,18 +979,18 @@ tape( 'if the `state` property is set to an Uint32Array containing an unsupporte function badValue( value ) { return function badValue() { - minstd.normalized.state = value; + pcg32.normalized.state = value; }; } }); tape( 'if the `state` property is set to an Uint32Array containing an incompatible seed length, an error is thrown (normalized)', function test( t ) { - var minstd; var values; + var pcg32; var v; var i; - minstd = factory(); + pcg32 = factory(); values = []; @@ -1015,7 +1015,7 @@ tape( 'if the `state` property is set to an Uint32Array containing an incompatib function badValue( value ) { return function badValue() { - minstd.normalized.state = value; + pcg32.normalized.state = value; }; } }); @@ -1040,7 +1040,7 @@ tape( 'the `normalized` method returns pseudorandom numbers drawn from a uniform function gof() { var rejected; var pValue; - var minstd; + var pcg32; var bool; var i; var j; @@ -1048,10 +1048,10 @@ tape( 'the `normalized` method returns pseudorandom numbers drawn from a uniform count += 1; rejected = 0; for ( i = 0; i < N; i++ ) { - minstd = factory(); - t.ok( true, 'seed: '+minstd.seed ); + pcg32 = factory(); + t.ok( true, 'seed: '+pcg32.seed ); for ( j = 0; j < x.length; j++ ) { - x[ j ] = minstd.normalized(); + x[ j ] = pcg32.normalized(); if ( x[ j ] < 0.0 || x[ j ] > 1.0 ) { t.ok( false, 'returned a number outside support: '+x[ j ] ); } @@ -1090,41 +1090,41 @@ tape( 'the `normalized` method returns pseudorandom numbers drawn from a uniform }); tape( 'the function supports specifying the generator state', function test( t ) { - var minstd; + var pcg32; var state; var arr; var i; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Create another PRNG using the captured state: - minstd = factory({ + pcg32 = factory({ 'state': state }); // Replay previously generated values... for ( i = 0; i < 100; i++ ) { - t.equal( minstd(), arr[ i ], 'returns expected value. i: '+i+'.' ); + t.equal( pcg32(), arr[ i ], 'returns expected value. i: '+i+'.' ); } t.end(); }); tape( 'the function supports specifying a shared generator state', function test( t ) { - var minstd; var shared; + var pcg32; var state; var rand1; var rand2; @@ -1134,19 +1134,19 @@ tape( 'the function supports specifying a shared generator state', function test var i; var j; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: @@ -1175,38 +1175,38 @@ tape( 'the function supports specifying a shared generator state', function test }); tape( 'the returned function supports setting the generator state', function test( t ) { - var minstd; + var pcg32; var state; var arr; var i; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Set the state: - minstd.state = state; + pcg32.state = state; // Replay previously generated values... for ( i = 0; i < 100; i++ ) { - t.equal( minstd(), arr[ i ], 'returns expected value. i: '+i+'.' ); + t.equal( pcg32(), arr[ i ], 'returns expected value. i: '+i+'.' ); } t.end(); }); tape( 'the returned function supports setting the generator state to a state array having a different length', function test( t ) { - var minstd; var shared; + var pcg32; var state; var rand1; var rand2; @@ -1216,21 +1216,21 @@ tape( 'the returned function supports setting the generator state to a state arr var i; // Seed length: 2 - minstd = factory({ + pcg32 = factory({ 'seed': [ 1234, 5678 ] }); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: @@ -1265,68 +1265,68 @@ tape( 'the returned function supports setting the generator state to a state arr }); tape( 'the returned function supports setting the generator state (normalized)', function test( t ) { - var minstd; + var pcg32; var state; var arr; var i; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd.normalized(); + pcg32.normalized(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd.normalized() ); + arr.push( pcg32.normalized() ); } // Set the state: - minstd.state = state; + pcg32.state = state; // Replay previously generated values... for ( i = 0; i < 100; i++ ) { - t.equal( minstd.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + t.equal( pcg32.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); } t.end(); }); tape( 'the returned function supports setting the generator state (normalized)', function test( t ) { - var minstd; + var pcg32; var state; var arr; var i; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd.normalized(); + pcg32.normalized(); } // Capture the current state: - state = minstd.normalized.state; + state = pcg32.normalized.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd.normalized() ); + arr.push( pcg32.normalized() ); } // Set the state: - minstd.normalized.state = state; + pcg32.normalized.state = state; // Replay previously generated values... for ( i = 0; i < 100; i++ ) { - t.equal( minstd.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + t.equal( pcg32.normalized(), 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 minstd; var shared; + var pcg32; var state; var rand1; var rand2; @@ -1336,19 +1336,19 @@ tape( 'the returned function supports setting a shared generator state (same len var i; var j; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: @@ -1395,8 +1395,8 @@ tape( 'the returned function supports setting a shared generator state (same len }); tape( 'the returned function supports setting a shared generator state (different length)', function test( t ) { - var minstd; var shared; + var pcg32; var state; var rand1; var rand2; @@ -1407,19 +1407,19 @@ tape( 'the returned function supports setting a shared generator state (differen var i; var j; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: @@ -1492,8 +1492,8 @@ tape( 'the returned function supports setting a shared generator state (differen }); tape( 'the returned function supports setting a shared generator state (no initial shared state)', function test( t ) { - var minstd; var shared; + var pcg32; var state; var rand1; var rand2; @@ -1503,14 +1503,14 @@ tape( 'the returned function supports setting a shared generator state (no initi var i; var j; - minstd = factory(); + pcg32 = factory(); // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Create a copy of the state (to prevent mutation) which will be shared by more than one PRNG: shared = new Uint32Array( state ); @@ -1518,7 +1518,7 @@ tape( 'the returned function supports setting a shared generator state (no initi // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Create PRNGs using the captured state: diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js index 90b809a9d169..dacf020b0058 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js @@ -24,69 +24,69 @@ var tape = require( 'tape' ); var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); var isPositiveInteger = require( '@stdlib/math/base/assert/is-positive-integer' ); var isUint32Array = require( '@stdlib/assert/is-uint32array' ); -var minstd = require( './../lib' ); +var pcg32 = require( './../lib' ); // TESTS // tape( 'main export is a function', function test( t ) { t.ok( true, __filename ); - t.strictEqual( typeof minstd, 'function', 'main export is a function' ); + t.strictEqual( typeof pcg32, '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 minstd.normalized, 'function', 'has method' ); + t.equal( typeof pcg32.normalized, 'function', 'has method' ); t.end(); }); tape( 'attached to the main export is a method to generate linear congruential pseudorandom number generator', function test( t ) { - t.equal( typeof minstd.factory, 'function', 'has method' ); + t.equal( typeof pcg32.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 minstd.toJSON, 'function', 'has method' ); + t.equal( typeof pcg32.toJSON, 'function', 'has method' ); t.end(); }); tape( 'attached to the main export is the generator name', function test( t ) { - t.equal( minstd.NAME, 'minstd', 'has property' ); + t.equal( pcg32.NAME, 'pcg32', 'has property' ); t.end(); }); tape( 'attached to the main export is the minimum possible generated number', function test( t ) { - t.equal( minstd.MIN, 0, 'has property' ); + t.equal( pcg32.MIN, 0, 'has property' ); t.end(); }); tape( 'attached to the main export is the maximum possible generated number', function test( t ) { - t.equal( minstd.MAX, UINT32_MAX, 'has property' ); + t.equal( pcg32.MAX, UINT32_MAX, 'has property' ); t.end(); }); tape( 'attached to the main export is the generator seed', function test( t ) { - t.equal( isUint32Array( minstd.seed ), true, 'has property' ); + t.equal( isUint32Array( pcg32.seed ), true, 'has property' ); t.end(); }); tape( 'attached to the main export is the generator seed length', function test( t ) { - t.equal( typeof minstd.seedLength, 'number', 'has property' ); + t.equal( typeof pcg32.seedLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the main export is the generator state', function test( t ) { - t.equal( isUint32Array( minstd.state ), true, 'has property' ); + t.equal( isUint32Array( pcg32.state ), true, 'has property' ); t.end(); }); tape( 'attached to the main export is the generator state length', function test( t ) { - t.equal( typeof minstd.stateLength, 'number', 'has property' ); + t.equal( typeof pcg32.stateLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the main export is the generator state size', function test( t ) { - t.equal( typeof minstd.byteLength, 'number', 'has property' ); + t.equal( typeof pcg32.byteLength, 'number', 'has property' ); t.end(); }); @@ -94,7 +94,7 @@ tape( 'the function returns pseudorandom integers strictly between 0 and 2^32-1 var v; var i; for ( i = 0; i < 1e3; i++ ) { - v = minstd(); + v = pcg32(); t.equal( typeof v, 'number', 'returns a number' ); t.equal( isPositiveInteger( v ), true, 'returns a positive integer' ); t.equal( v >= 0 && v <= UINT32_MAX, true, 'returns an integer between 0 and 2^32-1 (inclusive)' ); @@ -106,7 +106,7 @@ tape( 'the `normalized` method returns pseudorandom numbers strictly between 0 ( var v; var i; for ( i = 0; i < 1e3; i++ ) { - v = minstd.normalized(); + v = pcg32.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)' ); } @@ -114,47 +114,47 @@ tape( 'the `normalized` method returns pseudorandom numbers strictly between 0 ( }); tape( 'attached to the `normalized` method is the generator name', function test( t ) { - t.equal( minstd.normalized.NAME, 'minstd', 'has property' ); + t.equal( pcg32.normalized.NAME, 'pcg32', 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the minimum possible generated number', function test( t ) { - t.equal( minstd.normalized.MIN, 0.0, 'has property' ); + t.equal( pcg32.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( minstd.normalized.MAX, UINT32_MAX / ( UINT32_MAX + 1.0 ), 'has property' ); + t.equal( pcg32.normalized.MAX, UINT32_MAX / ( UINT32_MAX + 1.0 ), 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator seed', function test( t ) { - t.equal( isUint32Array( minstd.normalized.seed ), true, 'has property' ); + t.equal( isUint32Array( pcg32.normalized.seed ), true, 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator seed length', function test( t ) { - t.equal( typeof minstd.normalized.seedLength, 'number', 'has property' ); + t.equal( typeof pcg32.normalized.seedLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator state', function test( t ) { - t.equal( isUint32Array( minstd.normalized.state ), true, 'has property' ); + t.equal( isUint32Array( pcg32.normalized.state ), true, 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator state length', function test( t ) { - t.equal( typeof minstd.normalized.stateLength, 'number', 'has property' ); + t.equal( typeof pcg32.normalized.stateLength, 'number', 'has property' ); t.end(); }); tape( 'attached to the `normalized` method is the generator state size', function test( t ) { - t.equal( typeof minstd.normalized.byteLength, 'number', 'has property' ); + t.equal( typeof pcg32.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 minstd.normalized.toJSON, 'function', 'has method' ); + t.equal( typeof pcg32.normalized.toJSON, 'function', 'has method' ); t.end(); }); @@ -165,22 +165,22 @@ tape( 'the generator supports setting the generator state', function test( t ) { // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd(); + pcg32(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd() ); + arr.push( pcg32() ); } // Set the state: - minstd.state = state; + pcg32.state = state; // Replay previously generated values... for ( i = 0; i < 100; i++ ) { - t.equal( minstd(), arr[ i ], 'returns expected value. i: '+i+'.' ); + t.equal( pcg32(), arr[ i ], 'returns expected value. i: '+i+'.' ); } t.end(); }); @@ -192,22 +192,22 @@ tape( 'the generator supports setting the generator state (normalized)', functio // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd.normalized(); + pcg32.normalized(); } // Capture the current state: - state = minstd.state; + state = pcg32.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd.normalized() ); + arr.push( pcg32.normalized() ); } // Set the state: - minstd.state = state; + pcg32.state = state; // Replay previously generated values... for ( i = 0; i < 100; i++ ) { - t.equal( minstd.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + t.equal( pcg32.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); } t.end(); }); @@ -219,22 +219,22 @@ tape( 'the generator supports setting the generator state (normalized)', functio // Move to a future state... for ( i = 0; i < 100; i++ ) { - minstd.normalized(); + pcg32.normalized(); } // Capture the current state: - state = minstd.normalized.state; + state = pcg32.normalized.state; // Move to a future state... arr = []; for ( i = 0; i < 100; i++ ) { - arr.push( minstd.normalized() ); + arr.push( pcg32.normalized() ); } // Set the state: - minstd.normalized.state = state; + pcg32.normalized.state = state; // Replay previously generated values... for ( i = 0; i < 100; i++ ) { - t.equal( minstd.normalized(), arr[ i ], 'returns expected value. i: '+i+'.' ); + t.equal( pcg32.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..a39be46cc557 100644 --- a/lib/node_modules/@stdlib/types/index.d.ts +++ b/lib/node_modules/@stdlib/types/index.d.ts @@ -4274,6 +4274,25 @@ declare module '@stdlib/types/random' { * const s: PRNGStateMINSTD = new Int32Array( 6 ); */ type PRNGStateMINSTD = Int32Array; + + /** + * A pseudorandom number generator (PRNG) seed for the 32-bit PCG PRNG. + * + * @example + * const s: PRNGSeedPCG32 = 12345; + * + * @example + * const s: PRNGSeedPCG32 = [ 12345, 67891 ]; + */ + type PRNGSeedPCG32 = number | ArrayLike; + + /** + * A pseudorandom number generator (PRNG) state for the 32-bit PCG PRNG. + * + * @example + * const s: PRNGStatePCG32 = new Uint32Array( 7 ); + */ + type PRNGStatePCG32 = Uint32Array; } /** diff --git a/lib/node_modules/@stdlib/types/test.ts b/lib/node_modules/@stdlib/types/test.ts index 7f1fd8511abd..af9aa7d7d4f7 100644 --- a/lib/node_modules/@stdlib/types/test.ts +++ b/lib/node_modules/@stdlib/types/test.ts @@ -666,6 +666,22 @@ function cmplx128Array(): array.Complex128Array { if ( s6[ 0 ] !== 0 ) { throw new Error( 'something went wrong' ); } + + const s10: random.PRNGSeedPCG32 = 12345; + if ( s10 !== 12345 ) { + throw new Error( 'something went wrong' ); + } + + const s11: random.PRNGSeedPCG32 = new Uint32Array( 10 ); + if ( s11[ 0 ] !== 0 ) { + throw new Error( 'something went wrong' ); + } + + const s12: random.PRNGStatePCG32 = new Uint32Array( 10 ); + if ( s12[ 0 ] !== 0 ) { + throw new Error( 'something went wrong' ); + } + } // The compiler should not throw an error when using slice types... From 9a88df51fae9a23d98df62cff6ff30d737acf6c9 Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Fri, 4 Apr 2025 06:32:58 +0600 Subject: [PATCH 4/9] docs: add seed and state types for pcg32 in `tools/docs/jsdoc/typedefs` --- tools/docs/jsdoc/typedefs/random.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/docs/jsdoc/typedefs/random.js b/tools/docs/jsdoc/typedefs/random.js index 1aaaddb2ae3a..5949978d252f 100644 --- a/tools/docs/jsdoc/typedefs/random.js +++ b/tools/docs/jsdoc/typedefs/random.js @@ -27,3 +27,15 @@ * * @type {Int32Array} PRNGStateMINSTD */ + +/** +* A pseudorandom number generator (PRNG) seed for the 32-bit PCG PRNG. +* +* @type {(uinteger32|Collection)} PRNGSeedPCG32 +*/ + +/** +* A pseudorandom number generator (PRNG) state for the 32-bit PCG PRNG. +* +* @type {Uint32Array} PRNGStatePCG32 +*/ From 9a6d63ff1aab656859978d297e758451003e6dd0 Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Fri, 4 Apr 2025 17:59:14 +0600 Subject: [PATCH 5/9] refactor: updated 64-bit operations to follow big-endian notation --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/random/base/pcg32/lib/factory.js | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js index fcbfc97441b2..fc5fa1572fec 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js @@ -46,11 +46,11 @@ var randuint32 = require( './rand_uint32.js' ); var NORMALIZATION_CONSTANT = UINT32_MAX + 1; var MAX_SEED = UINT32_MAX >>> 0; // asm type annotation -// LCG multiplier -var MULTIPLIER = [ 0x4C957F2D >>> 0, 0x5851F42D >>> 0 ]; // asm type annotation +// Define the LCG multiplier +var MULTIPLIER = [ 0x5851F42D >>> 0, 0x4C957F2D >>> 0 ]; // asm type annotation -// LCG increment, controls the stream. #TODO: Make user definable like seed -var INCREMENT = [ 0x0000DEAD >>> 0, 0xBEEF0000 >>> 0 ]; // asm type annotation +// Define the LCG increment which controls the stream. #TODO: Make user definable like seed +var INCREMENT = [ 0xBEEF0000 >>> 0, 0x0000DEAD >>> 0 ]; // asm type annotation // Define the size of state as a multiple of 32-bits var N = 2; @@ -216,10 +216,11 @@ function factory( options ) { // Initialize the internal PRNG state: pcg32(); - state.set( addUint64( state, [ - seed[ 0 ], - ( slen > 1 ) ? seed[ 1 ] : 0 - ]) ); + if ( slen > 1 ) { + state.set( addUint64( state, [ seed[ 0 ], seed[ 1 ] ] ) ); + } else { + state.set( addUint64( state, [ 0, seed[ 0 ] ] ) ); + } pcg32(); } 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 ) ); @@ -249,7 +250,7 @@ function factory( options ) { // Initialize the internal PRNG state: pcg32(); - state.set( addUint64( state, [ seed[ 0 ], 0 ] ) ); + state.set( addUint64( state, [ 0, seed[ 0 ] ] ) ); pcg32(); } @@ -417,56 +418,57 @@ function factory( options ) { state.set( addUint64( multUint64( oldstate, MULTIPLIER ), INCREMENT ) ); - xorshifted[0] = ( ( oldstate[0] >>> 18 ) | ( oldstate[1] << 14 ) ) >>> 0; - xorshifted[1] = ( oldstate[1] >>> 18 ) >>> 0; + xorshifted[1] = ( ( oldstate[1] >>> 18 ) | ( oldstate[0] << 14 ) ) >>> 0; + xorshifted[0] = ( oldstate[0] >>> 18 ) >>> 0; - xorshifted[0] ^= oldstate[0]; xorshifted[1] ^= oldstate[1]; + xorshifted[0] ^= oldstate[0]; - xorshifted[0] = ( ( xorshifted[0] >>> 27 ) | ( xorshifted[1] << 5 ) ) >>> 0; - xorshifted[1] = ( xorshifted[1] >>> 27 ) >>> 0; + xorshifted[1] = ( ( xorshifted[1] >>> 27 ) | ( xorshifted[0] << 5 ) ) >>> 0; + xorshifted[0] = ( xorshifted[0] >>> 27 ) >>> 0; - rot = oldstate[1] >>> 27; - return ( (xorshifted[0] >>> rot) | ( xorshifted[0] << ( -rot & 31 ) ) ) >>> 0; + rot = oldstate[0] >>> 27; + return ( (xorshifted[1] >>> rot) | ( xorshifted[1] << ( -rot & 31 ) ) ) >>> 0; } /** * Adds two 64-bit unsigned integers. * * @private - * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] - * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] - * @returns {ArrayLike} 64-bit unsigned integer sum as [ low, high ] + * @param {ArrayLike} a - 64-bit unsigned integer as [ high, low ] + * @param {ArrayLike} b - 64-bit unsigned integer as [ high, low ] + * @returns {ArrayLike} 64-bit unsigned integer sum as [ high, low ] */ function addUint64( a, b ) { - var la = a[0] >>> 0; - var ha = a[1] >>> 0; - var lb = b[0] >>> 0; - var hb = b[1] >>> 0; + var la = a[1] >>> 0; + var ha = a[0] >>> 0; + var lb = b[1] >>> 0; + var hb = b[0] >>> 0; var ls = ( la + lb ) >>> 0; var hs = ( ha + hb + ( ls < la ) ) >>> 0; - return [ ls >>> 0, hs >>> 0 ]; + return [ hs >>> 0, ls >>> 0 ]; } /** * Multiplies two 64-bit unsigned integers. * * @private - * @param {ArrayLike} a - 64-bit unsigned integer as [ low, high ] - * @param {ArrayLike} b - 64-bit unsigned integer as [ low, high ] - * @returns {ArrayLike} 64-bit unsigned integer product as [ low, high ] + * @param {ArrayLike} a - 64-bit unsigned integer as [ high, low ] + * @param {ArrayLike} b - 64-bit unsigned integer as [ high, low ] + * @returns {ArrayLike} 64-bit unsigned integer product as [ high, low ] */ function multUint64( a, b ) { - var la = a[0] >>> 0; - var ha = a[1] >>> 0; - var lb = b[0] >>> 0; - var hb = b[1] >>> 0; + var la = a[1] >>> 0; + var ha = a[0] >>> 0; + var lb = b[1] >>> 0; + var hb = b[0] >>> 0; + var m1 = umul( la, hb ) >>> 0; var m2 = umul( ha, lb ) >>> 0; var p = umuldw( la, lb ); - p = [ p[ 1 ], p[ 0 ] ]; // swap to make little-endian - p[ 1 ] = ( p[ 1 ] + m1 + m2 ) >>> 0; + + p[ 0 ] = ( p[ 0 ] + m1 + m2 ) >>> 0; return p; } From 16f62aa6f2350a4efbaa6ec457c1b00f9bdb26bb Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Fri, 4 Apr 2025 21:04:29 +0600 Subject: [PATCH 6/9] feat: add C implementation for pcg32 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../pcg32/include/stdlib/random/base/pcg32.h | 8 +- .../@stdlib/random/base/pcg32/lib/factory.js | 4 +- .../@stdlib/random/base/pcg32/src/main.c | 89 ++++++++++--------- 3 files changed, 54 insertions(+), 47 deletions(-) diff --git a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h index 9276204ea2bf..4f52e4abd053 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h +++ b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h @@ -33,14 +33,14 @@ extern "C" { * Declare an opaque type definition for the PRNG state. */ typedef struct { - uint32_t seed; - uint32_t state; + uint64_t seed; + uint64_t state; } stdlib_base_random_pcg32_state_t; /** * Returns a pointer to a dynamically allocated PRNG. */ -struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const int32_t seed ); +struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const int64_t seed ); /** * Frees a PRNG's allocated memory. @@ -50,7 +50,7 @@ void stdlib_base_random_pcg32_free( struct BasePRNGObject *obj ); /** * Returns a PRNG seed. */ -int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, int32_t *out ); +int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, int64_t *out ); /** * Returns a copy of the current PRNG state. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js index fc5fa1572fec..c09da08457da 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js @@ -46,10 +46,10 @@ var randuint32 = require( './rand_uint32.js' ); var NORMALIZATION_CONSTANT = UINT32_MAX + 1; var MAX_SEED = UINT32_MAX >>> 0; // asm type annotation -// Define the LCG multiplier +// Define the LCG multiplier: var MULTIPLIER = [ 0x5851F42D >>> 0, 0x4C957F2D >>> 0 ]; // asm type annotation -// Define the LCG increment which controls the stream. #TODO: Make user definable like seed +// Define the LCG increment: #TODO: Make user definable like seed var INCREMENT = [ 0xBEEF0000 >>> 0, 0x0000DEAD >>> 0 ]; // asm type annotation // Define the size of state as a multiple of 32-bits diff --git a/lib/node_modules/@stdlib/random/base/pcg32/src/main.c b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c index 3ea80c6c676e..bb871df27e9f 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/src/main.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c @@ -28,28 +28,31 @@ static inline int8_t normalized( struct BasePRNGObject *obj, double *out ); static inline void pcg32_free( struct BasePRNGObject *obj ); // Define the LCG multiplier: -static const uint32_t A = 16807; +static const uint64_t MULTIPLIER = 0x5851F42D4C957F2DULL; -// Define the maximum signed 32-bit integer: 2147483647 => 0x7fffffff => 01111111111111111111111111111111 -static const uint32_t MAX_INT32 = 0x7fffffff; +// Define the LCG increment: #TODO: Make user definable like seed +static const uint64_t INCREMENT = 0xBEEF00000000DEADULL; + +// Define the maximum unsigned 32-bit integer: 4294967295 => 0xffffffff => 11111111111111111111111111111111 +static const uint32_t MAX_UINT32 = 0xffffffff; // Define the normalization constant: -static const double NORMALIZATION_CONSTANT = 2147483646.0; // MAX_INT32 - 1 +static const double NORMALIZATION_CONSTANT = MAX_UINT32 + 1.0; /** * PCG32 PRNG. * */ static const struct BasePRNG pcg32_prng = { - "pcg32", // name - (uint64_t)1, // min - (uint64_t)MAX_INT32-1, // max: (2^{31}-1) - 1 - 0.0, // min (normalized) - (MAX_INT32-2) / NORMALIZATION_CONSTANT, // max (normalized): (MAX-1)/MAX - sizeof( stdlib_base_random_pcg32_state_t ), // state_size - &next, // next() - &normalized, // normalized() - &pcg32_free // free() + "pcg32", // name + (uint64_t)0, // min + (uint64_t)MAX_UINT32, // max: 2^{32}-1 + 0.0, // min (normalized) + MAX_UINT32 / NORMALIZATION_CONSTANT, // max (normalized): (MAX-1)/MAX + sizeof( stdlib_base_random_pcg32_state_t ), // state_size + &next, // next() + &normalized, // normalized() + &pcg32_free // free() }; /** @@ -64,6 +67,11 @@ static const struct BasePRNG pcg32_prng = { * @return status code */ static inline int8_t next( struct BasePRNGObject *obj, uint64_t *out ) { + uint32_t xorshifted; + uint64_t oldstate; + uint32_t output; + uint32_t rot; + if ( obj == NULL || obj->prng != &pcg32_prng ) { return -1; } @@ -71,16 +79,18 @@ static inline int8_t next( struct BasePRNGObject *obj, uint64_t *out ) { stdlib_base_random_pcg32_state_t *so = (stdlib_base_random_pcg32_state_t *)( obj->state ); // Retrieve the current state: - uint32_t state = so->state; - - // Explicitly cast to 64-bit to handle integer overflow: - state = (A*(uint64_t)state) % MAX_INT32; + oldstate = so->state; // Update the PRNG state: - so->state = state; + so->state = oldstate * MULTIPLIER + INCREMENT; + + // Apply output permutaion (XSH-RR) + xorshifted = ( ( oldstate >> 18 ) ^ oldstate ) >> 27; + rot = oldstate >> 59; + output = ( xorshifted >> rot ) | ( xorshifted << ( -rot & 31 ) ); // Set the output value: - *out = (uint64_t)state; + *out = (uint64_t)output; return 0; } @@ -97,13 +107,13 @@ static inline int8_t next( struct BasePRNGObject *obj, uint64_t *out ) { * @return status code */ static inline int8_t normalized( struct BasePRNGObject *obj, double *out ) { - uint64_t state; - int8_t status = next( obj, &state ); + uint64_t output; + int8_t status = next( obj, &output ); if ( status != 0 ) { return -1; } - // Note: casting `state` to a double here is fine, as `state` will never exceed the maximum "safe" double-precision floating-point number: - *out = ((double)state-1.0) / NORMALIZATION_CONSTANT; + // Note: casting `output` to a double here is fine, as `output` will never exceed the maximum "safe" double-precision floating-point number: + *out = (double)output / NORMALIZATION_CONSTANT; return 0; } @@ -127,7 +137,7 @@ static inline void pcg32_free( struct BasePRNGObject *obj ) { * ## Notes * * - The user is responsible for freeing the allocated memory. -* - A provided `seed` is mapped to the interval `[1,2147483646]`. +* - A provided `seed` is mapped to the interval `[0,18446744073709551615]`. * * @param seed PRNG seed * @return pointer to a dynamically allocated PRNG or, if unable to allocate memory, a null pointer @@ -166,8 +176,8 @@ static inline void pcg32_free( struct BasePRNGObject *obj ) { * // Free allocated memory: * stdlib_base_random_pcg32_free( obj ); */ -struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const int32_t seed ) { - uint32_t iseed; +struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const uint64_t seed ) { + uint64_t output; struct BasePRNGObject *obj = malloc( sizeof( struct BasePRNGObject ) ); if ( obj == NULL ) { @@ -179,20 +189,17 @@ struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const int32_t seed ) return NULL; } // Ensure that the provided seed is within allowed bounds... - if ( seed == 0 ) { - iseed = 1; - } else if ( seed == MAX_INT32 ) { - iseed = MAX_INT32 - 1; - } else if ( seed < 0 ) { - iseed = -seed; - } else { - iseed = seed; - } - state->seed = (uint32_t)iseed; - state->state = (uint32_t)iseed; + // No need to check, any unsigned 64-bit integer is a valid seed - obj->prng = &pcg32_prng; + state->seed = seed; + state->state = 0; obj->state = state; + obj->prng = &pcg32_prng; + + // Initialize the internal PRNG state: + next( obj, &output ); + state->state += seed; + next( obj, &output ); return obj; } @@ -268,7 +275,7 @@ void stdlib_base_random_pcg32_free( struct BasePRNGObject *obj ) { * exit( 1 ); * } * -* int32_t seed; +* uint64_t seed; * int8_t status = stdlib_base_random_pcg32_seed( obj, &seed ); * if ( status != 0 ) { * fprintf( stderr, "Error encountered when attempting to retrieve the PRNG seed.\n" ); @@ -280,7 +287,7 @@ void stdlib_base_random_pcg32_free( struct BasePRNGObject *obj ) { * // Free allocated memory: * stdlib_base_random_pcg32_free( obj ); */ -int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, int32_t *out ) { +int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, uint64_t *out ) { if ( obj == NULL || obj->prng != &pcg32_prng ) { return -1; } @@ -288,7 +295,7 @@ int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, int32_t const stdlib_base_random_pcg32_state_t *state = (stdlib_base_random_pcg32_state_t *)( obj->state ); // Set the output value: - *out = (int32_t)( state->seed ); + *out = (uint64_t)( state->seed ); return 0; } From f3dd31711a3033713e17ba3e1e79df2ef53e4776 Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Fri, 4 Apr 2025 22:49:21 +0600 Subject: [PATCH 7/9] fix: fix seed type in C header and example --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: passed - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/random/base/pcg32/examples/c/example.c | 6 +++--- .../random/base/pcg32/include/stdlib/random/base/pcg32.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c index 072a4dcc8fa6..b3f08984d570 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c @@ -24,13 +24,13 @@ int main( void ) { int8_t status; - int32_t seed; + uint64_t seed; uint64_t v; int32_t i; double d; // Create a PRNG... - struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 12345 ); + struct BasePRNGObject *obj = stdlib_base_random_pcg32_allocate( 1234 ); if ( obj == NULL ) { fprintf( stderr, "Error allocating memory.\n" ); exit( 1 ); @@ -41,7 +41,7 @@ int main( void ) { printf( "Unable to retrieve the PRNG seed.\n" ); exit( 1 ); } - printf( "seed = %d\n", seed ); + printf( "seed = %"PRIu64"\n", seed ); printf( "name = %s\n", obj->prng->name ); printf( "min = %"PRIu64"\n", obj->prng->min ); diff --git a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h index 4f52e4abd053..154ea3cd7352 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h +++ b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h @@ -40,7 +40,7 @@ typedef struct { /** * Returns a pointer to a dynamically allocated PRNG. */ -struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const int64_t seed ); +struct BasePRNGObject * stdlib_base_random_pcg32_allocate( const uint64_t seed ); /** * Frees a PRNG's allocated memory. @@ -50,7 +50,7 @@ void stdlib_base_random_pcg32_free( struct BasePRNGObject *obj ); /** * Returns a PRNG seed. */ -int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, int64_t *out ); +int8_t stdlib_base_random_pcg32_seed( const struct BasePRNGObject *obj, uint64_t *out ); /** * Returns a copy of the current PRNG state. From 6e2abb9cdba55f0a49b2b9d338162b8b4598d4ce Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Sat, 5 Apr 2025 00:21:09 +0600 Subject: [PATCH 8/9] docs: add documentation for pcg32 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- .../@stdlib/random/base/pcg32/README.md | 54 ++++++------------- .../base/pcg32/benchmark/benchmark.factory.js | 4 +- .../random/base/pcg32/benchmark/c/benchmark.c | 2 +- .../@stdlib/random/base/pcg32/docs/repl.txt | 27 +++++----- .../random/base/pcg32/docs/types/index.d.ts | 20 +++---- .../@stdlib/random/base/pcg32/lib/factory.js | 4 +- .../@stdlib/random/base/pcg32/lib/index.js | 2 +- .../@stdlib/random/base/pcg32/lib/main.js | 46 +++++++--------- .../random/base/pcg32/lib/rand_uint32.js | 7 +-- .../@stdlib/random/base/pcg32/package.json | 8 ++- .../@stdlib/random/base/pcg32/test/test.js | 2 +- 11 files changed, 71 insertions(+), 105 deletions(-) diff --git a/lib/node_modules/@stdlib/random/base/pcg32/README.md b/lib/node_modules/@stdlib/random/base/pcg32/README.md index 47797d1c8d25..d37825e46245 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/README.md +++ b/lib/node_modules/@stdlib/random/base/pcg32/README.md @@ -20,7 +20,7 @@ limitations under the License. # PCG32 -> A linear congruential pseudorandom number generator ([LCG][lcg]) based on Park and Miller. +> A permuted congruential pseudorandom number generator ([PCG][pcg]) based on O’Neill.
@@ -32,7 +32,7 @@ var pcg32 = require( '@stdlib/random/base/pcg32' ); #### pcg32() -Returns a pseudorandom integer on the interval `[1, 2147483646]`. +Returns a pseudorandom integer on the interval `[0, 4294967295]`. ```javascript var r = pcg32(); @@ -50,7 +50,7 @@ var r = pcg32.normalized(); #### pcg32.factory( \[options] ) -Returns a linear congruential pseudorandom number generator ([LCG][lcg]). +Returns a permuted congruential pseudorandom number generator ([PCG][pcg]). ```javascript var rand = pcg32.factory(); @@ -59,10 +59,10 @@ var rand = pcg32.factory(); The function accepts the following `options`: - **seed**: pseudorandom number generator seed. -- **state**: an [`Int32Array`][@stdlib/array/int32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. +- **state**: an [`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 an `integer` on the interval `[1, 2147483646]` +By default, a random integer is used to seed the returned generator. To seed the generator, provide either an `integer` on the interval `[0, 4294967295]` ```javascript var rand = pcg32.factory({ @@ -73,13 +73,13 @@ var r = rand(); // returns 2557507945 ``` -or, for arbitrary length seeds, an array-like `object` containing signed 32-bit integers +or, for arbitrary length seeds, an array-like `object` containing unsigned 32-bit integers ```javascript -var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); var rand = pcg32.factory({ - 'seed': new Int32Array( [ 1234 ] ) + 'seed': new Uint32Array( [ 1234 ] ) }); var r = rand(); @@ -234,8 +234,8 @@ var o = pcg32.toJSON(); ## Notes -- The generator has a period of approximately `2.1e9` (see [Numerical Recipes in C, 2nd Edition](#references), p. 279). -- An [LCG][lcg] is fast and uses little memory. On the other hand, because the generator is a simple [linear congruential generator][lcg], the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. For more on the advantages and disadvantages of [LCGs][lcg], see [Wikipedia][pros-cons]. +- The generator has a period of `2^64`. +- A [PCG][pcg] is fast, compact, and has excellent statistical quality compared to classic [LCG][lcg]s. It uses a simple [LCG][lcg] under the hood but applies permutation steps to improve output randomness. While it's not cryptographically secure, it's well-suited for most non-crypto tasks like simulations or procedural generation. - 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). @@ -286,8 +286,7 @@ for ( i = 0; i < 100; i++ ) { ## References -- Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042][@park:1988]. -- Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press. +- O’Neill, Melissa E. 2014. “PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation.” Technical Report HMC-CS-2014-0905. Harvey Mudd College, Claremont, CA. [https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf][@oneill:2014].
@@ -297,17 +296,6 @@ for ( i = 0; i < 100; i++ ) { @@ -316,28 +304,16 @@ for ( i = 0; i < 100; i++ ) { diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js index ca16eef6c11a..fe10a75de8db 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js @@ -22,7 +22,7 @@ var bench = require( '@stdlib/bench' ); var isnan = require( '@stdlib/math/base/assert/is-nan' ); -var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); var pkg = require( './../package.json' ).name; var factory = require( './../lib' ).factory; @@ -81,7 +81,7 @@ bench( pkg+':factory:seed=', function benchmark( b ) { opts = {}; - seed = new Int32Array( 10 ); + seed = new Uint32Array( 10 ); for ( i = 0; i < seed.length; i++ ) { seed[ i ] = 123 + i; } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c index a6ffb56cfc31..6b08e11648ec 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c @@ -201,7 +201,7 @@ static double benchmark3( void ) { static double benchmark4( void ) { double elapsed; int8_t status; - int32_t v; + uint32_t v; double t; int i; diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt b/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt index 8b3aef05470c..f85b646f26de 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/repl.txt @@ -1,18 +1,17 @@ {{alias}}() - Returns a pseudorandom integer on the interval `[1, 2147483646]`. + Returns a pseudorandom integer on the interval `[0, 4294967295]`. - This pseudorandom number generator (PRNG) is a linear congruential - pseudorandom number generator (LCG) based on Park and Miller. + This pseudorandom number generator (PRNG) is a permuted congruential + pseudorandom number generator (PCG) based on O’Neill. - The generator has a period of approximately `2.1e9`. + The generator has a period of `2^64`. - An LCG is fast and uses little memory. On the other hand, because the - generator is a simple LCG, the generator has recognized shortcomings. By - today's PRNG standards, the generator's period is relatively short. More - importantly, the "randomness quality" of the generator's output is lacking. - These defects make the generator unsuitable, for example, in Monte Carlo - simulations and in cryptographic applications. + A PCG is fast, compact, and has excellent statistical quality compared to + classic LCGs. It uses a simple LCG under the hood but applies permutation + steps to improve output randomness. While it's not cryptographically secure, + it's well-suited for most non-crypto tasks like simulations or procedural + generation. Returns ------- @@ -38,7 +37,7 @@ {{alias}}.factory( [options] ) - Returns a linear congruential pseudorandom number generator (LCG). + Returns a permuted congruential pseudorandom number generator (PCG). Parameters ---------- @@ -47,11 +46,11 @@ options.seed: integer|ArrayLikeObject (optional) Pseudorandom number generator seed. The seed may be either a positive - signed 32-bit integer on the interval `[1, 2147483646]` or, for - arbitrary length seeds, an array-like object containing signed 32-bit + unsigned 32-bit integer on the interval `[0, 4294967295]` or, for + arbitrary length seeds, an array-like object containing unsigned 32-bit integers. - options.state: Int32Array (optional) + options.state: Uint32Array (optional) Pseudorandom number generator state. If provided, the `seed` option is ignored. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts index adc85cd22f65..b2dc4e45a6be 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts @@ -95,11 +95,11 @@ interface PRNG { } /** -* Interface for generating pseudorandom integers on the interval `[1, 2147483646]`. +* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`. */ interface NullaryFunction extends PRNG { /** - * Returns a pseudorandom integer on the interval `[1, 2147483646]`. + * Returns a pseudorandom integer on the interval `[0, 4294967295]`. * * @returns pseudorandom number */ @@ -107,16 +107,16 @@ interface NullaryFunction extends PRNG { } /** -* Interface for generating pseudorandom integers on the interval `[1, 2147483646]`. +* Interface for generating pseudorandom integers on the interval `[0, 4294967295]`. */ interface Random extends PRNG { /** - * Returns a pseudorandom integer on the interval `[1, 2147483646]`. + * Returns a pseudorandom integer on the interval `[0, 4294967295]`. * * ## Notes * - * - This pseudorandom number generator (PRNG) is a linear congruential pseudorandom number generator (LCG) based on Park and Miller. - * - The generator has a period of approximately `2.1e9`. + * - This pseudorandom number generator (PRNG) is a permuted congruential pseudorandom number generator (PCG) based on O’Neill. + * - The generator has a period of `2^64`. * - An LCG is fast and uses little memory. On the other hand, because the generator is a simple LCG, the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. * * @returns pseudorandom number @@ -139,7 +139,7 @@ interface Random extends PRNG { normalized(): number; /** - * Returns a linear congruential pseudorandom number generator (LCG). + * Returns a permuted congruential pseudorandom number generator (PCG). * * @param options - function options * @param options.seed - pseudorandom number generator seed @@ -164,12 +164,12 @@ interface Random extends PRNG { } /** -* Returns a pseudorandom integer on the interval `[1, 2147483646]`. +* Returns a pseudorandom integer on the interval `[0, 4294967295]`. * * ## Notes * -* - This pseudorandom number generator (PRNG) is a linear congruential pseudorandom number generator (LCG) based on Park and Miller. -* - The generator has a period of approximately `2.1e9`. +* - This pseudorandom number generator (PRNG) is a permuted congruential pseudorandom number generator (PCG) based on O’Neill. +* - The generator has a period of `2^64`. * - An LCG is fast and uses little memory. On the other hand, because the generator is a simple LCG, the generator has recognized shortcomings. By today's PRNG standards, the generator's period is relatively short. More importantly, the "randomness quality" of the generator's output is lacking. These defects make the generator unsuitable, for example, in Monte Carlo simulations and in cryptographic applications. * * @returns pseudorandom number diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js index c09da08457da..430e2098d1cb 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js @@ -115,7 +115,7 @@ function verifyState( state, FLG ) { // MAIN // /** -* Returns a linear congruential pseudorandom number generator (LCG) based on Park and Miller. +* Returns a permuted congruential pseudorandom number generator (PCG) based on O’Neill. * * @param {Options} [options] - options * @param {PRNGSeedPCG32} [options.seed] - pseudorandom number generator seed @@ -406,7 +406,7 @@ function factory( options ) { } /** - * Generates a pseudorandom integer on the interval \\( [1,2^{32}) \\). + * Generates a pseudorandom integer on the interval \\( [0,2^{32}) \\). * * @private * @returns {uinteger32} pseudorandom integer diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js index bd32b00ba98a..e1215a82786d 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js @@ -19,7 +19,7 @@ 'use strict'; /** -* A linear congruential pseudorandom number generator (LCG) based on Park and Miller. +* A permuted congruential pseudorandom number generator (PCG) based on O’Neill. * * @module @stdlib/random/base/pcg32 * diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js index 52e288dcb71f..d35ebd032f6e 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js @@ -27,62 +27,54 @@ var randuint32 = require( './rand_uint32.js' ); // MAIN // /** -* Generates a pseudorandom integer on the interval \\( [1,2^{31}-1) \\). +* Generates a pseudorandom integer on the interval \\( [0, 2^{32}) \\) using the PCG32 XSH-RR generator. * * ## Method * -* Linear congruential generators (LCGs) use the recurrence relation +* PCG (Permuted Congruential Generator) combines a traditional linear congruential generator (LCG) with an output permutation to improve statistical quality. +* +* The internal state is advanced using the LCG recurrence: * * ```tex -* X_{n+1} = ( a \cdot X_n + c ) \operatorname{mod}(m) +* X_{n+1} = a \cdot X_n + c \mod 2^{64} * ``` * -* where the modulus \\( m \\) is a prime number or power of a prime number and \\( a \\) is a primitive root modulo \\( m \\). -* -* -* -* For an LCG to be a Lehmer RNG, the seed \\( X_0 \\) must be coprime to \\( m \\). -* -* -* -* In this implementation, the constants \\( a \\), \\( c \\), and \\( m \\) have the values +* where: * * ```tex * \begin{align*} -* a &= 7^5 = 16807 \\ -* c &= 0 \\ -* m &= 2^{31} - 1 = 2147483647 +* a &= 6364136223846793005 \\ +* c &= \text{user-defined increment (must be odd)} * \end{align*} * ``` * +* The output function applies a permutation (XSH-RR variant), which extracts and rotates bits to decorrelate output from internal state: +* +* ```tex +* \text{output} = \operatorname{rotr}\left((x \gg ((x \gg 59) + 5)) \bmod 2^{32}, x \gg 27\right) +* ``` +* * * -* The constant \\( m \\) is a Mersenne prime (modulo \\(31\\)). +* Unlike raw LCGs, PCG’s output permutation helps avoid patterns in the lower bits and produces statistically robust outputs. * * * * * -* The constant \\( a \\) is a primitive root (modulo \\(31\\)). +* The internal LCG uses 64-bit arithmetic, but the output is a 32-bit integer. * * * -* Accordingly, the maximum possible product is -* -* ```tex -* 16807 \cdot (m - 1) \approx 2^{46} -* ``` -* -* The values for \\( a \\), \\( c \\), and \\( m \\) are taken from Park and Miller, "Random Number Generators: Good Ones Are Hard To Find". Park's and Miller's article is also the basis for a recipe in the second edition of _Numerical Recipes in C_. +* The constants and permutation functions are based on O’Neill's 2014 technical report, _"PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation"_. * * ## Notes * -* - The generator has a period of approximately \\(2.1\mbox{e}9\\) (see [Numerical Recipes in C, 2nd Edition](#references), p. 279). +* - The generator has a period of \\( 2^{64} \\). * * ## References * -* - Park, S. K., and K. W. Miller. 1988. "Random Number Generators: Good Ones Are Hard to Find." _Communications of the ACM_ 31 (10). New York, NY, USA: ACM: 1192–1201. doi:[10.1145/63039.63042](http://dx.doi.org/10.1145/63039.63042). -* - Press, William H., Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling. 1992. _Numerical Recipes in C: The Art of Scientific Computing, Second Edition_. Cambridge University Press. +* - O’Neill, Melissa E. 2014. “PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation.” Technical Report HMC-CS-2014-0905. Harvey Mudd College, Claremont, CA. [https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf](https://www.cs.hmc.edu/tr/hmc-cs-2014-0905.pdf). * * @function pcg32 * @type {PRNG} diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js index dce9581b3159..328a2f8b6df7 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js @@ -26,13 +26,13 @@ var floor = require( '@stdlib/math/base/special/floor' ); // VARIABLES // -var MAX = UINT32_MAX - 1; +var MAX = UINT32_MAX + 1; // MAIN // /** -* Returns a pseudorandom integer on the interval \\( [1, 2^{32}-1) \\). +* Returns a pseudorandom integer on the interval \\( [0, 2^{32}) \\). * * @private * @returns {PositiveInteger} pseudorandom integer @@ -42,7 +42,8 @@ var MAX = UINT32_MAX - 1; * // returns */ function randuint32() { - var v = floor( 1.0 + (MAX*Math.random()) ); // eslint-disable-line stdlib/no-builtin-math + // #TODO: Don't use floor, >>> can suffice + var v = floor( MAX * Math.random() ); // eslint-disable-line stdlib/no-builtin-math return v >>> 0; // asm type annotation } diff --git a/lib/node_modules/@stdlib/random/base/pcg32/package.json b/lib/node_modules/@stdlib/random/base/pcg32/package.json index 364c5e1d97d6..5c8b35c6a47a 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/package.json +++ b/lib/node_modules/@stdlib/random/base/pcg32/package.json @@ -1,7 +1,7 @@ { "name": "@stdlib/random/base/pcg32", "version": "0.0.0", - "description": "A linear congruential pseudorandom number generator (LCG) based on Park and Miller.", + "description": "A permuted congruential pseudorandom number generator (PCG) based on O’Neill.", "license": "Apache-2.0", "author": { "name": "The Stdlib Authors", @@ -66,11 +66,9 @@ "uniform", "generator", "lcg", + "pcg", "pcg32", - "lehmer", - "park", - "miller", - "park-miller", + "oneill", "seed", "seedable" ] diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js index dacf020b0058..3010ef45154b 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js @@ -40,7 +40,7 @@ tape( 'attached to the main export is a method to generate normalized pseudorand t.end(); }); -tape( 'attached to the main export is a method to generate linear congruential pseudorandom number generator', function test( t ) { +tape( 'attached to the main export is a method to generate permuted congruential pseudorandom number generator', function test( t ) { t.equal( typeof pcg32.factory, 'function', 'has method' ); t.end(); }); From 604b99bf6ddc2a7cf48bedceb7cf01eb38560c8f Mon Sep 17 00:00:00 2001 From: impawstarlight Date: Sat, 5 Apr 2025 00:32:01 +0600 Subject: [PATCH 9/9] chore: update copyright years --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/random/base/pcg32/README.md | 2 +- .../@stdlib/random/base/pcg32/benchmark/benchmark.factory.js | 2 +- .../@stdlib/random/base/pcg32/benchmark/benchmark.js | 2 +- .../@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js | 2 +- lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile | 2 +- .../@stdlib/random/base/pcg32/benchmark/c/benchmark.c | 2 +- .../@stdlib/random/base/pcg32/docs/types/index.d.ts | 2 +- lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts | 2 +- lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile | 2 +- lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c | 2 +- lib/node_modules/@stdlib/random/base/pcg32/examples/index.js | 2 +- .../random/base/pcg32/include/stdlib/random/base/pcg32.h | 2 +- lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js | 2 +- lib/node_modules/@stdlib/random/base/pcg32/lib/index.js | 2 +- lib/node_modules/@stdlib/random/base/pcg32/lib/main.js | 2 +- lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js | 2 +- lib/node_modules/@stdlib/random/base/pcg32/src/main.c | 2 +- lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js | 2 +- lib/node_modules/@stdlib/random/base/pcg32/test/test.js | 2 +- 19 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/node_modules/@stdlib/random/base/pcg32/README.md b/lib/node_modules/@stdlib/random/base/pcg32/README.md index d37825e46245..fe241a9dd96e 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/README.md +++ b/lib/node_modules/@stdlib/random/base/pcg32/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2018 The Stdlib Authors. +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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js index fe10a75de8db..3f5f69868d1d 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.factory.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js index 6d0008e61766..3bbf2c2fec08 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js index d3124e9b9ec6..68e6d79d9f68 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/benchmark.to_json.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile index c12ea1918c97..5d7e79f50788 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/Makefile @@ -1,7 +1,7 @@ #/ # @license Apache-2.0 # -# Copyright (c) 2018 The Stdlib Authors. +# 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c index 6b08e11648ec..ff6df75d9e47 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/benchmark/c/benchmark.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts index b2dc4e45a6be..bba1e132005d 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/index.d.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2019 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts index 382958cac526..8f35ecbdedcb 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts +++ b/lib/node_modules/@stdlib/random/base/pcg32/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2019 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile index 95c166477a67..25ced822f96a 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/Makefile @@ -1,7 +1,7 @@ #/ # @license Apache-2.0 # -# Copyright (c) 2018 The Stdlib Authors. +# 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c index b3f08984d570..dbcca31c1a00 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/c/example.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js b/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js index 26435e61e150..7ecae0eb26c2 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h index 154ea3cd7352..e858f24e1e4b 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h +++ b/lib/node_modules/@stdlib/random/base/pcg32/include/stdlib/random/base/pcg32.h @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js index 430e2098d1cb..129b7dcd16af 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/factory.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js index e1215a82786d..748b364d7ddd 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js index d35ebd032f6e..487223a55a5d 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js index 328a2f8b6df7..5de1ac0f4615 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/lib/rand_uint32.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/src/main.c b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c index bb871df27e9f..b3add1cef75a 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/src/main.c +++ b/lib/node_modules/@stdlib/random/base/pcg32/src/main.c @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js index 4950d6d19452..31558b222bbf 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.factory.js @@ -3,7 +3,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js index 3010ef45154b..13f99481ac2b 100644 --- a/lib/node_modules/@stdlib/random/base/pcg32/test/test.js +++ b/lib/node_modules/@stdlib/random/base/pcg32/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2018 The Stdlib Authors. +* 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.