Skip to content

Commit f0b81a1

Browse files
committed
Rename files and add support for getting and setting PRNG state
1 parent edc64d1 commit f0b81a1

File tree

4 files changed

+141
-69
lines changed

4 files changed

+141
-69
lines changed

lib/node_modules/@stdlib/random/base/hypergeometric/lib/factory.js

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
// MODULES //
2222

2323
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
24+
var setReadOnlyAccessor = require( '@stdlib/utils/define-read-only-accessor' );
25+
var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
2426
var isObject = require( '@stdlib/assert/is-plain-object' );
2527
var randu = require( '@stdlib/random/base/mt19937' ).factory;
2628
var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' );
2729
var PINF = require( '@stdlib/constants/math/float64-pinf' );
2830
var validate = require( './validate.js' );
29-
var hypergeometric0 = require( './_hypergeometric.js' );
31+
var hypergeometric0 = require( './hypergeometric.js' );
3032

3133

3234
// MAIN //
@@ -38,12 +40,16 @@ var hypergeometric0 = require( './_hypergeometric.js' );
3840
* @param {NonNegativeInteger} [K] - subpopulation size
3941
* @param {NonNegativeInteger} [n] - number of draws
4042
* @param {Options} [options] - function options
41-
* @param {*} [options.seed] - pseudorandom number generator seed
43+
* @param {(uinteger32|Collection<uinteger32>)} [options.seed] - pseudorandom number generator seed
44+
* @param {Uint32Array} [options.state] - pseudorandom number generator state
45+
* @param {boolean} [options.copy=true] - boolean indicating whether to copy a provided pseudorandom number generator state
4246
* @throws {TypeError} `N` must be a nonnegative integer
4347
* @throws {TypeError} `K` must be a nonnegative integer
4448
* @throws {TypeError} `n` must be a nonnegative integer
4549
* @throws {RangeError} `n` must be less than or equal to `N`
4650
* @throws {TypeError} options argument must be an object
51+
* @throws {TypeError} must provide valid options
52+
* @throws {Error} must provide a valid state
4753
* @returns {Function} pseudorandom number generator
4854
*
4955
* @example
@@ -106,11 +112,77 @@ function factory() {
106112
prng = hypergeometric1;
107113
}
108114
setReadOnly( prng, 'NAME', 'hypergeometric' );
109-
setReadOnly( prng, 'seed', rand.seed );
115+
setReadOnlyAccessor( prng, 'seed', getSeed );
116+
setReadOnlyAccessor( prng, 'seedLength', getSeedLength );
117+
setReadWriteAccessor( prng, 'state', getState, setState );
118+
setReadOnlyAccessor( prng, 'stateLength', getStateLength );
119+
setReadOnlyAccessor( prng, 'byteLength', getStateSize );
110120
setReadOnly( prng, 'PRNG', rand );
111121

112122
return prng;
113123

124+
/**
125+
* Returns the PRNG seed.
126+
*
127+
* @private
128+
* @returns {Uint32Array} seed
129+
*/
130+
function getSeed() {
131+
return rand.seed;
132+
}
133+
134+
/**
135+
* Returns the PRNG seed length.
136+
*
137+
* @private
138+
* @returns {PositiveInteger} seed length
139+
*/
140+
function getSeedLength() {
141+
return rand.seedLength;
142+
}
143+
144+
/**
145+
* Returns the PRNG state length.
146+
*
147+
* @private
148+
* @returns {PositiveInteger} state length
149+
*/
150+
function getStateLength() {
151+
return rand.stateLength;
152+
}
153+
154+
/**
155+
* Returns the PRNG state size (in bytes).
156+
*
157+
* @private
158+
* @returns {PositiveInteger} state size (in bytes)
159+
*/
160+
function getStateSize() {
161+
return rand.byteLength;
162+
}
163+
164+
/**
165+
* Returns the current pseudorandom number generator state.
166+
*
167+
* @private
168+
* @returns {Uint32Array} current state
169+
*/
170+
function getState() {
171+
return rand.state;
172+
}
173+
174+
/**
175+
* Sets the pseudorandom number generator state.
176+
*
177+
* @private
178+
* @param {Uint32Array} s - generator state
179+
* @throws {TypeError} must provide a `Uint32Array`
180+
* @throws {Error} must provide a valid state
181+
*/
182+
function setState( s ) {
183+
rand.state = s;
184+
}
185+
114186
/**
115187
* Returns a random number drawn from a hypergeometric distribution with bound parameters.
116188
*

lib/node_modules/@stdlib/random/base/hypergeometric/lib/hypergeometric.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,59 @@
2020

2121
// MODULES //
2222

23-
var factory = require( './factory.js' );
23+
var hin = require( './hin.js' );
2424

2525

2626
// MAIN //
2727

2828
/**
2929
* Returns a pseudorandom number drawn from a hypergeometric distribution.
3030
*
31-
* @name hypergeometric
32-
* @type {Function}
33-
* @param {NonNegativeInteger} N - population size
34-
* @param {NonNegativeInteger} K - subpopulation size
35-
* @param {NonNegativeInteger} n - number of draws
36-
* @returns {NonNegativeInteger} pseudorandom number
37-
*
38-
* @example
39-
* var v = hypergeometric( 5, 3, 2 );
40-
* // returns <number>
31+
* ## References
4132
*
42-
* @example
43-
* var v = hypergeometric( -5, 3, 2 );
44-
* // returns NaN
33+
* - Kachitvichyanukul, Voratas., and Burce Schmeiser. 1985. "Computer generation of hypergeometric random variates." _Journal of Statistical Computation and Simulation_ 22 (2): 127–45. doi:[10.1080/00949658508810839][@kachitvichyanukul:1985].
4534
*
46-
* @example
47-
* var v = hypergeometric( 5, 3.14, 2 );
48-
* // returns NaN
35+
* [@kachitvichyanukul:1985]: http://dx.doi.org/10.1080/00949658508810839
4936
*
50-
* @example
51-
* var v = hypergeometric( 5, 3, 1.5 );
52-
* // returns NaN
5337
*
54-
* @example
55-
* var v = hypergeometric( NaN, NaN, NaN );
56-
* // returns NaN
38+
* @private
39+
* @param {Function} rand - PRNG for uniformly distributed number
40+
* @param {NonNegativeInteger} N - population size
41+
* @param {NonNegativeInteger} K - subpopulation size
42+
* @param {NonNegativeInteger} n - number of draws
43+
* @returns {NonNegativeInteger} pseudorandom number
5744
*/
58-
var hypergeometric = factory();
45+
function hypergeometric( rand, N, K, n ) {
46+
var n1;
47+
var n2;
48+
var k;
49+
var x;
50+
51+
if ( n > N/2 ) {
52+
k = N - n;
53+
if ( 2*K <= N ) {
54+
n1 = K;
55+
n2 = N - K;
56+
x = hin( rand, n1, n2, k );
57+
return K - x;
58+
}
59+
n2 = K;
60+
n1 = N - K;
61+
x = hin( rand, n1, n2, k );
62+
return n - N + K + x;
63+
}
64+
k = n;
65+
if ( 2*K <= N ) {
66+
n1 = K;
67+
n2 = N - K;
68+
x = hin( rand, n1, n2, k );
69+
return x;
70+
}
71+
n1 = N - K;
72+
n2 = K;
73+
x = hin( rand, n1, n2, k );
74+
return n - x;
75+
}
5976

6077

6178
// EXPORTS //

lib/node_modules/@stdlib/random/base/hypergeometric/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
// MODULES //
5959

6060
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
61-
var hypergeometric = require( './hypergeometric.js' );
61+
var hypergeometric = require( './main.js' );
6262
var factory = require( './factory.js' );
6363

6464

lib/node_modules/@stdlib/random/base/hypergeometric/lib/_hypergeometric.js renamed to lib/node_modules/@stdlib/random/base/hypergeometric/lib/main.js

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,42 @@
2020

2121
// MODULES //
2222

23-
var hin = require( './hin.js' );
23+
var factory = require( './factory.js' );
2424

2525

2626
// MAIN //
2727

2828
/**
2929
* Returns a pseudorandom number drawn from a hypergeometric distribution.
3030
*
31-
* ## References
32-
*
33-
* - Kachitvichyanukul, Voratas., and Burce Schmeiser. 1985. "Computer generation of hypergeometric random variates." _Journal of Statistical Computation and Simulation_ 22 (2): 127–45. doi:[10.1080/00949658508810839][@kachitvichyanukul:1985].
34-
*
35-
* [@kachitvichyanukul:1985]: http://dx.doi.org/10.1080/00949658508810839
36-
*
37-
*
38-
* @private
39-
* @param {Function} rand - PRNG for uniformly distributed number
31+
* @name hypergeometric
32+
* @type {Function}
4033
* @param {NonNegativeInteger} N - population size
4134
* @param {NonNegativeInteger} K - subpopulation size
4235
* @param {NonNegativeInteger} n - number of draws
4336
* @returns {NonNegativeInteger} pseudorandom number
37+
*
38+
* @example
39+
* var v = hypergeometric( 5, 3, 2 );
40+
* // returns <number>
41+
*
42+
* @example
43+
* var v = hypergeometric( -5, 3, 2 );
44+
* // returns NaN
45+
*
46+
* @example
47+
* var v = hypergeometric( 5, 3.14, 2 );
48+
* // returns NaN
49+
*
50+
* @example
51+
* var v = hypergeometric( 5, 3, 1.5 );
52+
* // returns NaN
53+
*
54+
* @example
55+
* var v = hypergeometric( NaN, NaN, NaN );
56+
* // returns NaN
4457
*/
45-
function hypergeometric( rand, N, K, n ) {
46-
var n1;
47-
var n2;
48-
var k;
49-
var x;
50-
51-
if ( n > N/2 ) {
52-
k = N - n;
53-
if ( 2*K <= N ) {
54-
n1 = K;
55-
n2 = N - K;
56-
x = hin( rand, n1, n2, k );
57-
return K - x;
58-
}
59-
n2 = K;
60-
n1 = N - K;
61-
x = hin( rand, n1, n2, k );
62-
return n - N + K + x;
63-
}
64-
k = n;
65-
if ( 2*K <= N ) {
66-
n1 = K;
67-
n2 = N - K;
68-
x = hin( rand, n1, n2, k );
69-
return x;
70-
}
71-
n1 = N - K;
72-
n2 = K;
73-
x = hin( rand, n1, n2, k );
74-
return n - x;
75-
}
58+
var hypergeometric = factory();
7659

7760

7861
// EXPORTS //

0 commit comments

Comments
 (0)