text: /**
  * @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 isProbability = require( '@stdlib/assert/is-probability' ).isPrimitive;
  var isObject = require( '@stdlib/assert/is-plain-object' );
  var isPositiveNumber = require( '@stdlib/assert/is-positive-number' ).isPrimitive;
  var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
  var hasOwnProp = require( '@stdlib/assert/has-own-property' );
  var MAX_VALUE = require( '@stdlib/constants/math/float64-max' );
  var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
  var rnbinom = require( '@stdlib/random/base/negative-binomial' ).factory;
  var iteratorSymbol = require( '@stdlib/symbol/iterator' );
  
  
  // MAIN //
  
  /**
  * Returns an iterator for generating pseudorandom numbers drawn from a negative binomial distribution.
  *
  * @param {PositiveNumber} r - number of successes until experiment is stopped
  * @param {Probability} p - success probability
  * @param {Options} [options] - function options
  * @param {*} [options.seed] - pseudorandom number generator seed
  * @param {NonNegativeInteger} [options.iter] - number of iterations
  * @throws {TypeError} first argument must be a positive number
  * @throws {TypeError} second argument must be a probability
  * @throws {TypeError} options must be an object
  * @throws {TypeError} must provide valid options
  * @returns {Iterator} iterator
  *
  * @example
  * var iter = iterator( 8, 0.3 );
  *
  * var r = iter.next().value;
  * // returns <number>
  *
  * r = iter.next().value;
  * // returns <number>
  *
  * r = iter.next().value;
  * // returns <number>
  *
  * // ...
  */
  function iterator( r, p, options ) {
  	var niter;
  	var iter;
  	var rand;
  	var FLG;
  	var i;
  	if ( !isPositiveNumber( r ) ) {
  		throw new TypeError( 'invalid argument. First argument must be a positive integer. Value: `'+r+'`.' );
  	}
  	if ( !isProbability( p ) ) {
  		throw new TypeError( 'invalid argument. Second argument must be a probability. Value: `'+p+'`.' );
  	}
  	niter = MAX_VALUE;
  	if ( arguments.length > 2 ) {
  		if ( !isObject( options ) ) {
  			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `'+options+'`.' );
  		}
  		if ( hasOwnProp( options, 'iter' ) ) {
  			niter = options.iter;
  			if ( !isNonNegativeInteger( niter ) ) {
  				throw new TypeError( 'invalid option. `iter` option must be a nonnegative integer. Option: `'+niter+'`.' );
  			}
  		}
  		rand = rnbinom( r, p, options );
  	} else {
  		rand = rnbinom( r, p );
  	}
  	i = 0;
  
  	// Create an iterator protocol-compliant object:
  	iter = {};
  	setReadOnly( iter, 'next', next );
  	setReadOnly( iter, 'return', end );
  	setReadOnly( iter, 'seed', rand.seed );
  
  	// If an environment supports `Symbol.iterator`, make the iterator iterable:
  	if ( iteratorSymbol ) {
  		setReadOnly( iter, iteratorSymbol, factory );
  	}
  	return iter;
  
  	/**
  	* Returns an iterator protocol-compliant object containing the next iterated value.
  	*
  	* @private
  	* @returns {Object} iterator protocol-compliant object
  	*/
  	function next() {
  		i += 1;
  		if ( FLG || i > niter ) {
  			return {
  				'done': true
  			};
  		}
  		return {
  			'value': rand(),
  			'done': false
  		};
  	}
  
  	/**
  	* Finishes an iterator.
  	*
  	* @private
  	* @param {*} [value] - value to return
  	* @returns {Object} iterator protocol-compliant object
  	*/
  	function end( value ) {
  		FLG = true;
  		if ( arguments.length ) {
  			return {
  				'value': value,
  				'done': true
  			};
  		}
  		return {
  			'done': true
  		};
  	}
  
  	/**
  	* Returns a new iterator.
  	*
  	* @private
  	* @returns {Iterator} iterator
  	*/
  	function factory() {
  		return iterator( r, p, {
  			'seed': iter.seed,
  			'iter': niter
  		});
  	}
  }
  
  
  // EXPORTS //
  
  module.exports = iterator;
  