|
| 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 numbers drawn from a single-parameter probability distribution. |
| 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/unary' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### Random( prng, idtypes odtypes, policies\[, options] ) |
| 44 | + |
| 45 | +Returns an interface for creating ndarrays filled with pseudorandom numbers drawn from a single-parameter probability distribution. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 49 | +var exponential = require( '@stdlib/random/base/exponential' ); |
| 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( exponential, idt, odt, policies, options ); |
| 62 | +``` |
| 63 | + |
| 64 | +The constructor has the following parameters: |
| 65 | + |
| 66 | +- **prng**: unary pseudorandom number generator. |
| 67 | + |
| 68 | +- **idtypes**: list of supported input data types. |
| 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 [order][@stdlib/ndarray/orders]. |
| 81 | + |
| 82 | +#### Random.prototype.generate( shape, param1\[, options] ) |
| 83 | + |
| 84 | +Returns an ndarray filled with pseudorandom numbers drawn from a probability distribution. |
| 85 | + |
| 86 | +```javascript |
| 87 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 88 | +var exponential = require( '@stdlib/random/base/exponential' ); |
| 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( exponential, idt, odt, policies, options ); |
| 101 | + |
| 102 | +var v = rand.generate( [ 2, 2 ], 2.0 ); |
| 103 | +// returns <ndarray> |
| 104 | +``` |
| 105 | + |
| 106 | +The method has the following parameters: |
| 107 | + |
| 108 | +- **shape**: output ndarray shape. |
| 109 | +- **param1**: distribution 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 | +- **options**: function options (_optional_). |
| 111 | + |
| 112 | +The method accepts the following options: |
| 113 | + |
| 114 | +- **dtype**: output ndarray data type. Setting this option overrides the output data type policy. |
| 115 | +- **order**: memory layout. Setting this option overrides the default memory layout order. |
| 116 | +- **mode**: specifies how to handle indices which exceed ndarray dimensions. |
| 117 | +- **submode**: specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis. |
| 118 | +- **readonly**: boolean indicating whether an ndarray should be read-only. |
| 119 | + |
| 120 | +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. |
| 121 | + |
| 122 | +```javascript |
| 123 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 124 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 125 | +var exponential = require( '@stdlib/random/base/exponential' ); |
| 126 | + |
| 127 | +var idt = dtypes( 'real_and_generic' ); |
| 128 | +var odt = dtypes( 'real_floating_point_and_generic' ); |
| 129 | + |
| 130 | +var policies = { |
| 131 | + 'output': 'real_floating_point_and_generic' |
| 132 | +}; |
| 133 | +var options = { |
| 134 | + 'order': 'row-major' |
| 135 | +}; |
| 136 | + |
| 137 | +var rand = new Random( exponential, idt, odt, policies, options ); |
| 138 | + |
| 139 | +var v = rand.generate( [ 2, 2 ], 2.0, { |
| 140 | + 'dtype': 'generic' |
| 141 | +}); |
| 142 | +// returns <ndarray> |
| 143 | + |
| 144 | +var dt = getDType( v ); |
| 145 | +// returns 'generic' |
| 146 | +``` |
| 147 | + |
| 148 | +#### Random.prototype.assign( param1, out ) |
| 149 | + |
| 150 | +Fills an ndarray with pseudorandom numbers drawn from a probability distribution. |
| 151 | + |
| 152 | +```javascript |
| 153 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 154 | +var ndzeros = require( '@stdlib/ndarray/zeros' ); |
| 155 | +var exponential = require( '@stdlib/random/base/exponential' ); |
| 156 | + |
| 157 | +var idt = dtypes( 'real_and_generic' ); |
| 158 | +var odt = dtypes( 'real_floating_point_and_generic' ); |
| 159 | + |
| 160 | +var policies = { |
| 161 | + 'output': 'real_floating_point_and_generic' |
| 162 | +}; |
| 163 | +var options = { |
| 164 | + 'order': 'row-major' |
| 165 | +}; |
| 166 | + |
| 167 | +var rand = new Random( exponential, idt, odt, policies, options ); |
| 168 | + |
| 169 | +var out = ndzeros( [ 2, 2 ] ); |
| 170 | +var v = rand.assign( 2.0, out ); |
| 171 | +// returns <ndarray> |
| 172 | + |
| 173 | +var bool = ( v === out ); |
| 174 | +// returns true |
| 175 | +``` |
| 176 | + |
| 177 | +The method has the following parameters: |
| 178 | + |
| 179 | +- **param1**: distribution 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. |
| 180 | +- **out**: output ndarray. |
| 181 | + |
| 182 | +</section> |
| 183 | + |
| 184 | +<!-- /.usage --> |
| 185 | + |
| 186 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 187 | + |
| 188 | +<section class="notes"> |
| 189 | + |
| 190 | +## Notes |
| 191 | + |
| 192 | +- 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. |
| 193 | + |
| 194 | +</section> |
| 195 | + |
| 196 | +<!-- /.notes --> |
| 197 | + |
| 198 | +<!-- Package usage examples. --> |
| 199 | + |
| 200 | +<section class="examples"> |
| 201 | + |
| 202 | +## Examples |
| 203 | + |
| 204 | +<!-- eslint no-undef: "error" --> |
| 205 | + |
| 206 | +```javascript |
| 207 | +var exponential = require( '@stdlib/random/base/exponential' ); |
| 208 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 209 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 210 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 211 | +var Random = require( '@stdlib/random/tools/unary' ); |
| 212 | + |
| 213 | +// Create a new PRNG instance... |
| 214 | +var idt = dtypes( 'real_and_generic' ); |
| 215 | +var odt = dtypes( 'real_floating_point_and_generic' ); |
| 216 | +var policies = { |
| 217 | + 'output': 'real_floating_point_and_generic' |
| 218 | +}; |
| 219 | +var random = new Random( exponential, idt, odt, policies ); |
| 220 | + |
| 221 | +// Generate a 3x3 matrix of pseudorandom numbers: |
| 222 | +var x = random.generate( [ 3, 3 ], 2.0 ); |
| 223 | +console.log( ndarray2array( x ) ); |
| 224 | + |
| 225 | +// Generate another matrix with a specified data type: |
| 226 | +x = random.generate( [ 3, 3 ], 2.0, { |
| 227 | + 'dtype': 'float32' |
| 228 | +}); |
| 229 | +console.log( ndarray2array( x ) ); |
| 230 | + |
| 231 | +// Define an array of distribution parameters: |
| 232 | +var param = new ndarray( 'generic', [ 2.0, 20.0, 200.0 ], [ 3, 1 ], [ 1, 1 ], 0, 'row-major' ); |
| 233 | + |
| 234 | +// Broadcast the parameters to generate another 3x3 matrix of pseudorandom numbers: |
| 235 | +x = random.generate( [ 3, 3 ], param ); |
| 236 | +console.log( ndarray2array( x ) ); |
| 237 | +``` |
| 238 | + |
| 239 | +</section> |
| 240 | + |
| 241 | +<!-- /.examples --> |
| 242 | + |
| 243 | +<!-- 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. --> |
| 244 | + |
| 245 | +<section class="references"> |
| 246 | + |
| 247 | +</section> |
| 248 | + |
| 249 | +<!-- /.references --> |
| 250 | + |
| 251 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 252 | + |
| 253 | +<section class="related"> |
| 254 | + |
| 255 | +</section> |
| 256 | + |
| 257 | +<!-- /.related --> |
| 258 | + |
| 259 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 260 | + |
| 261 | +<section class="links"> |
| 262 | + |
| 263 | +[@stdlib/ndarray/policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/policies |
| 264 | + |
| 265 | +[@stdlib/ndarray/orders]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/orders |
| 266 | + |
| 267 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 268 | + |
| 269 | +</section> |
| 270 | + |
| 271 | +<!-- /.links --> |
0 commit comments