[][src]Trait ndarray_rand::RandomExt

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: ?Sized>(
        shape: Sh,
        distribution: IdS,
        rng: &mut R
    ) -> ArrayBase<S, D>
    where
        IdS: Distribution<S::Elem>,
        R: Rng,
        Sh: ShapeBuilder<Dim = D>
; }

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

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.

use ndarray::Array;
use ndarray_rand::RandomExt;
use ndarray_rand::rand_distr::Uniform;

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]]

fn random_using<Sh, IdS, R: ?Sized>(
    shape: Sh,
    distribution: IdS,
    rng: &mut R
) -> ArrayBase<S, D> where
    IdS: Distribution<S::Elem>,
    R: Rng,
    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.

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.), &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]]
Loading content...

Implementations on Foreign Types

impl<S, D> RandomExt<S, D> for ArrayBase<S, D> where
    S: DataOwned,
    D: Dimension
[src]

Loading content...

Implementors

Loading content...