|
| 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 | +# How to create ndarrays of random numbers |
| 22 | + |
| 23 | +## Introduction |
| 24 | + |
| 25 | +stdlib provides elegantly designed APIs and packages to help you create multi-dimensional arrays (a.k.a., ndarrays) containing pseudorandom numbers drawn from statistical distributions. These APIs allow you to specify seeds in order to generate reproducible sequences and to specify the output array shape and precision. |
| 26 | + |
| 27 | +In this recipe, we will use [`@stdlib/random/uniform`][@stdlib/random/uniform] to create ndarrays containing pseudorandom numbers drawn from a uniform distribution. |
| 28 | + |
| 29 | +## Setup |
| 30 | + |
| 31 | +Before we begin, we need to install `@stdlib/stdlib`, if it isn't already available. |
| 32 | + |
| 33 | +```bash |
| 34 | +$ npm install @stdlib/stdlib |
| 35 | +``` |
| 36 | + |
| 37 | +## Steps |
| 38 | + |
| 39 | +1. Import all necessary packages for generating pseudorandom numbers. |
| 40 | +2. Create a multi-dimensional array. |
| 41 | +3. \[Optional] Regenerate a sequence by using a seed. |
| 42 | + |
| 43 | +### 1. Import necessary packages for generating pseudorandom numbers |
| 44 | + |
| 45 | +For this recipe, we will use `@stdlib/stdlib` and its package [`@stdlib/random/uniform`][@stdlib/random/uniform]. We will also use the package [`@stdlib/ndarray/to-array`][@stdlib/ndarray/to-array] to convert an ndarray to a conventional array-of-arrays format. |
| 46 | + |
| 47 | +```javascript |
| 48 | +const uniform = require( '@stdlib/random/uniform' ); |
| 49 | +const ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 50 | +``` |
| 51 | + |
| 52 | +### 2. Create a multi-dimensional array |
| 53 | + |
| 54 | +Every stdlib interface for creating ndarrays of pseudorandom numbers drawn from a statistical distribution has two requirements. The first is defining the desired output shape. The second is defining the distribution parameters which control the shape of the sampled distribution. |
| 55 | + |
| 56 | +For this recipe, the uniform distribution has two parameters: |
| 57 | + |
| 58 | +- **a**: minimum support (inclusive). |
| 59 | +- **b**: maximum support (exclusive). |
| 60 | + |
| 61 | +In the following code snippet, we generate a 3x3 matrix of pseudorandom numbers sampled from a uniform distribution with minimum support `-10` and maximum support `+10`. |
| 62 | + |
| 63 | +```javascript |
| 64 | +const shape = [ 3, 3 ]; |
| 65 | +const a = -10.0; |
| 66 | +const b = 10.0; |
| 67 | + |
| 68 | +const x = uniform( shape, a, b ); |
| 69 | +// returns <ndarray> |
| 70 | +``` |
| 71 | + |
| 72 | +ndarrays are multi-dimensional views atop linear memory. While boosting performance and improving memory efficiency, especially when compared to nested arrays, this makes ndarrays somewhat opaque data structures. Accordingly, to convert an ndarray to a conventional array-of-arrays format, we can use [`@stdlib/ndarray/to-array`][@stdlib/ndarray/to-array], as done in the following code snippet. |
| 73 | + |
| 74 | +```javascript |
| 75 | +const xa = ndarray2array( x ); |
| 76 | +// e.g., returns [ [ ~-2.41, ~3.08, ~5.09 ], [ ~5.73, ~-8.12, ~-8.99 ], [ ~0.11, ~-6.69, ~4.79 ] ] |
| 77 | +``` |
| 78 | + |
| 79 | +The output displayed in the above example is representative, and your values are likely to differ. However, notice that, even after running the example multiple times, the values always reside on the half-open interval `[-10,10)`, as expected based on the parameterization we used above. |
| 80 | + |
| 81 | +At this point, we have finished creating an ndarray of pseudorandom numbers, and, now, we will reproduce the same sequence of numbers by using a seed. |
| 82 | + |
| 83 | +### 3. \[Optional] Regenerate a sequence by using a seed |
| 84 | + |
| 85 | +A **seed** is a value which is used to initialize a pseudorandom number generator, and a pseudorandom number generator's sequence is completely determined by that seed. Accordingly, if another pseudorandom number generator instance is initialized with the same seed, that instance will produce the same sequence of numbers. |
| 86 | + |
| 87 | +In the following code snippet, we pass the seed from the `uniform` pseudorandom number generator used above to a function which returns a new `uniform` instance. When calling that instance with the same distribution parameters `a` and `b` that we used above, we generate an ndarray containing the same set of pseudorandomly generated numbers. |
| 88 | + |
| 89 | +```javascript |
| 90 | +const random = uniform.factory({ |
| 91 | + 'seed': uniform.seed |
| 92 | +}); |
| 93 | + |
| 94 | +const y = random( shape, a, b ); |
| 95 | +// returns <ndarray> |
| 96 | + |
| 97 | +const ya = ndarray2array( y ); |
| 98 | +// e.g., returns [ [ ~-2.41, ~3.08, ~5.09 ], [ ~5.73, ~-8.12, ~-8.99 ], [ ~0.11, ~-6.69, ~4.79 ] ] |
| 99 | +``` |
| 100 | + |
| 101 | +Congratulations! You have successfully used stdlib to generate an ndarray of pseudorandom numbers. |
| 102 | + |
| 103 | +## Learn More |
| 104 | + |
| 105 | +Check out our other [recipes][stdlib-user-guides-recipes] to continue your learning! |
| 106 | + |
| 107 | +<!-- links --> |
| 108 | + |
| 109 | +<section class="links"> |
| 110 | + |
| 111 | +[@stdlib/random/uniform]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/uniform |
| 112 | + |
| 113 | +[@stdlib/ndarray/to-array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/to-array |
| 114 | + |
| 115 | +[stdlib-user-guides-recipes]: https://github.com/stdlib-js/stdlib/tree/develop/docs/user-guides/recipes |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.links --> |
0 commit comments