Skip to content

Commit 62872f6

Browse files
committed
feat: add random/hypergeometric
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 30ca17e commit 62872f6

22 files changed

+5343
-0
lines changed
Lines changed: 393 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,393 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Hypergeometric Random Numbers
22+
23+
> Generate pseudorandom numbers drawn from a [hypergeometric][@stdlib/random/base/hypergeometric] distribution.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var hypergeometric = require( '@stdlib/random/hypergeometric' );
31+
```
32+
33+
#### hypergeometric( shape, N, K, n\[, options] )
34+
35+
Returns an [ndarray][@stdlib/ndarray/ctor] containing pseudorandom numbers drawn from a [hypergeometric][@stdlib/random/base/hypergeometric] distribution.
36+
37+
```javascript
38+
var arr = hypergeometric( [ 3, 3 ], 20, 10, 7 );
39+
// returns <ndarray>
40+
```
41+
42+
The function has the following parameters:
43+
44+
- **shape**: output shape.
45+
- **N**: population size. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output shape.
46+
- **K**: subpopulation size. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output shape.
47+
- **n**: number of draws. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output shape.
48+
- **options**: function options.
49+
50+
When provided scalar distribution parameters, every element in the output [ndarray][@stdlib/ndarray/ctor] is drawn from the same distribution. To generate pseudorandom numbers drawn from different distributions, provide distribution parameter arguments as [ndarrays][@stdlib/ndarray/ctor]. The following example demonstrates broadcasting an [ndarray][@stdlib/ndarray/ctor] containing distribution parameters to generate sub-matrices drawn from different distributions.
51+
52+
```javascript
53+
var getShape = require( '@stdlib/ndarray/shape' );
54+
var array = require( '@stdlib/ndarray/array' );
55+
56+
var N = array( [ [ [ 20 ] ], [ [ 50 ] ] ] );
57+
// returns <ndarray>
58+
59+
var K = array( [ [ [ 10 ] ], [ [ 20 ] ] ] );
60+
// returns <ndarray>
61+
62+
var n = array( [ [ [ 7 ] ], [ [ 10 ] ] ] );
63+
// returns <ndarray>
64+
65+
var shape = getShape( N );
66+
// returns [ 2, 1, 1 ]
67+
68+
var arr = hypergeometric( [ 2, 3, 3 ], N, K, n );
69+
// returns <ndarray>
70+
```
71+
72+
If provided an empty shape, the function returns a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
73+
74+
```javascript
75+
var getShape = require( '@stdlib/ndarray/shape' );
76+
77+
var arr = hypergeometric( [], 20, 10, 7 );
78+
// returns <ndarray>
79+
80+
var shape = getShape( arr );
81+
// returns []
82+
83+
var v = arr.get();
84+
// returns <number>
85+
```
86+
87+
The function accepts the following options:
88+
89+
- **dtype**: output ndarray data type. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
90+
- **order**: ndarray order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`.
91+
- **mode**: specifies how to handle indices which exceed ndarray dimensions. For a list of supported modes, see [`ndarray`][@stdlib/ndarray/ctor]. Default: `'throw'`.
92+
- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed ndarray dimensions. If provided fewer modes than dimensions, an [ndarray][@stdlib/ndarray/ctor] instance recycles modes using modulo arithmetic. Default: `[ options.mode ]`.
93+
- **readonly**: boolean indicating whether an ndarray should be **read-only**. Default: `false`.
94+
95+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
96+
97+
```javascript
98+
var getDType = require( '@stdlib/ndarray/dtype' );
99+
100+
var opts = {
101+
'dtype': 'generic'
102+
};
103+
104+
var arr = hypergeometric( [ 3, 3 ], 20, 10, 7, opts );
105+
// returns <ndarray>
106+
107+
var dt = String( getDType( arr ) );
108+
// returns 'generic'
109+
```
110+
111+
#### hypergeometric.assign( N, K, n, out )
112+
113+
Fills an [ndarray][@stdlib/ndarray/ctor] with pseudorandom numbers drawn from a [hypergeometric][@stdlib/random/base/hypergeometric] distribution.
114+
115+
```javascript
116+
var zeros = require( '@stdlib/ndarray/zeros' );
117+
118+
var out = zeros( [ 3, 3 ] );
119+
// returns <ndarray>
120+
121+
var v = hypergeometric.assign( 20, 10, 7, out );
122+
// returns <ndarray>
123+
124+
var bool = ( v === out );
125+
// returns true
126+
```
127+
128+
The method has the following parameters:
129+
130+
- **N**: population size. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [ndarray][@stdlib/ndarray/ctor].
131+
- **K**: subpopulation size. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [ndarray][@stdlib/ndarray/ctor].
132+
- **n**: number of draws. May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [ndarray][@stdlib/ndarray/ctor].
133+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
134+
135+
#### hypergeometric.factory( \[options] )
136+
137+
Returns a function for generating pseudorandom numbers drawn from a [hypergeometric][@stdlib/random/base/hypergeometric] distribution.
138+
139+
```javascript
140+
var getShape = require( '@stdlib/ndarray/shape' );
141+
142+
var random = hypergeometric.factory();
143+
144+
var out = random( [ 3, 3 ], 20, 10, 7 );
145+
// returns <ndarray>
146+
147+
var sh = getShape( out );
148+
// returns [ 3, 3 ]
149+
```
150+
151+
The method accepts the following options:
152+
153+
- **prng**: pseudorandom number generator for generating uniformly distributed pseudorandom numbers on the interval `[0,1)`. If provided, the function **ignores** both the `state` and `seed` options. In order to seed the underlying pseudorandom number generator, one must seed the provided `prng` (assuming the provided `prng` is seedable).
154+
- **seed**: pseudorandom number generator seed.
155+
- **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
156+
- **copy**: boolean indicating whether to copy a provided pseudorandom number generator state. Setting this option to `false` allows sharing state between two or more pseudorandom number generators. Setting this option to `true` ensures that an underlying generator has exclusive control over its internal state. Default: `true`.
157+
158+
To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option.
159+
160+
```javascript
161+
var minstd = require( '@stdlib/random/base/minstd' );
162+
163+
var opts = {
164+
'prng': minstd.normalized
165+
};
166+
var random = hypergeometric.factory( opts );
167+
168+
var out = random( [ 3, 3 ], 20, 10, 7 );
169+
// returns <ndarray>
170+
```
171+
172+
To seed the underlying pseudorandom number generator, set the `seed` option.
173+
174+
```javascript
175+
var opts = {
176+
'seed': 12345
177+
};
178+
var random = hypergeometric.factory( opts );
179+
180+
var out = random( [ 3, 3 ], 20, 10, 7 );
181+
// returns <ndarray>
182+
```
183+
184+
The function returned by the `factory` method has the same interface and accepts the same options as the `hypergeometric` function above.
185+
186+
#### hypergeometric.PRNG
187+
188+
The underlying pseudorandom number generator.
189+
190+
```javascript
191+
var prng = hypergeometric.PRNG;
192+
// returns <Function>
193+
```
194+
195+
#### hypergeometric.seed
196+
197+
The value used to seed the underlying pseudorandom number generator.
198+
199+
```javascript
200+
var seed = hypergeometric.seed;
201+
// returns <Uint32Array>
202+
```
203+
204+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
205+
206+
```javascript
207+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
208+
209+
var random = hypergeometric.factory({
210+
'prng': minstd
211+
});
212+
213+
var seed = random.seed;
214+
// returns null
215+
```
216+
217+
#### hypergeometric.seedLength
218+
219+
Length of underlying pseudorandom number generator seed.
220+
221+
```javascript
222+
var len = hypergeometric.seedLength;
223+
// returns <number>
224+
```
225+
226+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
227+
228+
```javascript
229+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
230+
231+
var random = hypergeometric.factory({
232+
'prng': minstd
233+
});
234+
235+
var len = random.seedLength;
236+
// returns null
237+
```
238+
239+
#### hypergeometric.state
240+
241+
Writable property for getting and setting the underlying pseudorandom number generator state.
242+
243+
```javascript
244+
var state = hypergeometric.state;
245+
// returns <Uint32Array>
246+
```
247+
248+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
249+
250+
```javascript
251+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
252+
253+
var random = hypergeometric.factory({
254+
'prng': minstd
255+
});
256+
257+
var state = random.state;
258+
// returns null
259+
```
260+
261+
#### hypergeometric.stateLength
262+
263+
Length of underlying pseudorandom number generator state.
264+
265+
```javascript
266+
var len = hypergeometric.stateLength;
267+
// returns <number>
268+
```
269+
270+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
271+
272+
```javascript
273+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
274+
275+
var random = hypergeometric.factory({
276+
'prng': minstd
277+
});
278+
279+
var len = random.stateLength;
280+
// returns null
281+
```
282+
283+
#### hypergeometric.byteLength
284+
285+
Size (in bytes) of underlying pseudorandom number generator state.
286+
287+
```javascript
288+
var sz = hypergeometric.byteLength;
289+
// returns <number>
290+
```
291+
292+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
293+
294+
```javascript
295+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
296+
297+
var random = hypergeometric.factory({
298+
'prng': minstd
299+
});
300+
301+
var sz = random.byteLength;
302+
// returns null
303+
```
304+
305+
</section>
306+
307+
<!-- /.usage -->
308+
309+
<section class="notes">
310+
311+
## Notes
312+
313+
- If PRNG state is "shared" (meaning a state array was provided during function creation and **not** copied) and one sets the underlying generator state to a state array having a different length, the function returned by the `factory` method does **not** update the existing shared state and, instead, points to the newly provided state array. In order to synchronize the output of the underlying generator according to the new shared state array, the state array for **each** relevant creation function and/or PRNG must be **explicitly** set.
314+
- If PRNG state is "shared" and one sets the underlying generator state to a state array of the same length, the PRNG state is updated (along with the state of all other creation functions and/or PRNGs sharing the PRNG's state array).
315+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
316+
317+
</section>
318+
319+
<!-- /.notes -->
320+
321+
<section class="examples">
322+
323+
## Examples
324+
325+
<!-- eslint no-undef: "error" -->
326+
327+
```javascript
328+
var logEach = require( '@stdlib/console/log-each' );
329+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
330+
var hypergeometric = require( '@stdlib/random/hypergeometric' );
331+
332+
// Create a function for generating random arrays originating from the same state:
333+
var random = hypergeometric.factory({
334+
'state': hypergeometric.state,
335+
'copy': true
336+
});
337+
338+
// Generate 3 one-dimensional arrays:
339+
var x1 = random( [ 5 ], 20, 10, 7 );
340+
var x2 = random( [ 5 ], 20, 10, 7 );
341+
var x3 = random( [ 5 ], 20, 10, 7 );
342+
343+
// Print the contents:
344+
logEach( '%f, %f, %f', ndarray2array( x1 ), ndarray2array( x2 ), ndarray2array( x3 ) );
345+
346+
// Create another function for generating random arrays with the original state:
347+
random = hypergeometric.factory({
348+
'state': hypergeometric.state,
349+
'copy': true
350+
});
351+
352+
// Generate a two-dimensional array which replicates the above pseudorandom number generation sequence:
353+
var x4 = random( [ 3, 5 ], 20, 10, 7 );
354+
355+
// Convert to a list of nested arrays:
356+
var arr = ndarray2array( x4 );
357+
358+
// Print the contents:
359+
console.log( '' );
360+
logEach( '%f, %f, %f', arr[ 0 ], arr[ 1 ], arr[ 2 ] );
361+
```
362+
363+
</section>
364+
365+
<!-- /.examples -->
366+
367+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
368+
369+
<section class="related">
370+
371+
</section>
372+
373+
<!-- /.related -->
374+
375+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
376+
377+
<section class="links">
378+
379+
[@stdlib/random/base/hypergeometric]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/hypergeometric
380+
381+
[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32
382+
383+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
384+
385+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
386+
387+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
388+
389+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
390+
391+
</section>
392+
393+
<!-- /.links -->

0 commit comments

Comments
 (0)