diff --git a/Cargo.toml b/Cargo.toml index 34150ca00..c03725d7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,8 @@ optional = true [dependencies] blas-sys = { version = "0.5", optional = true, default-features = false } - +asprim = "0.1" +rand = "0.3" # deprecated! use ndarray-rblas instead rblas = { version = "0.0.13", optional = true } diff --git a/src/impl_constructors.rs b/src/impl_constructors.rs index feb608013..2e43c39a9 100644 --- a/src/impl_constructors.rs +++ b/src/impl_constructors.rs @@ -8,6 +8,12 @@ //! Constructor methods for ndarray //! +extern crate rand; +use self::rand::distributions::{Normal, IndependentSample}; +use self::rand::{Rng}; + +extern crate asprim; +use self::asprim::AsPrim; use libnum; use imp_prelude::*; @@ -252,3 +258,28 @@ impl ArrayBase } } + +use super::NdFloat; + +impl ArrayBase + where S: DataOwned, + A: NdFloat+AsPrim, + D: Dimension, +{ + pub fn randn_rng(dim: D, rng: &mut R) -> ArrayBase + { + let size = dim.size_checked().expect("Shape too large: overflow in size"); + let normal = Normal::new(1.0, 0.0); + let iter = (0..size).map(|_| normal.ind_sample(rng).as_()); + let arr = ArrayBase::from_iter(iter); + arr.into_shape(dim).unwrap() + } + + pub fn randn(dim: D) -> ArrayBase + { + // Use the thread_rng once to seed a fast, non-crypto grade rng. + // To use a crypto quality RNG, use randn_rng directly. + let mut rng = rand::weak_rng(); + ArrayBase::::randn_rng(dim, &mut rng) + } +}