<commit_before>/**
* @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 setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isObject = require( '@stdlib/assert/is-plain-object' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var poisson = require( '@stdlib/random/base/poisson' ).factory;
var minstd = require( '@stdlib/random/base/minstd' ).factory;
var gamma = require( '@stdlib/random/base/gamma' ).factory;
var validate = require( './validate.js' );
var negativeBinomial0 = require( './negative_binomial.js' );


// MAIN //

/**
* Returns a pseudorandom number generator for generating negative binomial distributed random numbers.
*
* @param {PositiveNumber} [r] - number of successes until experiment is stopped
* @param {number} [p] - success probability
* @param {Options} [options] - function options
* @param {*} [options.seed] - pseudorandom number generator seed
* @throws {TypeError} `r` must be a positive number
* @throws {TypeError} `p` must be number
* @throws {RangeError} `p` must be a number on the interval `(0,1)`
* @throws {TypeError} options argument must be an object
* @returns {Function} pseudorandom number generator
*
* @example
* var negativeBinomial = factory( 10, 0.5 );
* var v = negativeBinomial();
* // returns <number>
*
* @example
* var negativeBinomial = factory( 10, 0.8, {
*     'seed': 297
* });
* var v = negativeBinomial();
* // returns <number>
*
* @example
* var negativeBinomial = factory();
* var v = negativeBinomial( 10, 0.5 );
* // returns <number>
*/
function factory() {
	var rgamma;
	var randi;
	var rpois;
	var opts;
	var prng;
	var err;
	var p;
	var r;

	if ( arguments.length === 1 ) {
		opts = arguments[ 0 ];
		if ( !isObject( opts ) ) {
			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
		}
	} else if ( arguments.length > 1 ) {
		r = arguments[ 0 ];
		p = arguments[ 1 ];
		err = validate( r, p );
		if ( err ) {
			throw err;
		}
		if ( arguments.length > 2 ) {
			opts = arguments[ 2 ];
			if ( !isObject( opts ) ) {
				throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
			}
		}
	}
	if ( opts && hasOwnProp( opts, 'seed' ) ) {
		randi = minstd({
			'seed': opts.seed
		});
	} else {
		randi = minstd();
	}
	rpois = poisson({
		'seed': randi()
	});
	rgamma = gamma({
		'seed': randi()
	});
	if ( r === void 0 ) {
		prng = negativeBinomial2;
	} else {
		prng = negativeBinomial1;
	}
	setReadOnly( prng, 'NAME', 'negative-binomial' );
	setReadOnly( prng, 'seed', randi.seed );
	setReadOnly( prng, 'PRNG', randi );

	return prng;

	/**
	* Returns a pseudorandom number drawn from a negative binomial distribution with bound parameters `r` and `p`.
	*
	* @private
	* @returns {NonNegativeInteger} pseudorandom number
	*<commit_msg>Add support for getting and setting PRNG state<commit_after>/**
* @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 setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-read-only-accessor' );
var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isObject = require( '@stdlib/assert/is-plain-object' );
var isUint32Array = require( '@stdlib/assert/is-uint32array' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var poisson = require( '@stdlib/random/base/poisson' ).factory;
var gamma = require( '@stdlib/random/base/gamma' ).factory;
var gcopy = require( '@stdlib/blas/base/gcopy' );
var Uint32Array = require( '@stdlib/array/uint32' );
var copy = require( '@stdlib/utils/copy' );
var validate = require( './validate.js' );
var negativeBinomial0 = require( './negative_binomial.js' );


// MAIN //

/**
* Returns a pseudorandom number generator for generating negative binomial distributed random numbers.
*
* @param {PositiveNumber} [r] - number of successes until experiment is stopped
* @param {number} [p] - success probability
* @param {Options} [options] - function options
* @param {(uinteger32|Collection<uinteger32>)} [options.seed] - pseudorandom number generator seed
* @param {Uint32Array} [options.state] - pseudorandom number generator state
* @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
* @throws {TypeError} `r` must be a positive number
* @throws {TypeError} `p` must be number
* @throws {RangeError} `p` must be a number on the interval `(0,1)`
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @throws {Error} must provide a valid state
* @returns {Function} pseudorandom number generator
*
* @example
* var negativeBinomial = factory( 10, 0.5 );
* var v = negativeBinomial();
* // returns <number>
*
* @example
* var negativeBinomial = factory( 10, 0.8, {
*     'seed': 297
* });
* var v = negativeBinomial();
* // returns <number>
*
* @example
* var negativeBinomial = factory();
* var v = negativeBinomial( 10, 0.5 );
* // returns <number>
*/
function factory() {
	var rgamma;
	var STATE;
	var rpois;
	var opts;
	var prng;
	var rand;
	var FLG;
	var err;
	var p;
	var r;

	FLG = true;
	if ( arguments.length === 0 ) {
		opts = {
			'copy': false
		};
		rpois = poisson( opts );
	} else if ( arguments.length === 1 ) {
		opts = arguments[ 0 ];
		if ( !isObject( opts ) ) {
			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
		}
		if ( hasOwnProp( opts, 'copy' ) && !isBoolean( opts.copy ) ) {
			throw new TypeError( 'invalid option. `copy` option must be a boolean. Option: `' + opts.copy + '`.' );
		}
		if ( hasOwnProp( opts, 'state' ) && !isUint32Array( opts.state ) ) {
			throw new TypeError( 'invalid option. `state` option must be a Uint32Array. Option: `' + opts.state + '`.' );
		}
		opts = copy( opts, 1 );
		if ( opts.copy === false ) {
			FLG = false;
		} else if ( opts.state ) {
			opts.state = gcopy( opts.state.length, opts.state, 1, new Uint32Array( opts.state.length ), 1 ); // eslint-disable-line max-len
		}
		opts.copy = false;
		rpois = poisson( opts );
	} else {
		r = arguments[ 0 ];
		p = arguments[ 1 ];
		err = validate( r, p );
		if ( err ) {
			throw err;
		}
		if ( arguments.length > 2 ) {
			opts = arguments[ 2 ];
			if ( !isObject( opts ) ) {
				throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + opts + '`.' );
			}
			if ( hasOwnProp( opts, 'copy' ) && !isBoolean( opts.copy ) ) {
				throw new TypeError( 'invalid option. `copy` option must be a boolean. Option: `' + opts.copy + '`.' );
			}
			if ( hasOwnProp( opts, 'state' ) && !isUint32Array( opts.state ) ) {
				throw new TypeError( 'invalid option. `state` option must be a Uint32Array. Option: `' + opts.state + '`.' );
			}
			opts = copy( opts, 1 );
			if ( opts.copy === false ) {
				FLG = false;
			} else if ( opts.state ) {
				opts.state = gcopy( opts.state.length, opts.state, 1, new Uint32Array( opts.state.length ), 1 ); // eslint-disable-line max-len
			}
			opts.copy = false;
			rpois = poisson( opts );
		} else {
			opts = {
				'copy': false
			};
			rpois = poisson( opts );
		}
	}
	if ( opts.state ) {
		STATE = opts.state;
	} else {
		STATE = rpois.state;
		rpois.state = STATE; // updates the underlying PRNG to point to a shared state
	}
	rgamma = gamma({
		'state': STATE,
		'copy': false
	});

	rand = rpois.PRNG;
	if ( r === void 0 ) {
		prng = negativeBinomial2;
	} else {
		prng = negativeBinomial1;
	}
	setReadOnly( prng, 'NAME', 'negative-binomial' );
	setReadOnlyAccessor( prng, 'seed', getSeed );
	setReadOnlyAccessor( prng, 'seedLength', getSeedLength );
	setReadWriteAccessor( prng, 'state', getState, setState );
	setReadOnlyAccessor( prng, 'stateLength', getStateLength );
	setReadOnlyAccessor( prng, 'byteLength', getStateSize );
	setReadOnly( prng, 'PRNG', rand );

	return prng;

	/**
	* Returns the PRNG seed.
	*
	* @private
	* @returns {Uint32Array} seed
	*/
	function getSeed() {
		return rand.seed;
	}

	/**
	* Returns the PRNG seed length.
	*
	* @private
	* @returns {PositiveInteger} seed length
	*/
	function getSeedLength() {
		return rand.seedLength;
	}

	/**
	* Returns the PRNG state length.
	*
	* @private
	* @returns {PositiveInteger} state length
	*/
	function getStateLength() {
		return rand.stateLength;
	}

	/**
	* Returns the PRNG state size (in bytes).
	*
	* @private
	* @returns {PositiveInteger} state size (in bytes)
	*/
	function getStateSize() {
		return rand.byteLength;
	}

	/**
	* Returns the current pseudorandom number generator state.
	*
	* @private
	* @returns {Uint32Array} current state
	*/
	function getState() {
		return rand.state;
	}

	/**
	* Sets the pseudorandom number generator state.
	*
	* @private
	* @param {Uint32Array} s - generator state
	* @throws {TypeError} must provide a `Uint32Array`
	* @throws {Error} must provide a valid state
	*/
	function setState( s ) {
		if ( !isUint32Array( s ) ) {
			throw new TypeError( 'invalid argument. Must provide a Uint32Array. Value: `' + s + '`.' );
		}
		if ( FLG ) {
			s = gcopy( s.length, s, 1, new Uint32Array( s.length ), 1 );
		}
		rand.state = s;
	}

	/**
	* Returns a pseudorandom number drawn from a negative binomial distribution with bound parameters `r` and `p`.
	*
	* @private
	* @returns {NonNegativeInteger} pseudorandom number
	*
{'fraction_non_alphanumeric': 0.1437738305589138, 'fraction_numerical': 0.01153166558169813, 'mean_word_length': 3.5800681431005112, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5326432', 'n_tokens_mistral': 3812, 'n_tokens_neox': 3526, 'n_words': 1141}