rand-array-iid

Crates.iorand-array-iid
lib.rsrand-array-iid
version0.2.0
sourcesrc
created_at2020-10-06 19:54:41.266149
updated_at2020-10-14 21:03:24.252408
descriptionCreate arrays whose elements are independently identically distributed.
homepage
repositoryhttps://github.com/krtab/rand-array-iid
max_upload_size
id296698
size13,341
Arthur Carcano (krtab)

documentation

README

Rand-array-iid

Build Status Crates.io Docs

A rust crate to create arrays whose elements are independently identically distributed.

Install

[dependencies]
rand-array-iid = "0.1.0"

Examples

An array of normally distributed scalars

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);

An array of 3D vectors sampled from the unit sphere

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);

Why only arrays?

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()
}
Commit count: 35

cargo fmt