Crates.io | rand-array-iid |
lib.rs | rand-array-iid |
version | 0.2.0 |
source | src |
created_at | 2020-10-06 19:54:41.266149 |
updated_at | 2020-10-14 21:03:24.252408 |
description | Create arrays whose elements are independently identically distributed. |
homepage | |
repository | https://github.com/krtab/rand-array-iid |
max_upload_size | |
id | 296698 |
size | 13,341 |
A rust crate to create arrays whose elements are independently identically distributed.
[dependencies]
rand-array-iid = "0.1.0"
use rand_array_iid::IIDDistr;
use rand_distr::Distribution;
use rand_distr::StandardNormal;
let distr = IIDDistr::new(StandardNormal);
let mut rng = rand::thread_rng();
// Each of x element is distributed according to StandardNormal.
let x : [f64; 10] = distr.sample(&mut rng);
use rand_array_iid::IIDDistr;
use rand_distr::Distribution;
use rand_distr::UnitSphere;
let distr = IIDDistr::new(UnitSphere);
let mut rng = rand::thread_rng();
// Each of x element is sampled uniformly from the surface of the 3D unit sphere.
let x : [[f64; 3]; 10] = distr.sample(&mut rng);
Collections such as Vec
that implement std::iter::FromIterator
bear
no information on their size in their type, hence the distribution would have
to be restricted to a given size. They can also be sampled as follow:
use rand_distr::Distribution;
use rand::Rng;
fn sample_iid<D,R, Col>(dist: D, rng: &mut R, n: usize) -> Col
where
R: Rng + ?Sized,
Col: std::iter::IntoIterator,
Col: std::iter::FromIterator<<Col as std::iter::IntoIterator>::Item>,
D: Distribution<<Col as std::iter::IntoIterator>::Item>,
{
dist.sample_iter(rng).take(n).collect()
}