1extern crate rand;
2
3use self::rand::{
4    distributions::{
5        Uniform,
6        Distribution,
7    },
8    Rng,
9};
10
11use array::Array;
12
13
14pub fn random_from_distribution<T, D>(distr: D, shape: Vec<i32>) -> Array<T>
21    where T: Copy, D: Distribution<T>
22{
23    let mut rng = rand::thread_rng();
24
25    let len: i32 = shape.iter().product();
26    let data: Vec<T> = rng.sample_iter(&distr).take(len as usize).collect();
27
28    return Array::new(data, shape);
29}
30
31#[inline]
39pub fn random_range<T>(from: T, to: T, shape: Vec<i32>) -> Array<T>
40    where T: rand::distributions::uniform::SampleUniform + Copy
41{
42    return random_from_distribution(Uniform::new::<T, T>(from, to), shape);
43}
44
45#[inline]
51pub fn random<T>(shape: Vec<i32>) -> Array<T>
52    where T: rand::distributions::uniform::SampleUniform + From<u8> + Copy
53{
54    return random_range::<T>(T::from(0), T::from(1), shape);
55}
56