Skip to content

Commit 58f2aa6

Browse files
committed
feat: add random/array/logistic
Closes: #943
1 parent deecd83 commit 58f2aa6

22 files changed

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

0 commit comments

Comments
 (0)