You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/user-guides/recipes/how_to_create_an_ndarray_filled_with_random_numbers.md
+34-15Lines changed: 34 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -20,13 +20,13 @@ limitations under the License.
20
20
21
21
# How to create an ndarray filled with random numbers
22
22
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.
24
24
25
25
## Introduction
26
26
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.
28
28
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.
30
30
31
31
## Setup
32
32
@@ -39,19 +39,19 @@ $ npm install @stdlib/stdlib
39
39
## Steps
40
40
41
41
1. Import all necessary packages for generating pseudorandom numbers.
42
-
2.Create a multi-dimensional array.
42
+
2.Generate an ndarray.
43
43
3.\[Optional] Regenerate a sequence by using a seed.
44
44
45
45
### 1. Import required packages
46
46
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.
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.
57
57
@@ -60,10 +60,10 @@ For this recipe, the uniform distribution has two parameters:
60
60
-**a**: minimum support (inclusive).
61
61
-**b**: maximum support (exclusive).
62
62
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`.
64
64
65
65
```javascript
66
-
constshape= [ 3, 3 ]; //3x3
66
+
constshape= [ 3, 3 ]; //3×3
67
67
consta=-10.0; // minimum support
68
68
constb=10.0; // maximum support
69
69
@@ -78,50 +78,69 @@ const xa = ndarray2array( x );
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 thatthe values always reside on the half-open interval `[-10,10)`.
82
82
83
83
> 💡 Tip
84
84
>
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.
86
86
>
87
87
> ```javascript
88
88
>constx32=uniform( shape, a, b, {
89
89
>'dtype':'float32'
90
90
> });
91
91
>```
92
92
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.
94
94
95
95
### 3. \[Optional] Regenerate a sequence by using a seed
96
96
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.
98
98
99
99
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.
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
+
constr2=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
+
constz=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.
114
127
115
128
## Learn More
116
129
117
130
Check out our other [recipes][stdlib-user-guides-recipes] to continue your learning!
118
131
132
+
Sampling other distributions? See [`@stdlib/random`][@stdlib/random] for all of what stdlib has to offer!
0 commit comments