time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

factory.js (7347B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     24 var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
     25 var setReadWriteAccessor = require( '@stdlib/utils/define-nonenumerable-read-write-accessor' );
     26 var isObject = require( '@stdlib/assert/is-plain-object' );
     27 var isFunction = require( '@stdlib/assert/is-function' );
     28 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     29 var constantFunction = require( '@stdlib/utils/constant-function' );
     30 var noop = require( '@stdlib/utils/noop' );
     31 var randu = require( './../../../base/mt19937' ).factory;
     32 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     33 var typedarray2json = require( '@stdlib/array/to-json' );
     34 var validate = require( './validate.js' );
     35 var arcsine0 = require( './arcsine.js' );
     36 
     37 
     38 // MAIN //
     39 
     40 /**
     41 * Returns a pseudorandom number generator for generating arcsine distributed random numbers.
     42 *
     43 * @param {number} [a] - minimum support
     44 * @param {number} [b] - maximum support
     45 * @param {Options} [options] - function options
     46 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers
     47 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed
     48 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state
     49 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
     50 * @throws {TypeError} `a` must be a number
     51 * @throws {TypeError} `b` must be a number
     52 * @throws {RangeError} `a` must be less than `b`
     53 * @throws {TypeError} options argument must be an object
     54 * @throws {TypeError} must provide valid options
     55 * @throws {Error} must provide a valid state
     56 * @returns {PRNG} pseudorandom number generator
     57 *
     58 * @example
     59 * var arcsine = factory( 0.0, 1.0 );
     60 *
     61 * var v = arcsine();
     62 * // returns <number>
     63 *
     64 * @example
     65 * var arcsine = factory( -3.0, -1.0, {
     66 *     'seed': 297
     67 * });
     68 * var v = arcsine();
     69 * // returns <number>
     70 */
     71 function factory() {
     72 	var opts;
     73 	var rand;
     74 	var prng;
     75 	var err;
     76 	var a;
     77 	var b;
     78 
     79 	if ( arguments.length === 0 ) {
     80 		rand = randu();
     81 	} else if ( arguments.length === 1 ) {
     82 		opts = arguments[ 0 ];
     83 		if ( !isObject( opts ) ) {
     84 			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `'+opts+'`.' );
     85 		}
     86 		if ( hasOwnProp( opts, 'prng' ) ) {
     87 			if ( !isFunction( opts.prng ) ) {
     88 				throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' );
     89 			}
     90 			rand = opts.prng;
     91 		} else {
     92 			rand = randu( opts );
     93 		}
     94 	} else {
     95 		a = arguments[ 0 ];
     96 		b = arguments[ 1 ];
     97 		err = validate( a, b );
     98 		if ( err ) {
     99 			throw err;
    100 		}
    101 		if ( arguments.length > 2 ) {
    102 			opts = arguments[ 2 ];
    103 			if ( !isObject( opts ) ) {
    104 				throw new TypeError( 'invalid argument. Options argument must be an object. Value: `'+opts+'`.' );
    105 			}
    106 			if ( hasOwnProp( opts, 'prng' ) ) {
    107 				if ( !isFunction( opts.prng ) ) {
    108 					throw new TypeError( 'invalid option. `prng` option must be a pseudorandom number generator function. Option: `' + opts.prng + '`.' );
    109 				}
    110 				rand = opts.prng;
    111 			} else {
    112 				rand = randu( opts );
    113 			}
    114 		} else {
    115 			rand = randu();
    116 		}
    117 	}
    118 	if ( a === void 0 ) {
    119 		prng = arcsine2;
    120 	} else {
    121 		prng = arcsine1;
    122 	}
    123 	setReadOnly( prng, 'NAME', 'arcsine' );
    124 
    125 	// If we are provided an "external" PRNG, we don't support getting or setting PRNG state, as we'd need to check for compatible state value types, etc, entailing considerable complexity.
    126 	if ( opts && opts.prng ) {
    127 		setReadOnly( prng, 'seed', null );
    128 		setReadOnly( prng, 'seedLength', null );
    129 		setReadWriteAccessor( prng, 'state', constantFunction( null ), noop );
    130 		setReadOnly( prng, 'stateLength', null );
    131 		setReadOnly( prng, 'byteLength', null );
    132 		setReadOnly( prng, 'toJSON', constantFunction( null ) );
    133 		setReadOnly( prng, 'PRNG', rand );
    134 	} else {
    135 		setReadOnlyAccessor( prng, 'seed', getSeed );
    136 		setReadOnlyAccessor( prng, 'seedLength', getSeedLength );
    137 		setReadWriteAccessor( prng, 'state', getState, setState );
    138 		setReadOnlyAccessor( prng, 'stateLength', getStateLength );
    139 		setReadOnlyAccessor( prng, 'byteLength', getStateSize );
    140 		setReadOnly( prng, 'toJSON', toJSON );
    141 		setReadOnly( prng, 'PRNG', rand );
    142 		rand = rand.normalized;
    143 	}
    144 	return prng;
    145 
    146 	/**
    147 	* Returns the PRNG seed.
    148 	*
    149 	* @private
    150 	* @returns {PRNGSeedMT19937} seed
    151 	*/
    152 	function getSeed() {
    153 		return rand.seed;
    154 	}
    155 
    156 	/**
    157 	* Returns the PRNG seed length.
    158 	*
    159 	* @private
    160 	* @returns {PositiveInteger} seed length
    161 	*/
    162 	function getSeedLength() {
    163 		return rand.seedLength;
    164 	}
    165 
    166 	/**
    167 	* Returns the PRNG state length.
    168 	*
    169 	* @private
    170 	* @returns {PositiveInteger} state length
    171 	*/
    172 	function getStateLength() {
    173 		return rand.stateLength;
    174 	}
    175 
    176 	/**
    177 	* Returns the PRNG state size (in bytes).
    178 	*
    179 	* @private
    180 	* @returns {PositiveInteger} state size (in bytes)
    181 	*/
    182 	function getStateSize() {
    183 		return rand.byteLength;
    184 	}
    185 
    186 	/**
    187 	* Returns the current pseudorandom number generator state.
    188 	*
    189 	* @private
    190 	* @returns {PRNGStateMT19937} current state
    191 	*/
    192 	function getState() {
    193 		return rand.state;
    194 	}
    195 
    196 	/**
    197 	* Sets the pseudorandom number generator state.
    198 	*
    199 	* @private
    200 	* @param {PRNGStateMT19937} s - generator state
    201 	* @throws {Error} must provide a valid state
    202 	*/
    203 	function setState( s ) {
    204 		rand.state = s;
    205 	}
    206 
    207 	/**
    208 	* Serializes the pseudorandom number generator as a JSON object.
    209 	*
    210 	* ## Notes
    211 	*
    212 	* -   `JSON.stringify()` implicitly calls this method when stringifying a PRNG.
    213 	*
    214 	* @private
    215 	* @returns {Object} JSON representation
    216 	*/
    217 	function toJSON() {
    218 		var out = {};
    219 		out.type = 'PRNG';
    220 		out.name = prng.NAME;
    221 		out.state = typedarray2json( rand.state );
    222 		if ( a === void 0 ) {
    223 			out.params = [];
    224 		} else {
    225 			out.params = [ a, b ];
    226 		}
    227 		return out;
    228 	}
    229 
    230 	/**
    231 	* Returns a arcsine distributed pseudorandom number with minimum support `a` and maximum support `b`.
    232 	*
    233 	* @private
    234 	* @returns {number} pseudorandom number
    235 	*
    236 	* @example
    237 	* var v = arcsine1();
    238 	* // returns <number>
    239 	*/
    240 	function arcsine1() {
    241 		return arcsine0( rand, a, b );
    242 	}
    243 
    244 	/**
    245 	* Returns an arcsine distributed pseudorandom number with minimum support `a` and maximum support `b`.
    246 	*
    247 	* @private
    248 	* @param {number} a - minimum support
    249 	* @param {number} b - maximum support
    250 	* @returns {number} pseudorandom number
    251 	*
    252 	* @example
    253 	* var v = arcsine2( 0.0, 1.0 );
    254 	* // returns <number>
    255 	*
    256 	* @example
    257 	* var v = arcsine2( 1.0, 0.0 );
    258 	* // returns NaN
    259 	*/
    260 	function arcsine2( a, b ) {
    261 		if (
    262 			isnan( a ) ||
    263 			isnan( b ) ||
    264 			a >= b
    265 		) {
    266 			return NaN;
    267 		}
    268 		return arcsine0( rand, a, b );
    269 	}
    270 }
    271 
    272 
    273 // EXPORTS //
    274 
    275 module.exports = factory;