main.js (7167B)
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 constantFunction = require( '@stdlib/utils/constant-function' ); 27 var noop = require( '@stdlib/utils/noop' ); 28 var copy = require( '@stdlib/utils/copy' ); 29 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; 30 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 31 var isObject = require( '@stdlib/assert/is-plain-object' ); 32 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 33 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 34 var MAX_VALUE = require( '@stdlib/constants/float64/max' ); 35 var rtriangular = require( './../../../base/triangular' ).factory; 36 var iteratorSymbol = require( '@stdlib/symbol/iterator' ); 37 38 39 // MAIN // 40 41 /** 42 * Returns an iterator for generating pseudorandom numbers drawn from a triangular distribution. 43 * 44 * @param {number} a - minimum support 45 * @param {number} b - maximum support 46 * @param {number} c - mode 47 * @param {Options} [options] - function options 48 * @param {PRNG} [options.prng] - pseudorandom number generator which generates uniformly distributed pseudorandom numbers 49 * @param {PRNGSeedMT19937} [options.seed] - pseudorandom number generator seed 50 * @param {PRNGStateMT19937} [options.state] - pseudorandom number generator state 51 * @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state 52 * @param {NonNegativeInteger} [options.iter] - number of iterations 53 * @throws {TypeError} `a` must be a number 54 * @throws {TypeError} `b` must be a number 55 * @throws {RangeError} arguments must satisfy `a <= c <= b` 56 * @throws {TypeError} options argument must be an object 57 * @throws {TypeError} must provide valid options 58 * @throws {Error} must provide a valid state 59 * @returns {Iterator} iterator 60 * 61 * @example 62 * var iter = iterator( 2.0, 5.0, 3.0 ); 63 * 64 * var r = iter.next().value; 65 * // returns <number> 66 * 67 * r = iter.next().value; 68 * // returns <number> 69 * 70 * r = iter.next().value; 71 * // returns <number> 72 * 73 * // ... 74 */ 75 function iterator( a, b, c, options ) { 76 var opts; 77 var iter; 78 var rand; 79 var FLG; 80 var i; 81 if ( !isNumber( a ) || isnan( a ) ) { 82 throw new TypeError( 'invalid argument. First argument must be a number primitive and not `NaN`. Value: `'+a+'`.' ); 83 } 84 if ( !isNumber( b ) || isnan( b ) ) { 85 throw new TypeError( 'invalid argument. Second argument must be a number primitive and not `NaN`. Value: `'+b+'`.' ); 86 } 87 if ( !isNumber( c ) || isnan( c ) ) { 88 throw new TypeError( 'invalid argument. Third argument must be a number primitive and not `NaN`. Value: `'+c+'`.' ); 89 } 90 if ( !(a <= c && c <= b) ) { 91 throw new RangeError( 'invalid arguments. The condition `a <= c <= b` must be satisfied. Value: `['+a+','+b+','+c+']`.'); 92 } 93 if ( arguments.length > 3 ) { 94 if ( !isObject( options ) ) { 95 throw new TypeError( 'invalid argument. Options argument must be an object. Value: `'+options+'`.' ); 96 } 97 opts = copy( options, 1 ); 98 if ( hasOwnProp( opts, 'iter' ) ) { 99 if ( !isNonNegativeInteger( opts.iter ) ) { 100 throw new TypeError( 'invalid option. `iter` option must be a nonnegative integer. Option: `'+opts.iter+'`.' ); 101 } 102 } else { 103 opts.iter = MAX_VALUE; 104 } 105 rand = rtriangular( a, b, c, opts ); 106 if ( opts.prng === void 0 && opts.copy !== false ) { 107 opts.state = rand.state; // cache a copy of the PRNG state 108 } 109 } else { 110 rand = rtriangular( a, b, c ); 111 opts = { 112 'iter': MAX_VALUE, 113 'state': rand.state // cache a copy of the PRNG state 114 }; 115 } 116 i = 0; 117 118 // Create an iterator protocol-compliant object: 119 iter = {}; 120 setReadOnly( iter, 'next', next ); 121 setReadOnly( iter, 'return', end ); 122 123 if ( opts && opts.prng ) { 124 setReadOnly( iter, 'seed', null ); 125 setReadOnly( iter, 'seedLength', null ); 126 setReadWriteAccessor( iter, 'state', constantFunction( null ), noop ); 127 setReadOnly( iter, 'stateLength', null ); 128 setReadOnly( iter, 'byteLength', null ); 129 } else { 130 setReadOnlyAccessor( iter, 'seed', getSeed ); 131 setReadOnlyAccessor( iter, 'seedLength', getSeedLength ); 132 setReadWriteAccessor( iter, 'state', getState, setState ); 133 setReadOnlyAccessor( iter, 'stateLength', getStateLength ); 134 setReadOnlyAccessor( iter, 'byteLength', getStateSize ); 135 } 136 setReadOnly( iter, 'PRNG', rand.PRNG ); 137 138 // If an environment supports `Symbol.iterator`, make the iterator iterable: 139 if ( iteratorSymbol ) { 140 setReadOnly( iter, iteratorSymbol, factory ); 141 } 142 return iter; 143 144 /** 145 * Returns an iterator protocol-compliant object containing the next iterated value. 146 * 147 * @private 148 * @returns {Object} iterator protocol-compliant object 149 */ 150 function next() { 151 i += 1; 152 if ( FLG || i > opts.iter ) { 153 return { 154 'done': true 155 }; 156 } 157 return { 158 'value': rand(), 159 'done': false 160 }; 161 } 162 163 /** 164 * Finishes an iterator. 165 * 166 * @private 167 * @param {*} [value] - value to return 168 * @returns {Object} iterator protocol-compliant object 169 */ 170 function end( value ) { 171 FLG = true; 172 if ( arguments.length ) { 173 return { 174 'value': value, 175 'done': true 176 }; 177 } 178 return { 179 'done': true 180 }; 181 } 182 183 /** 184 * Returns a new iterator. 185 * 186 * @private 187 * @returns {Iterator} iterator 188 */ 189 function factory() { 190 return iterator( a, b, c, opts ); 191 } 192 193 /** 194 * Returns the PRNG seed. 195 * 196 * @private 197 * @returns {PRNGSeedMT19937} seed 198 */ 199 function getSeed() { 200 return rand.PRNG.seed; 201 } 202 203 /** 204 * Returns the PRNG seed length. 205 * 206 * @private 207 * @returns {PositiveInteger} seed length 208 */ 209 function getSeedLength() { 210 return rand.PRNG.seedLength; 211 } 212 213 /** 214 * Returns the PRNG state length. 215 * 216 * @private 217 * @returns {PositiveInteger} state length 218 */ 219 function getStateLength() { 220 return rand.PRNG.stateLength; 221 } 222 223 /** 224 * Returns the PRNG state size (in bytes). 225 * 226 * @private 227 * @returns {PositiveInteger} state size (in bytes) 228 */ 229 function getStateSize() { 230 return rand.PRNG.byteLength; 231 } 232 233 /** 234 * Returns the current pseudorandom number generator state. 235 * 236 * @private 237 * @returns {PRNGStateMT19937} current state 238 */ 239 function getState() { 240 return rand.PRNG.state; 241 } 242 243 /** 244 * Sets the pseudorandom number generator state. 245 * 246 * @private 247 * @param {PRNGStateMT19937} s - generator state 248 * @throws {Error} must provide a valid state 249 */ 250 function setState( s ) { 251 rand.PRNG.state = s; 252 } 253 } 254 255 256 // EXPORTS // 257 258 module.exports = iterator;