#rand #random #array #arr

no-std rand-array-iid

Create arrays whose elements are independently identically distributed

3 unstable releases

0.2.0 Oct 14, 2020
0.1.1 Oct 13, 2020
0.1.0 Oct 6, 2020

#15 in #distr

49 downloads per month

MIT license

12KB
122 lines

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()
}

Dependencies

~1.5MB
~26K SLoC