Skip to content

Commit e510ba5

Browse files
committed
feat: add random/tools/binary
--- 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 c9777d9 commit e510ba5

File tree

14 files changed

+2527
-0
lines changed

14 files changed

+2527
-0
lines changed
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
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+
# Random
22+
23+
> Constructor for creating ndarrays filled with pseudorandom values drawn from a binary PRNG.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var Random = require( '@stdlib/random/tools/binary' );
41+
```
42+
43+
#### Random( prng, idtypes, odtypes, policies\[, options] )
44+
45+
Returns an interface for creating ndarrays filled with pseudorandom values drawn from a binary PRNG.
46+
47+
```javascript
48+
var dtypes = require( '@stdlib/ndarray/dtypes' );
49+
var uniform = require( '@stdlib/random/base/uniform' );
50+
51+
var idt = dtypes( 'real_and_generic' );
52+
var odt = dtypes( 'real_floating_point_and_generic' );
53+
54+
var policies = {
55+
'output': 'real_floating_point_and_generic'
56+
};
57+
var options = {
58+
'order': 'row-major'
59+
};
60+
61+
var rand = new Random( uniform, [ idt, idt ], odt, policies, options );
62+
```
63+
64+
The constructor has the following parameters:
65+
66+
- **prng**: binary pseudorandom value generator.
67+
68+
- **idtypes**: list containing a list of supported input data types for each PRNG parameter.
69+
70+
- **odtypes**: list of supported output data types.
71+
72+
- **policies**: interface policies. Must have the following properties:
73+
74+
- **output**: output data type [policy][@stdlib/ndarray/policies].
75+
76+
- **options**: function options (_optional_).
77+
78+
The constructor supports the following options:
79+
80+
- **order**: default [memory layout][@stdlib/ndarray/orders].
81+
82+
#### Random.prototype.generate( shape, param1, param2\[, options] )
83+
84+
Returns an ndarray filled with pseudorandom values drawn from a binary PRNG.
85+
86+
```javascript
87+
var dtypes = require( '@stdlib/ndarray/dtypes' );
88+
var uniform = require( '@stdlib/random/base/uniform' );
89+
90+
var idt = dtypes( 'real_and_generic' );
91+
var odt = dtypes( 'real_floating_point_and_generic' );
92+
93+
var policies = {
94+
'output': 'real_floating_point_and_generic'
95+
};
96+
var options = {
97+
'order': 'row-major'
98+
};
99+
100+
var rand = new Random( uniform, [ idt, idt ], odt, policies, options );
101+
102+
var v = rand.generate( [ 2, 2 ], 0.0, 1.0 );
103+
// returns <ndarray>
104+
```
105+
106+
The method has the following parameters:
107+
108+
- **shape**: output ndarray shape.
109+
- **param1**: first PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output ndarray shape.
110+
- **param2**: second PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output ndarray shape.
111+
- **options**: function options (_optional_).
112+
113+
The method accepts the following options:
114+
115+
- **dtype**: output ndarray data type. Setting this option overrides the output data type policy.
116+
- **order**: memory layout. Setting this option overrides the default memory layout.
117+
- **mode**: specifies how to handle indices which exceed ndarray dimensions.
118+
- **submode**: specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis.
119+
- **readonly**: boolean indicating whether an ndarray should be read-only.
120+
121+
By default, the method returns an ndarray having a data type determined by the output data type policy. To override the default behavior, set the `dtype` option.
122+
123+
```javascript
124+
var dtypes = require( '@stdlib/ndarray/dtypes' );
125+
var getDType = require( '@stdlib/ndarray/dtype' );
126+
var uniform = require( '@stdlib/random/base/uniform' );
127+
128+
var idt = dtypes( 'real_and_generic' );
129+
var odt = dtypes( 'real_floating_point_and_generic' );
130+
131+
var policies = {
132+
'output': 'real_floating_point_and_generic'
133+
};
134+
var options = {
135+
'order': 'row-major'
136+
};
137+
138+
var rand = new Random( uniform, [ idt, idt ], odt, policies, options );
139+
140+
var v = rand.generate( [ 2, 2 ], 0.0, 1.0, {
141+
'dtype': 'generic'
142+
});
143+
// returns <ndarray>
144+
145+
var dt = getDType( v );
146+
// returns 'generic'
147+
```
148+
149+
#### Random.prototype.assign( param1, param2, out )
150+
151+
Fills an ndarray with pseudorandom values drawn from a binary PRNG.
152+
153+
```javascript
154+
var dtypes = require( '@stdlib/ndarray/dtypes' );
155+
var ndzeros = require( '@stdlib/ndarray/zeros' );
156+
var uniform = require( '@stdlib/random/base/uniform' );
157+
158+
var idt = dtypes( 'real_and_generic' );
159+
var odt = dtypes( 'real_floating_point_and_generic' );
160+
161+
var policies = {
162+
'output': 'real_floating_point_and_generic'
163+
};
164+
var options = {
165+
'order': 'row-major'
166+
};
167+
168+
var rand = new Random( uniform, [ idt, idt ], odt, policies, options );
169+
170+
var out = ndzeros( [ 2, 2 ] );
171+
var v = rand.assign( 0.0, 1.0, out );
172+
// returns <ndarray>
173+
174+
var bool = ( v === out );
175+
// returns true
176+
```
177+
178+
The method has the following parameters:
179+
180+
- **param1**: first PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray.
181+
- **param2**: second PRNG parameter. May be either a scalar or an ndarray. If an ndarray, must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output ndarray.
182+
- **out**: output ndarray.
183+
184+
</section>
185+
186+
<!-- /.usage -->
187+
188+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
189+
190+
<section class="notes">
191+
192+
## Notes
193+
194+
- The output data type policy only applies to the `generate` method. For the `assign` method, the output ndarray is allowed to have any supported output data type.
195+
196+
</section>
197+
198+
<!-- /.notes -->
199+
200+
<!-- Package usage examples. -->
201+
202+
<section class="examples">
203+
204+
## Examples
205+
206+
<!-- eslint no-undef: "error" -->
207+
208+
```javascript
209+
var uniform = require( '@stdlib/random/base/uniform' );
210+
var dtypes = require( '@stdlib/ndarray/dtypes' );
211+
var ndarray = require( '@stdlib/ndarray/ctor' );
212+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
213+
var Random = require( '@stdlib/random/tools/binary' );
214+
215+
// Create a new PRNG instance...
216+
var idt = dtypes( 'real_and_generic' );
217+
var odt = dtypes( 'real_floating_point_and_generic' );
218+
var policies = {
219+
'output': 'real_floating_point_and_generic'
220+
};
221+
var random = new Random( uniform, [ idt, idt ], odt, policies );
222+
223+
// Generate a 3x3 matrix of pseudorandom numbers:
224+
var x = random.generate( [ 3, 3 ], 0.0, 1.0 );
225+
console.log( ndarray2array( x ) );
226+
227+
// Generate another matrix with a specified data type:
228+
x = random.generate( [ 3, 3 ], 0.0, 1.0, {
229+
'dtype': 'float32'
230+
});
231+
console.log( ndarray2array( x ) );
232+
233+
// Define arrays of distribution parameters:
234+
var param1 = new ndarray( 'generic', [ 1.0, 10.0, 100.0 ], [ 3, 1 ], [ 1, 1 ], 0, 'row-major' );
235+
var param2 = new ndarray( 'generic', [ 2.0, 20.0, 200.0 ], [ 3, 1 ], [ 1, 1 ], 0, 'row-major' );
236+
237+
// Broadcast the parameters to generate another 3x3 matrix of pseudorandom numbers:
238+
x = random.generate( [ 3, 3 ], param1, param2 );
239+
console.log( ndarray2array( x ) );
240+
```
241+
242+
</section>
243+
244+
<!-- /.examples -->
245+
246+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
247+
248+
<section class="references">
249+
250+
</section>
251+
252+
<!-- /.references -->
253+
254+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
255+
256+
<section class="related">
257+
258+
</section>
259+
260+
<!-- /.related -->
261+
262+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
263+
264+
<section class="links">
265+
266+
[@stdlib/ndarray/policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/policies
267+
268+
[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders
269+
270+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
271+
272+
</section>
273+
274+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var uniform = require( '@stdlib/random/base/uniform' );
28+
var zeros = require( '@stdlib/ndarray/zeros' );
29+
var pkg = require( './../package.json' ).name;
30+
var Random = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var policies;
44+
var random;
45+
var out;
46+
var dt;
47+
48+
dt = dtypes( 'real_floating_point' );
49+
policies = {
50+
'output': 'real_floating_point',
51+
'casting': 'none'
52+
};
53+
random = new Random( uniform, [ dt, dt ], dt, policies );
54+
55+
out = zeros( [ len ], {
56+
'dtype': 'float64'
57+
});
58+
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var o;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
o = random.assign( 0.0, 1.0, out );
74+
if ( typeof o !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnan( o.get( i%len ) ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( pkg+':assign:len='+len, f );
109+
}
110+
}
111+
112+
main();

0 commit comments

Comments
 (0)