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
+18-6Lines changed: 18 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -18,11 +18,13 @@ limitations under the License.
18
18
19
19
-->
20
20
21
-
# How to create ndarrays of random numbers
21
+
# How to create an ndarray filled with random numbers
22
+
23
+
> Create an ndarray of uniformly-distributed pseudorandom numbers (with optional reproducibility via a seed).
22
24
23
25
## Introduction
24
26
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.
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.
26
28
27
29
In this recipe, we will use [`@stdlib/random/uniform`][@stdlib/random/uniform] to create ndarrays containing pseudorandom numbers drawn from a uniform distribution.
28
30
@@ -40,7 +42,7 @@ $ npm install @stdlib/stdlib
40
42
2. Create a multi-dimensional array.
41
43
3.\[Optional] Regenerate a sequence by using a seed.
42
44
43
-
### 1. Import necessary packages for generating pseudorandom numbers
45
+
### 1. Import required packages
44
46
45
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.
46
48
@@ -61,9 +63,9 @@ For this recipe, the uniform distribution has two parameters:
61
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`.
62
64
63
65
```javascript
64
-
constshape= [ 3, 3 ];
65
-
consta=-10.0;
66
-
constb=10.0;
66
+
constshape= [ 3, 3 ];// 3x3
67
+
consta=-10.0;// minimum support
68
+
constb=10.0;// maximum support
67
69
68
70
constx=uniform( shape, a, b );
69
71
// returns <ndarray>
@@ -78,6 +80,16 @@ const xa = ndarray2array( x );
78
80
79
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.
80
82
83
+
> 💡 Tip
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`.
86
+
>
87
+
> ```javascript
88
+
>constx32=uniform( shape, a, b, {
89
+
>'dtype':'float32'
90
+
> });
91
+
>```
92
+
81
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.
82
94
83
95
### 3. \[Optional] Regenerate a sequence by using a seed
0 commit comments