Skip to content

Commit ff7ce26

Browse files
committed
feat: add random/t
--- 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 3645e6c commit ff7ce26

22 files changed

+5006
-0
lines changed
Lines changed: 383 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,383 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# Student's t Random Numbers
22+
23+
> Generate pseudorandom numbers drawn from a [Student's t-distribution][@stdlib/random/base/t].
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var t = require( '@stdlib/random/t' );
31+
```
32+
33+
#### t( shape, v\[, options] )
34+
35+
Returns an [ndarray][@stdlib/ndarray/ctor] containing pseudorandom numbers drawn from a [Student's t-distribution][@stdlib/random/base/t].
36+
37+
```javascript
38+
var arr = t( [ 3, 3 ], 2.0 );
39+
// returns <ndarray>
40+
```
41+
42+
The function has the following parameters:
43+
44+
- **shape**: output shape.
45+
- **v**: degrees of freedom. 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+
- **options**: function options.
47+
48+
When provided a scalar distribution parameter, every element in the output [ndarray][@stdlib/ndarray/ctor] is drawn from the same distribution. To generate pseudorandom numbers drawn from different distributions, provide a distribution parameter argument as an [ndarray][@stdlib/ndarray/ctor]. The following example demonstrates broadcasting an [ndarray][@stdlib/ndarray/ctor] containing distribution parameters to generate sub-matrices drawn from different distributions.
49+
50+
```javascript
51+
var getShape = require( '@stdlib/ndarray/shape' );
52+
var array = require( '@stdlib/ndarray/array' );
53+
54+
var v = array( [ [ [ 2.0 ] ], [ [ 10.0 ] ] ] );
55+
// returns <ndarray>
56+
57+
var shape = getShape( v );
58+
// returns [ 2, 1, 1 ]
59+
60+
var arr = t( [ 2, 3, 3 ], v );
61+
// returns <ndarray>
62+
```
63+
64+
If provided an empty shape, the function returns a zero-dimensional [ndarray][@stdlib/ndarray/ctor].
65+
66+
```javascript
67+
var getShape = require( '@stdlib/ndarray/shape' );
68+
69+
var arr = t( [], 2.0 );
70+
// returns <ndarray>
71+
72+
var shape = getShape( arr );
73+
// returns []
74+
75+
var v = arr.get();
76+
// returns <number>
77+
```
78+
79+
The function accepts the following options:
80+
81+
- **dtype**: output ndarray data type. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
82+
- **order**: ndarray order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`.
83+
- **mode**: specifies how to handle indices which exceed ndarray dimensions. For a list of supported modes, see [`ndarray`][@stdlib/ndarray/ctor]. Default: `'throw'`.
84+
- **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 ]`.
85+
- **readonly**: boolean indicating whether an ndarray should be **read-only**. Default: `false`.
86+
87+
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.
88+
89+
```javascript
90+
var getDType = require( '@stdlib/ndarray/dtype' );
91+
92+
var opts = {
93+
'dtype': 'generic'
94+
};
95+
96+
var arr = t( [ 3, 3 ], 2.0, opts );
97+
// returns <ndarray>
98+
99+
var dt = String( getDType( arr ) );
100+
// returns 'generic'
101+
```
102+
103+
#### t.assign( v, out )
104+
105+
Fills an [ndarray][@stdlib/ndarray/ctor] with pseudorandom numbers drawn from a [Student's t-distribution][@stdlib/random/base/t].
106+
107+
```javascript
108+
var zeros = require( '@stdlib/ndarray/zeros' );
109+
110+
var out = zeros( [ 3, 3 ] );
111+
// returns <ndarray>
112+
113+
var v = t.assign( 2.0, out );
114+
// returns <ndarray>
115+
116+
var bool = ( v === out );
117+
// returns true
118+
```
119+
120+
The method has the following parameters:
121+
122+
- **v**: degrees of freedom. 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].
123+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
124+
125+
#### t.factory( \[options] )
126+
127+
Returns a function for generating pseudorandom numbers drawn from a [Student's t-distribution][@stdlib/random/base/t].
128+
129+
```javascript
130+
var getShape = require( '@stdlib/ndarray/shape' );
131+
132+
var random = t.factory();
133+
134+
var out = random( [ 3, 3 ], 2.0 );
135+
// returns <ndarray>
136+
137+
var sh = getShape( out );
138+
// returns [ 3, 3 ]
139+
```
140+
141+
The method accepts the following options:
142+
143+
- **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).
144+
- **seed**: pseudorandom number generator seed.
145+
- **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
146+
- **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`.
147+
148+
To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option.
149+
150+
```javascript
151+
var minstd = require( '@stdlib/random/base/minstd' );
152+
153+
var opts = {
154+
'prng': minstd.normalized
155+
};
156+
var random = t.factory( opts );
157+
158+
var out = random( [ 3, 3 ], 2.0 );
159+
// returns <ndarray>
160+
```
161+
162+
To seed the underlying pseudorandom number generator, set the `seed` option.
163+
164+
```javascript
165+
var opts = {
166+
'seed': 12345
167+
};
168+
var random = t.factory( opts );
169+
170+
var out = random( [ 3, 3 ], 2.0 );
171+
// returns <ndarray>
172+
```
173+
174+
The function returned by the `factory` method has the same interface and accepts the same options as the `t` function above.
175+
176+
#### t.PRNG
177+
178+
The underlying pseudorandom number generator.
179+
180+
```javascript
181+
var prng = t.PRNG;
182+
// returns <Function>
183+
```
184+
185+
#### t.seed
186+
187+
The value used to seed the underlying pseudorandom number generator.
188+
189+
```javascript
190+
var seed = t.seed;
191+
// returns <Uint32Array>
192+
```
193+
194+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
195+
196+
```javascript
197+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
198+
199+
var random = t.factory({
200+
'prng': minstd
201+
});
202+
203+
var seed = random.seed;
204+
// returns null
205+
```
206+
207+
#### t.seedLength
208+
209+
Length of underlying pseudorandom number generator seed.
210+
211+
```javascript
212+
var len = t.seedLength;
213+
// returns <number>
214+
```
215+
216+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
217+
218+
```javascript
219+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
220+
221+
var random = t.factory({
222+
'prng': minstd
223+
});
224+
225+
var len = random.seedLength;
226+
// returns null
227+
```
228+
229+
#### t.state
230+
231+
Writable property for getting and setting the underlying pseudorandom number generator state.
232+
233+
```javascript
234+
var state = t.state;
235+
// returns <Uint32Array>
236+
```
237+
238+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
239+
240+
```javascript
241+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
242+
243+
var random = t.factory({
244+
'prng': minstd
245+
});
246+
247+
var state = random.state;
248+
// returns null
249+
```
250+
251+
#### t.stateLength
252+
253+
Length of underlying pseudorandom number generator state.
254+
255+
```javascript
256+
var len = t.stateLength;
257+
// returns <number>
258+
```
259+
260+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
261+
262+
```javascript
263+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
264+
265+
var random = t.factory({
266+
'prng': minstd
267+
});
268+
269+
var len = random.stateLength;
270+
// returns null
271+
```
272+
273+
#### t.byteLength
274+
275+
Size (in bytes) of underlying pseudorandom number generator state.
276+
277+
```javascript
278+
var sz = t.byteLength;
279+
// returns <number>
280+
```
281+
282+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
283+
284+
```javascript
285+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
286+
287+
var random = t.factory({
288+
'prng': minstd
289+
});
290+
291+
var sz = random.byteLength;
292+
// returns null
293+
```
294+
295+
</section>
296+
297+
<!-- /.usage -->
298+
299+
<section class="notes">
300+
301+
## Notes
302+
303+
- 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.
304+
- 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).
305+
- 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 floating-point 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].
306+
307+
</section>
308+
309+
<!-- /.notes -->
310+
311+
<section class="examples">
312+
313+
## Examples
314+
315+
<!-- eslint no-undef: "error" -->
316+
317+
```javascript
318+
var logEach = require( '@stdlib/console/log-each' );
319+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
320+
var t = require( '@stdlib/random/t' );
321+
322+
// Create a function for generating random arrays originating from the same state:
323+
var random = t.factory({
324+
'state': t.state,
325+
'copy': true
326+
});
327+
328+
// Generate 3 one-dimensional arrays:
329+
var x1 = random( [ 5 ], 2.0 );
330+
var x2 = random( [ 5 ], 2.0 );
331+
var x3 = random( [ 5 ], 2.0 );
332+
333+
// Print the contents:
334+
logEach( '%f, %f, %f', ndarray2array( x1 ), ndarray2array( x2 ), ndarray2array( x3 ) );
335+
336+
// Create another function for generating random arrays with the original state:
337+
random = t.factory({
338+
'state': t.state,
339+
'copy': true
340+
});
341+
342+
// Generate a two-dimensional array which replicates the above pseudorandom number generation sequence:
343+
var x4 = random( [ 3, 5 ], 2.0 );
344+
345+
// Convert to a list of nested arrays:
346+
var arr = ndarray2array( x4 );
347+
348+
// Print the contents:
349+
console.log( '' );
350+
logEach( '%f, %f, %f', arr[ 0 ], arr[ 1 ], arr[ 2 ] );
351+
```
352+
353+
</section>
354+
355+
<!-- /.examples -->
356+
357+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
358+
359+
<section class="related">
360+
361+
</section>
362+
363+
<!-- /.related -->
364+
365+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
366+
367+
<section class="links">
368+
369+
[@stdlib/random/base/t]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/t
370+
371+
[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32
372+
373+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
374+
375+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
376+
377+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
378+
379+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
380+
381+
</section>
382+
383+
<!-- /.links -->

0 commit comments

Comments
 (0)