Skip to content

Commit 9197455

Browse files
committed
feat: add random/strided/t
Closes: #890
1 parent 44ac21f commit 9197455

19 files changed

+3854
-0
lines changed
Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,375 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
> Fill a strided array with pseudorandom numbers drawn from a [Student's t][@stdlib/random/base/t]-distribution.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var t = require( '@stdlib/random/strided/t' );
31+
```
32+
33+
#### t( N, v, sv, out, so )
34+
35+
Fills a strided array with pseudorandom numbers drawn from a [Student's t][@stdlib/random/base/t]-distribution.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
// Create an array:
41+
var out = new Float64Array( 10 );
42+
43+
// Fill the array with pseudorandom numbers:
44+
t( out.length, [ 2.0 ], 0, out, 1 );
45+
```
46+
47+
The function has the following parameters:
48+
49+
- **N**: number of indexed elements.
50+
- **v**: rate parameter.
51+
- **sv**: index increment for `v`.
52+
- **out**: output array.
53+
- **so**: index increment for `out`.
54+
55+
The `N` and stride parameters determine which strided array elements are accessed at runtime. For example, to access every other value in `out`,
56+
57+
```javascript
58+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
59+
60+
t( 3, [ 2.0 ], 0, out, 2 );
61+
```
62+
63+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
64+
65+
<!-- eslint-disable stdlib/capitalized-comments -->
66+
67+
```javascript
68+
var Float64Array = require( '@stdlib/array/float64' );
69+
70+
// Initial array:
71+
var v0 = new Float64Array( [ 2.0, 2.0, 2.0, 2.0, 2.0, 2.0 ] );
72+
73+
// Create offset view:
74+
var v1 = new Float64Array( v0.buffer, v0.BYTES_PER_ELEMENT*3 ); // start at 4th element
75+
76+
// Create an output array:
77+
var out = new Float64Array( 3 );
78+
79+
// Fill the output array:
80+
t( out.length, v1, -1, out, 1 );
81+
```
82+
83+
#### t.ndarray( N, v, sv, ov, out, so, oo )
84+
85+
Fills a strided array with pseudorandom numbers drawn from a [Student's t][@stdlib/random/base/t]-distribution using alternative indexing semantics.
86+
87+
```javascript
88+
var Float64Array = require( '@stdlib/array/float64' );
89+
90+
// Create an array:
91+
var out = new Float64Array( 10 );
92+
93+
// Fill the array with pseudorandom numbers:
94+
t.ndarray( out.length, [ 2.0 ], 0, 0, out, 1, 0 );
95+
```
96+
97+
The function has the following additional parameters:
98+
99+
- **ov**: starting index for `v`.
100+
- **oo**: starting index for `out`.
101+
102+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the offset parameters support indexing semantics based on starting indices. For example, to access every other value in `out` starting from the second value,
103+
104+
```javascript
105+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
106+
107+
t.ndarray( 3, [ 2.0 ], 0, 0, out, 2, 1 );
108+
```
109+
110+
#### t.factory( \[options] )
111+
112+
Returns a function for filling strided arrays with pseudorandom numbers drawn from a [Student's t][@stdlib/random/base/t]-distribution.
113+
114+
```javascript
115+
var Float64Array = require( '@stdlib/array/float64' );
116+
117+
var random = t.factory();
118+
// returns <Function>
119+
120+
// Create an array:
121+
var out = new Float64Array( 10 );
122+
123+
// Fill the array with pseudorandom numbers:
124+
random( out.length, [ 2.0 ], 0, out, 1 );
125+
```
126+
127+
The function accepts the following `options`:
128+
129+
- **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).
130+
- **seed**: pseudorandom number generator seed.
131+
- **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option.
132+
- **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`.
133+
134+
To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option.
135+
136+
```javascript
137+
var Float64Array = require( '@stdlib/array/float64' );
138+
var minstd = require( '@stdlib/random/base/minstd' );
139+
140+
var opts = {
141+
'prng': minstd.normalized
142+
};
143+
var random = t.factory( opts );
144+
145+
var out = new Float64Array( 10 );
146+
random( out.length, [ 2.0 ], 0, out, 1 );
147+
```
148+
149+
To seed the underlying pseudorandom number generator, set the `seed` option.
150+
151+
```javascript
152+
var Float64Array = require( '@stdlib/array/float64' );
153+
154+
var opts = {
155+
'seed': 12345
156+
};
157+
var random = t.factory( opts );
158+
159+
var out = new Float64Array( 10 );
160+
random( out.length, [ 2.0 ], 0, out, 1 );
161+
```
162+
163+
* * *
164+
165+
#### random.PRNG
166+
167+
The underlying pseudorandom number generator.
168+
169+
```javascript
170+
var prng = t.PRNG;
171+
// returns <Function>
172+
```
173+
174+
#### t.seed
175+
176+
The value used to seed the underlying pseudorandom number generator.
177+
178+
```javascript
179+
var seed = t.seed;
180+
// returns <Uint32Array>
181+
```
182+
183+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
184+
185+
```javascript
186+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
187+
188+
var random = t.factory({
189+
'prng': minstd
190+
});
191+
// returns <Function>
192+
193+
var seed = random.seed;
194+
// returns null
195+
```
196+
197+
#### t.seedLength
198+
199+
Length of underlying pseudorandom number generator seed.
200+
201+
```javascript
202+
var len = t.seedLength;
203+
// returns <number>
204+
```
205+
206+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
207+
208+
```javascript
209+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
210+
211+
var random = t.factory({
212+
'prng': minstd
213+
});
214+
// returns <Function>
215+
216+
var len = random.seedLength;
217+
// returns null
218+
```
219+
220+
#### t.state
221+
222+
Writable property for getting and setting the underlying pseudorandom number generator state.
223+
224+
```javascript
225+
var state = t.state;
226+
// returns <Uint32Array>
227+
```
228+
229+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
230+
231+
```javascript
232+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
233+
234+
var random = t.factory({
235+
'prng': minstd
236+
});
237+
// returns <Function>
238+
239+
var state = random.state;
240+
// returns null
241+
```
242+
243+
#### t.stateLength
244+
245+
Length of underlying pseudorandom number generator state.
246+
247+
```javascript
248+
var len = t.stateLength;
249+
// returns <number>
250+
```
251+
252+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
253+
254+
```javascript
255+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
256+
257+
var random = t.factory({
258+
'prng': minstd
259+
});
260+
// returns <Function>
261+
262+
var len = random.stateLength;
263+
// returns null
264+
```
265+
266+
#### t.byteLength
267+
268+
Size (in bytes) of underlying pseudorandom number generator state.
269+
270+
```javascript
271+
var sz = t.byteLength;
272+
// returns <number>
273+
```
274+
275+
If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`.
276+
277+
```javascript
278+
var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized;
279+
280+
var random = t.factory({
281+
'prng': minstd
282+
});
283+
// returns <Function>
284+
285+
var sz = random.byteLength;
286+
// returns null
287+
```
288+
289+
</section>
290+
291+
<!-- /.usage -->
292+
293+
<section class="notes">
294+
295+
* * *
296+
297+
## Notes
298+
299+
- If `N <= 0`, both `t` and `t.ndarray` leave the output array unchanged.
300+
- Both `t` and `t.ndarray` support array-like objects having getter and setter accessors for array element access.
301+
302+
</section>
303+
304+
<!-- /.notes -->
305+
306+
<section class="examples">
307+
308+
* * *
309+
310+
## Examples
311+
312+
<!-- eslint no-undef: "error" -->
313+
314+
```javascript
315+
var zeros = require( '@stdlib/array/zeros' );
316+
var zeroTo = require( '@stdlib/array/zero-to' );
317+
var logEach = require( '@stdlib/console/log-each' );
318+
var t = require( '@stdlib/random/strided/t' );
319+
320+
// Specify a PRNG seed:
321+
var opts = {
322+
'seed': 1234
323+
};
324+
325+
// Create a seeded PRNG:
326+
var rand1 = t.factory( opts );
327+
328+
// Create an array:
329+
var x1 = zeros( 10, 'float64' );
330+
331+
// Fill the array with pseudorandom numbers:
332+
rand1( x1.length, [ 2.0 ], 0, x1, 1 );
333+
334+
// Create another function for filling strided arrays:
335+
var rand2 = t.factory( opts );
336+
// returns <Function>
337+
338+
// Create a second array:
339+
var x2 = zeros( 10, 'generic' );
340+
341+
// Fill the array with the same pseudorandom numbers:
342+
rand2( x2.length, [ 2.0 ], 0, x2, 1 );
343+
344+
// Create a list of indices:
345+
var idx = zeroTo( x1.length, 'generic' );
346+
347+
// Print the array contents:
348+
logEach( 'x1[%d] = %.2f; x2[%d] = %.2f', idx, x1, idx, x2 );
349+
```
350+
351+
</section>
352+
353+
<!-- /.examples -->
354+
355+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
356+
357+
<section class="related">
358+
359+
</section>
360+
361+
<!-- /.related -->
362+
363+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
364+
365+
<section class="links">
366+
367+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
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+
</section>
374+
375+
<!-- /.links -->

0 commit comments

Comments
 (0)