|
6 | 6 | // option. This file may not be copied, modified, or distributed
|
7 | 7 | // except according to those terms.
|
8 | 8 |
|
9 |
| -//! Constructors for randomized arrays. `rand` integration for `ndarray`. |
| 9 | +//! Constructors for randomized arrays: `rand` integration for `ndarray`. |
10 | 10 | //!
|
11 | 11 | //! See [**`RandomExt`**](trait.RandomExt.html) for usage examples.
|
12 | 12 | //!
|
13 |
| -//! **Note:** `ndarray-rand` depends on `rand` 0.7. If you use any other items |
14 |
| -//! from `rand`, you need to specify a compatible version of `rand` in your |
15 |
| -//! `Cargo.toml`. If you want to use a RNG or distribution from another crate |
16 |
| -//! with `ndarray-rand`, you need to make sure that crate also depends on the |
17 |
| -//! correct version of `rand`. Otherwise, the compiler will return errors |
18 |
| -//! saying that the items are not compatible (e.g. that a type doesn't |
19 |
| -//! implement a necessary trait). |
| 13 | +//! ## Note |
| 14 | +//! |
| 15 | +//! `ndarray-rand` depends on [`rand` 0.7.0](https://docs.rs/rand/0.7.0/rand/). |
| 16 | +//! |
| 17 | +//! [`rand`](https://docs.rs/rand/0.7.0/rand/) and [`rand-distr`](https://docs.rs/rand_distr/0.2.1/rand_distr/) |
| 18 | +//! are re-exported as sub-modules, [`ndarray_rand::rand`](rand/index.html) |
| 19 | +//! and [`ndarray_rand::rand_distr`](rand_distr/index.html) respectively. |
| 20 | +//! Please rely on these submodules for guaranteed version compatibility. |
| 21 | +//! |
| 22 | +//! If you want to use a random number generator or distribution from another crate |
| 23 | +//! with `ndarray-rand`, you need to make sure that the other crate also depends on the |
| 24 | +//! same version of `rand`. Otherwise, the compiler will return errors saying |
| 25 | +//! that the items are not compatible (e.g. that a type doesn't implement a |
| 26 | +//! necessary trait). |
20 | 27 |
|
21 |
| -use rand::distributions::Distribution; |
22 |
| -use rand::rngs::SmallRng; |
23 |
| -use rand::{thread_rng, Rng, SeedableRng}; |
| 28 | +use crate::rand::distributions::Distribution; |
| 29 | +use crate::rand::rngs::SmallRng; |
| 30 | +use crate::rand::{thread_rng, Rng, SeedableRng}; |
24 | 31 |
|
25 | 32 | use ndarray::ShapeBuilder;
|
26 | 33 | use ndarray::{ArrayBase, DataOwned, Dimension};
|
27 | 34 |
|
| 35 | +/// [`rand`](https://docs.rs/rand/0.7.0/rand/), re-exported for convenience and version-compatibility. |
| 36 | +pub mod rand { |
| 37 | + pub use rand::*; |
| 38 | +} |
| 39 | + |
| 40 | +/// [`rand-distr`](https://docs.rs/rand_distr/0.2.1/rand_distr/), re-exported for convenience and version-compatibility. |
| 41 | +pub mod rand_distr { |
| 42 | + pub use rand_distr::*; |
| 43 | +} |
| 44 | + |
28 | 45 | /// Constructors for n-dimensional arrays with random elements.
|
29 | 46 | ///
|
30 | 47 | /// This trait extends ndarray’s `ArrayBase` and can not be implemented
|
31 | 48 | /// for other types.
|
32 | 49 | ///
|
33 | 50 | /// The default RNG is a fast automatically seeded rng (currently
|
34 |
| -/// [`rand::rngs::SmallRng`](https://docs.rs/rand/0.5/rand/rngs/struct.SmallRng.html) |
35 |
| -/// seeded from [`rand::thread_rng`](https://docs.rs/rand/0.5/rand/fn.thread_rng.html)). |
| 51 | +/// [`rand::rngs::SmallRng`](https://docs.rs/rand/0.7/rand/rngs/struct.SmallRng.html) |
| 52 | +/// seeded from [`rand::thread_rng`](https://docs.rs/rand/0.7/rand/fn.thread_rng.html)). |
36 | 53 | ///
|
37 | 54 | /// Note that `SmallRng` is cheap to initialize and fast, but it may generate
|
38 | 55 | /// low-quality random numbers, and reproducibility is not guaranteed. See its
|
|
50 | 67 | /// overflows usize.
|
51 | 68 | ///
|
52 | 69 | /// ```
|
53 |
| - /// extern crate rand; |
54 |
| - /// extern crate ndarray; |
55 |
| - /// extern crate ndarray_rand; |
56 |
| - /// |
57 |
| - /// use rand::distributions::Uniform; |
58 | 70 | /// use ndarray::Array;
|
59 | 71 | /// use ndarray_rand::RandomExt;
|
| 72 | + /// use ndarray_rand::rand_distr::Uniform; |
60 | 73 | ///
|
61 | 74 | /// # fn main() {
|
62 | 75 | /// let a = Array::random((2, 5), Uniform::new(0., 10.));
|
|
74 | 87 | /// `distribution`, using a specific Rng `rng`.
|
75 | 88 | ///
|
76 | 89 | /// ***Panics*** if the number of elements overflows usize.
|
| 90 | + /// |
| 91 | + /// ``` |
| 92 | + /// use ndarray::Array; |
| 93 | + /// use ndarray_rand::RandomExt; |
| 94 | + /// use ndarray_rand::rand::SeedableRng; |
| 95 | + /// use ndarray_rand::rand_distr::Uniform; |
| 96 | + /// use rand_isaac::isaac64::Isaac64Rng; |
| 97 | + /// |
| 98 | + /// # fn main() { |
| 99 | + /// // Get a seeded random number generator for reproducibility (Isaac64 algorithm) |
| 100 | + /// let seed = 42; |
| 101 | + /// let mut rng = Isaac64Rng::seed_from_u64(seed); |
| 102 | + /// |
| 103 | + /// // Generate a random array using `rng` |
| 104 | + /// let a = Array::random_using((2, 5), Uniform::new(0., 10.), &mut rng); |
| 105 | + /// println!("{:8.4}", a); |
| 106 | + /// // Example Output: |
| 107 | + /// // [[ 8.6900, 6.9824, 3.8922, 6.5861, 2.4890], |
| 108 | + /// // [ 0.0914, 5.5186, 5.8135, 5.2361, 3.1879]] |
| 109 | + /// # } |
77 | 110 | fn random_using<Sh, IdS, R>(shape: Sh, distribution: IdS, rng: &mut R) -> ArrayBase<S, D>
|
78 | 111 | where
|
79 | 112 | IdS: Distribution<S::Elem>,
|
@@ -109,16 +142,13 @@ where
|
109 | 142 | /// A wrapper type that allows casting f64 distributions to f32
|
110 | 143 | ///
|
111 | 144 | /// ```
|
112 |
| -/// extern crate rand; |
113 |
| -/// extern crate ndarray; |
114 |
| -/// extern crate ndarray_rand; |
115 |
| -/// |
116 |
| -/// use rand::distributions::Normal; |
117 | 145 | /// use ndarray::Array;
|
118 | 146 | /// use ndarray_rand::{RandomExt, F32};
|
| 147 | +/// use ndarray_rand::rand_distr::Normal; |
119 | 148 | ///
|
120 | 149 | /// # fn main() {
|
121 |
| -/// let a = Array::random((2, 5), F32(Normal::new(0., 1.))); |
| 150 | +/// let distribution_f64 = Normal::new(0., 1.).expect("Failed to create normal distribution"); |
| 151 | +/// let a = Array::random((2, 5), F32(distribution_f64)); |
122 | 152 | /// println!("{:8.4}", a);
|
123 | 153 | /// // Example Output:
|
124 | 154 | /// // [[ -0.6910, 1.1730, 1.0902, -0.4092, -1.7340],
|
|
0 commit comments