|
| 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 --> |
0 commit comments