pub trait RandomExt<S, A, D>{
// Required methods
fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>
where IdS: Distribution<S::Elem>,
S: DataOwned<Elem = A>,
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,
S: DataOwned<Elem = A>,
Sh: ShapeBuilder<Dim = D>;
fn sample_axis(
&self,
axis: Axis,
n_samples: usize,
strategy: SamplingStrategy,
) -> Array<A, D>
where A: Copy,
S: Data<Elem = A>,
D: RemoveAxis;
fn sample_axis_using<R>(
&self,
axis: Axis,
n_samples: usize,
strategy: SamplingStrategy,
rng: &mut R,
) -> Array<A, D>
where R: Rng + ?Sized,
A: Copy,
S: Data<Elem = A>,
D: RemoveAxis;
}Expand description
Extension trait for constructing n-dimensional arrays with random elements.
The default RNG is a fast automatically seeded rng (currently
rand::rngs::SmallRng, seeded from rand::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>
fn random<Sh, IdS>(shape: Sh, distribution: IdS) -> ArrayBase<S, D>
Create an array with shape dim with elements drawn from
distribution using the default RNG.
Panics if creation of the RNG fails, the number of elements overflows usize, or the axis has zero length.
use ndarray::Array;
use ndarray_rand::RandomExt;
use ndarray_rand::rand_distr::Uniform;
let a = Array::random((2, 5), Uniform::new(0., 10.).unwrap());
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,
S: DataOwned<Elem = A>,
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,
S: DataOwned<Elem = A>,
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 or the axis has zero length.
use ndarray::Array;
use ndarray_rand::RandomExt;
use ndarray_rand::rand::SeedableRng;
use ndarray_rand::rand_distr::Uniform;
use rand_isaac::isaac64::Isaac64Rng;
// Get a seeded random number generator for reproducibility (Isaac64 algorithm)
let seed = 42;
let mut rng = Isaac64Rng::seed_from_u64(seed);
// Generate a random array using `rng`
let a = Array::random_using((2, 5), Uniform::new(0., 10.).unwrap(), &mut rng);
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 sample_axis(
&self,
axis: Axis,
n_samples: usize,
strategy: SamplingStrategy,
) -> Array<A, D>
fn sample_axis( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, ) -> Array<A, D>
Sample n_samples lanes slicing along axis using the default RNG.
See RandomRefExt::sample_axis for additional information.
Sourcefn sample_axis_using<R>(
&self,
axis: Axis,
n_samples: usize,
strategy: SamplingStrategy,
rng: &mut R,
) -> Array<A, D>
fn sample_axis_using<R>( &self, axis: Axis, n_samples: usize, strategy: SamplingStrategy, rng: &mut R, ) -> Array<A, D>
Sample n_samples lanes slicing along axis using the specified RNG rng.
See RandomRefExt::sample_axis_using for additional information.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.