|
| 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 | +# Uniform Random Numbers |
| 22 | + |
| 23 | +> Generate pseudorandom numbers drawn from a [uniform][@stdlib/random/base/uniform] distribution. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var uniform = require( '@stdlib/random/uniform' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### uniform( shape, a, b\[, options] ) |
| 34 | + |
| 35 | +Returns an [ndarray][@stdlib/ndarray/ctor] containing pseudorandom numbers drawn from a [uniform][@stdlib/random/base/uniform] distribution. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var arr = uniform( [ 3, 3 ], 0.0, 1.0 ); |
| 39 | +// returns <ndarray> |
| 40 | +``` |
| 41 | + |
| 42 | +The function has the following parameters: |
| 43 | + |
| 44 | +- **shape**: output shape. |
| 45 | +- **a**: minimum support (inclusive). May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output shape. |
| 46 | +- **b**: maximum support (exclusive). May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the specified output shape. |
| 47 | +- **options**: function options. |
| 48 | + |
| 49 | +When provided scalar distribution parameters, every element in the output [ndarray][@stdlib/ndarray/ctor] is drawn from the same distribution. To generate pseudorandom numbers drawn from different distributions, provide distribution parameter arguments as [ndarrays][@stdlib/ndarray/ctor]. The following example demonstrates broadcasting an [ndarray][@stdlib/ndarray/ctor] containing distribution parameters to generate sub-matrices drawn from different distributions. |
| 50 | + |
| 51 | +```javascript |
| 52 | +var getShape = require( '@stdlib/ndarray/shape' ); |
| 53 | +var array = require( '@stdlib/ndarray/array' ); |
| 54 | + |
| 55 | +var a = array( [ [ [ 0.0 ] ], [ [ 10.0 ] ] ] ); |
| 56 | +// returns <ndarray> |
| 57 | + |
| 58 | +var b = array( [ [ [ 10.0 ] ], [ [ 20.0 ] ] ] ); |
| 59 | +// returns <ndarray> |
| 60 | + |
| 61 | +var shape = getShape( a ); |
| 62 | +// returns [ 2, 1, 1 ] |
| 63 | + |
| 64 | +var arr = uniform( [ 2, 3, 3 ], a, b ); |
| 65 | +// returns <ndarray> |
| 66 | +``` |
| 67 | + |
| 68 | +If provided an empty shape, the function returns a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 69 | + |
| 70 | +```javascript |
| 71 | +var getShape = require( '@stdlib/ndarray/shape' ); |
| 72 | + |
| 73 | +var arr = uniform( [], 0.0, 1.0 ); |
| 74 | +// returns <ndarray> |
| 75 | + |
| 76 | +var shape = getShape( arr ); |
| 77 | +// returns [] |
| 78 | + |
| 79 | +var v = arr.get(); |
| 80 | +// returns <number> |
| 81 | +``` |
| 82 | + |
| 83 | +The function accepts the following options: |
| 84 | + |
| 85 | +- **dtype**: output ndarray data type. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 86 | +- **order**: ndarray order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`. |
| 87 | +- **mode**: specifies how to handle indices which exceed ndarray dimensions. For a list of supported modes, see [`ndarray`][@stdlib/ndarray/ctor]. Default: `'throw'`. |
| 88 | +- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed ndarray dimensions. If provided fewer modes than dimensions, an [ndarray][@stdlib/ndarray/ctor] instance recycles modes using modulo arithmetic. Default: `[ options.mode ]`. |
| 89 | +- **readonly**: boolean indicating whether an ndarray should be **read-only**. Default: `false`. |
| 90 | + |
| 91 | +By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option. |
| 92 | + |
| 93 | +```javascript |
| 94 | +var getDType = require( '@stdlib/ndarray/dtype' ); |
| 95 | + |
| 96 | +var opts = { |
| 97 | + 'dtype': 'generic' |
| 98 | +}; |
| 99 | + |
| 100 | +var arr = uniform( [ 3, 3 ], 0.0, 1.0, opts ); |
| 101 | +// returns <ndarray> |
| 102 | + |
| 103 | +var dt = getDType( arr ); |
| 104 | +// returns 'generic' |
| 105 | +``` |
| 106 | + |
| 107 | +#### uniform.assign( a, b, out ) |
| 108 | + |
| 109 | +Fills an [ndarray][@stdlib/ndarray/ctor] with pseudorandom numbers drawn from a [uniform][@stdlib/random/base/uniform] distribution. |
| 110 | + |
| 111 | +```javascript |
| 112 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 113 | + |
| 114 | +var out = zeros( [ 3, 3 ] ); |
| 115 | +// returns <ndarray> |
| 116 | + |
| 117 | +var v = uniform.assign( 0.0, 1.0, out ); |
| 118 | +// returns <ndarray> |
| 119 | + |
| 120 | +var bool = ( v === out ); |
| 121 | +// returns true |
| 122 | +``` |
| 123 | + |
| 124 | +The method has the following parameters: |
| 125 | + |
| 126 | +- **a**: minimum support (inclusive). May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [ndarray][@stdlib/ndarray/ctor]. |
| 127 | +- **b**: maximum support (exclusive). May be either a scalar or an [ndarray][@stdlib/ndarray/ctor]. When providing an [ndarray][@stdlib/ndarray/ctor], the [ndarray][@stdlib/ndarray/ctor] must be [broadcast compatible][@stdlib/ndarray/base/broadcast-shapes] with the output [ndarray][@stdlib/ndarray/ctor]. |
| 128 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 129 | + |
| 130 | +#### uniform.factory( \[options] ) |
| 131 | + |
| 132 | +Returns a function for generating pseudorandom numbers drawn from a [uniform][@stdlib/random/base/uniform] distribution. |
| 133 | + |
| 134 | +```javascript |
| 135 | +var getShape = require( '@stdlib/ndarray/shape' ); |
| 136 | + |
| 137 | +var random = uniform.factory(); |
| 138 | + |
| 139 | +var out = random( [ 3, 3 ], 0.0, 1.0 ); |
| 140 | +// returns <ndarray> |
| 141 | + |
| 142 | +var sh = getShape( out ); |
| 143 | +// returns [ 3, 3 ] |
| 144 | +``` |
| 145 | + |
| 146 | +The method accepts the following options: |
| 147 | + |
| 148 | +- **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). |
| 149 | +- **seed**: pseudorandom number generator seed. |
| 150 | +- **state**: a [`Uint32Array`][@stdlib/array/uint32] containing pseudorandom number generator state. If provided, the function ignores the `seed` option. |
| 151 | +- **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`. |
| 152 | + |
| 153 | +To use a custom PRNG as the underlying source of uniformly distributed pseudorandom numbers, set the `prng` option. |
| 154 | + |
| 155 | +```javascript |
| 156 | +var minstd = require( '@stdlib/random/base/minstd' ); |
| 157 | + |
| 158 | +var opts = { |
| 159 | + 'prng': minstd.normalized |
| 160 | +}; |
| 161 | +var random = uniform.factory( opts ); |
| 162 | + |
| 163 | +var out = random( [ 3, 3 ], 0.0, 1.0 ); |
| 164 | +// returns <ndarray> |
| 165 | +``` |
| 166 | + |
| 167 | +To seed the underlying pseudorandom number generator, set the `seed` option. |
| 168 | + |
| 169 | +```javascript |
| 170 | +var opts = { |
| 171 | + 'seed': 12345 |
| 172 | +}; |
| 173 | +var random = uniform.factory( opts ); |
| 174 | + |
| 175 | +var out = random( [ 3, 3 ], 0.0, 1.0 ); |
| 176 | +// returns <ndarray> |
| 177 | +``` |
| 178 | + |
| 179 | +The function returned by the `factory` method has the same interface and accepts the same options as the `uniform` function above. |
| 180 | + |
| 181 | +#### uniform.PRNG |
| 182 | + |
| 183 | +The underlying pseudorandom number generator. |
| 184 | + |
| 185 | +```javascript |
| 186 | +var prng = uniform.PRNG; |
| 187 | +// returns <Function> |
| 188 | +``` |
| 189 | + |
| 190 | +#### uniform.seed |
| 191 | + |
| 192 | +The value used to seed the underlying pseudorandom number generator. |
| 193 | + |
| 194 | +```javascript |
| 195 | +var seed = uniform.seed; |
| 196 | +// returns <Uint32Array> |
| 197 | +``` |
| 198 | + |
| 199 | +If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`. |
| 200 | + |
| 201 | +```javascript |
| 202 | +var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized; |
| 203 | + |
| 204 | +var random = uniform.factory({ |
| 205 | + 'prng': minstd |
| 206 | +}); |
| 207 | + |
| 208 | +var seed = random.seed; |
| 209 | +// returns null |
| 210 | +``` |
| 211 | + |
| 212 | +#### uniform.seedLength |
| 213 | + |
| 214 | +Length of underlying pseudorandom number generator seed. |
| 215 | + |
| 216 | +```javascript |
| 217 | +var len = uniform.seedLength; |
| 218 | +// returns <number> |
| 219 | +``` |
| 220 | + |
| 221 | +If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`. |
| 222 | + |
| 223 | +```javascript |
| 224 | +var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized; |
| 225 | + |
| 226 | +var random = uniform.factory({ |
| 227 | + 'prng': minstd |
| 228 | +}); |
| 229 | + |
| 230 | +var len = random.seedLength; |
| 231 | +// returns null |
| 232 | +``` |
| 233 | + |
| 234 | +#### uniform.state |
| 235 | + |
| 236 | +Writable property for getting and setting the underlying pseudorandom number generator state. |
| 237 | + |
| 238 | +```javascript |
| 239 | +var state = uniform.state; |
| 240 | +// returns <Uint32Array> |
| 241 | +``` |
| 242 | + |
| 243 | +If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`. |
| 244 | + |
| 245 | +```javascript |
| 246 | +var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized; |
| 247 | + |
| 248 | +var random = uniform.factory({ |
| 249 | + 'prng': minstd |
| 250 | +}); |
| 251 | + |
| 252 | +var state = random.state; |
| 253 | +// returns null |
| 254 | +``` |
| 255 | + |
| 256 | +#### uniform.stateLength |
| 257 | + |
| 258 | +Length of underlying pseudorandom number generator state. |
| 259 | + |
| 260 | +```javascript |
| 261 | +var len = uniform.stateLength; |
| 262 | +// returns <number> |
| 263 | +``` |
| 264 | + |
| 265 | +If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`. |
| 266 | + |
| 267 | +```javascript |
| 268 | +var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized; |
| 269 | + |
| 270 | +var random = uniform.factory({ |
| 271 | + 'prng': minstd |
| 272 | +}); |
| 273 | + |
| 274 | +var len = random.stateLength; |
| 275 | +// returns null |
| 276 | +``` |
| 277 | + |
| 278 | +#### uniform.byteLength |
| 279 | + |
| 280 | +Size (in bytes) of underlying pseudorandom number generator state. |
| 281 | + |
| 282 | +```javascript |
| 283 | +var sz = uniform.byteLength; |
| 284 | +// returns <number> |
| 285 | +``` |
| 286 | + |
| 287 | +If the `factory` method is provided a PRNG for uniformly distributed numbers, the associated property value on the returned function is `null`. |
| 288 | + |
| 289 | +```javascript |
| 290 | +var minstd = require( '@stdlib/random/base/minstd-shuffle' ).normalized; |
| 291 | + |
| 292 | +var random = uniform.factory({ |
| 293 | + 'prng': minstd |
| 294 | +}); |
| 295 | + |
| 296 | +var sz = random.byteLength; |
| 297 | +// returns null |
| 298 | +``` |
| 299 | + |
| 300 | +</section> |
| 301 | + |
| 302 | +<!-- /.usage --> |
| 303 | + |
| 304 | +<section class="notes"> |
| 305 | + |
| 306 | +## Notes |
| 307 | + |
| 308 | +- 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. |
| 309 | +- 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). |
| 310 | +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. |
| 311 | + |
| 312 | +</section> |
| 313 | + |
| 314 | +<!-- /.notes --> |
| 315 | + |
| 316 | +<section class="examples"> |
| 317 | + |
| 318 | +## Examples |
| 319 | + |
| 320 | +<!-- eslint no-undef: "error" --> |
| 321 | + |
| 322 | +```javascript |
| 323 | +var logEach = require( '@stdlib/console/log-each' ); |
| 324 | +var toArray = require( '@stdlib/ndarray/to-array' ); |
| 325 | +var uniform = require( '@stdlib/random/uniform' ); |
| 326 | + |
| 327 | +// Create a function for generating random arrays originating from the same state: |
| 328 | +var random = uniform.factory({ |
| 329 | + 'state': uniform.state, |
| 330 | + 'copy': true |
| 331 | +}); |
| 332 | + |
| 333 | +// Generate 3 one-dimensional arrays: |
| 334 | +var x1 = random( [ 5 ], 0.0, 1.0 ); |
| 335 | +var x2 = random( [ 5 ], 0.0, 1.0 ); |
| 336 | +var x3 = random( [ 5 ], 0.0, 1.0 ); |
| 337 | + |
| 338 | +// Print the contents: |
| 339 | +logEach( '%f, %f, %f', toArray( x1 ), toArray( x2 ), toArray( x3 ) ); |
| 340 | + |
| 341 | +// Create another function for generating random arrays with the original state: |
| 342 | +random = uniform.factory({ |
| 343 | + 'state': uniform.state, |
| 344 | + 'copy': true |
| 345 | +}); |
| 346 | + |
| 347 | +// Generate a two-dimensional array which replicates the above pseudorandom number generation sequence: |
| 348 | +var x4 = random( [ 3, 5 ], 0.0, 1.0 ); |
| 349 | + |
| 350 | +// Convert to a list of nested arrays: |
| 351 | +var arr = toArray( x4 ); |
| 352 | + |
| 353 | +// Print the contents: |
| 354 | +console.log( '' ); |
| 355 | +logEach( '%f, %f, %f', arr[ 0 ], arr[ 1 ], arr[ 2 ] ); |
| 356 | +``` |
| 357 | + |
| 358 | +</section> |
| 359 | + |
| 360 | +<!-- /.examples --> |
| 361 | + |
| 362 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 363 | + |
| 364 | +<section class="related"> |
| 365 | + |
| 366 | +</section> |
| 367 | + |
| 368 | +<!-- /.related --> |
| 369 | + |
| 370 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 371 | + |
| 372 | +<section class="links"> |
| 373 | + |
| 374 | +[@stdlib/random/base/uniform]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/base/uniform |
| 375 | + |
| 376 | +[@stdlib/array/uint32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint32 |
| 377 | + |
| 378 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 379 | + |
| 380 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies |
| 381 | + |
| 382 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 383 | + |
| 384 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 385 | + |
| 386 | +</section> |
| 387 | + |
| 388 | +<!-- /.links --> |
0 commit comments