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