Skip to content

Commit e1e030b

Browse files
committed
docs: update copy
--- 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: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 97128a6 commit e1e030b

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

docs/user-guides/recipes/how_to_create_an_ndarray_filled_with_random_numbers.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ limitations under the License.
2020

2121
# How to create an ndarray filled with random numbers
2222

23-
> Create an ndarray of uniformly-distributed pseudorandom numbers (with optional reproducibility via a seed).
23+
> Create an ndarray of uniformly-distributed pseudorandom numbers and then regenerate the exact same ndarray using a seed or saved PRNG state.
2424
2525
## Introduction
2626

27-
stdlib provides 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.
27+
stdlib provides APIs and packages to help you create [ndarrays][@stdlib/ndarray/ctor] (a.k.a., multi-dimensional arrays) 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.
2828

29-
In this recipe, we will use [`@stdlib/random/uniform`][@stdlib/random/uniform] to create ndarrays containing pseudorandom numbers drawn from a uniform distribution.
29+
In this recipe, we'll use [`@stdlib/random/uniform`][@stdlib/random/uniform] to create ndarrays containing pseudorandom numbers drawn from a uniform distribution.
3030

3131
## Setup
3232

@@ -39,19 +39,19 @@ $ npm install @stdlib/stdlib
3939
## Steps
4040

4141
1. Import all necessary packages for generating pseudorandom numbers.
42-
2. Create a multi-dimensional array.
42+
2. Generate an ndarray.
4343
3. \[Optional] Regenerate a sequence by using a seed.
4444

4545
### 1. Import required packages
4646

47-
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.
47+
For this recipe, we'll use `@stdlib/stdlib` and its package [`@stdlib/random/uniform`][@stdlib/random/uniform]. We'll also use the package [`@stdlib/ndarray/to-array`][@stdlib/ndarray/to-array] to convert an ndarray to a conventional array-of-arrays format.
4848

4949
```javascript
5050
const uniform = require( '@stdlib/random/uniform' );
5151
const ndarray2array = require( '@stdlib/ndarray/to-array' );
5252
```
5353

54-
### 2. Create a multi-dimensional array
54+
### 2. Generate an ndarray
5555

5656
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.
5757

@@ -60,10 +60,10 @@ For this recipe, the uniform distribution has two parameters:
6060
- **a**: minimum support (inclusive).
6161
- **b**: maximum support (exclusive).
6262

63-
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`.
63+
In the following code snippet, we generate a 3×3 matrix of pseudorandom numbers sampled from a uniform distribution with minimum support `-10` and maximum support `+10`.
6464

6565
```javascript
66-
const shape = [ 3, 3 ]; // 3x3
66+
const shape = [ 3, 3 ]; // 3×3
6767
const a = -10.0; // minimum support
6868
const b = 10.0; // maximum support
6969

@@ -78,50 +78,69 @@ const xa = ndarray2array( x );
7878
// e.g., returns [ [ ~-2.41, ~3.08, ~5.09 ], [ ~5.73, ~-8.12, ~-8.99 ], [ ~0.11, ~-6.69, ~4.79 ] ]
7979
```
8080

81-
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.
81+
The output displayed in the above example is representative, and your values are likely to differ. However, notice that the values always reside on the half-open interval `[-10,10)`.
8282

8383
> 💡 Tip
8484
>
85-
> By default, `uniform` returns an ndarray having a `float64` data type (i.e., an ndarray of double-precision floating-point numbers). To return an ndarray having either a `float32` or `generic` data type, you can provide a `dtype` option when calling `uniform`.
85+
> By default, `uniform` returns an ndarray having a `float64` data type (i.e., an ndarray of double-precision floating-point numbers). To return an ndarray having either a `float32` or `generic` data type, you can provide a `dtype` option when calling `uniform`, as displayed in the following code snippet.
8686
>
8787
> ```javascript
8888
> const x32 = uniform( shape, a, b, {
8989
> 'dtype': 'float32'
9090
> });
9191
> ```
9292
93-
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.
93+
At this point, we've finished creating an ndarray of pseudorandom numbers, and now we'll reproduce the same sequence of numbers by using a seed.
9494
9595
### 3. \[Optional] Regenerate a sequence by using a seed
9696
97-
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.
97+
A **seed** is a value which initializes 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.
9898
9999
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.
100100
101101
```javascript
102-
const random = uniform.factory({
102+
const r1 = uniform.factory({
103103
'seed': uniform.seed
104104
});
105105
106-
const y = random( shape, a, b );
106+
const y = r1( shape, a, b );
107107
// returns <ndarray>
108108
109109
const ya = ndarray2array( y );
110110
// e.g., returns [ [ ~-2.41, ~3.08, ~5.09 ], [ ~5.73, ~-8.12, ~-8.99 ], [ ~0.11, ~-6.69, ~4.79 ] ]
111111
```
112112
113-
Congratulations! You have successfully used stdlib to generate an ndarray of pseudorandom numbers.
113+
Need to configure a `uniform` instance to continue from the current generator state, instead of starting over? Pass a state when creating a new `uniform` instance, as done in the following code snippet.
114+
115+
```javascript
116+
const r2 = uniform.factory({
117+
'state': uniform.state,
118+
'copy': true // set to `false` to share state
119+
});
120+
121+
// Generate the same sequence of values as if calling `uniform` a second ndarray:
122+
const z = r2( shape, a, b );
123+
// returns <ndarray>
124+
```
125+
126+
Congratulations! You have successfully used stdlib to generate (and re-generate) an ndarray of pseudorandom numbers.
114127

115128
## Learn More
116129

117130
Check out our other [recipes][stdlib-user-guides-recipes] to continue your learning!
118131

132+
Sampling other distributions? See [`@stdlib/random`][@stdlib/random] for all of what stdlib has to offer!
133+
119134
<!-- links -->
120135

121136
<section class="links">
122137

138+
[@stdlib/random]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random
139+
123140
[@stdlib/random/uniform]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/random/uniform
124141

142+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
143+
125144
[@stdlib/ndarray/to-array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/to-array
126145

127146
[stdlib-user-guides-recipes]: https://github.com/stdlib-js/stdlib/tree/develop/docs/user-guides/recipes

0 commit comments

Comments
 (0)