Trait ndarray_rand::RandomExt
source · pub trait RandomExt<S, D>where
S: DataOwned,
D: Dimension,{
fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>
where
IdS: Distribution<S::Elem>,
Sh: ShapeBuilder<Dim = D>;
fn random_using<Sh, IdS, R>(
shape: Sh,
distribution: IdS,
rng: &mut R
) -> ArrayBase<S, D>
where
IdS: Distribution<S::Elem>,
R: Rng + ?Sized,
Sh: ShapeBuilder<Dim = D>;
}
Expand description
Constructors for n-dimensional arrays with random elements.
This trait extends ndarray’s ArrayBase
and can not be implemented
for other types.
The default RNG is a fast automatically seeded rng (currently
rand::rngs::SmallRng
seeded from rand::thread_rng
).
Note that SmallRng
is cheap to initialize and fast, but it may generate
low-quality random numbers, and reproducibility is not guaranteed. See its
documentation for information. You can select a different RNG with
.random_using()
.
Required Methods
sourcefn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>where
IdS: Distribution<S::Elem>,
Sh: ShapeBuilder<Dim = D>,
fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>where
IdS: Distribution<S::Elem>,
Sh: ShapeBuilder<Dim = D>,
Create an array with shape dim
with elements drawn from
distribution
using the default RNG.
Panics if creation of the RNG fails or if the number of elements overflows usize.
extern crate rand;
extern crate ndarray;
extern crate ndarray_rand;
use rand::distributions::Uniform;
use ndarray::Array;
use ndarray_rand::RandomExt;
let a = Array::random((2, 5), Uniform::new(0., 10.));
println!("{:8.4}", a);
// Example Output:
// [[ 8.6900, 6.9824, 3.8922, 6.5861, 2.4890],
// [ 0.0914, 5.5186, 5.8135, 5.2361, 3.1879]]
sourcefn random_using<Sh, IdS, R>(
shape: Sh,
distribution: IdS,
rng: &mut R
) -> ArrayBase<S, D>where
IdS: Distribution<S::Elem>,
R: Rng + ?Sized,
Sh: ShapeBuilder<Dim = D>,
fn random_using<Sh, IdS, R>(
shape: Sh,
distribution: IdS,
rng: &mut R
) -> ArrayBase<S, D>where
IdS: Distribution<S::Elem>,
R: Rng + ?Sized,
Sh: ShapeBuilder<Dim = D>,
Create an array with shape dim
with elements drawn from
distribution
, using a specific Rng rng
.
Panics if the number of elements overflows usize.