https://github.com/krtab/rand-array-iid
A rust crate to create arrays whose elements are independently identically distributed.
https://github.com/krtab/rand-array-iid
Last synced: about 1 year ago
JSON representation
A rust crate to create arrays whose elements are independently identically distributed.
- Host: GitHub
- URL: https://github.com/krtab/rand-array-iid
- Owner: krtab
- License: mit
- Created: 2020-10-05T17:23:28.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-10-14T21:01:26.000Z (over 5 years ago)
- Last Synced: 2025-04-02T05:52:31.323Z (about 1 year ago)
- Language: Rust
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Rand-array-iid
[](https://travis-ci.org/krtab/rand-array-iid)
[](https://crates.io/crates/rand-array-iid)
[](https://docs.rs/rand-array-iid/)
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
```rust
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
```rust
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:
```rust
use rand_distr::Distribution;
use rand::Rng;
fn sample_iid(dist: D, rng: &mut R, n: usize) -> Col
where
R: Rng + ?Sized,
Col: std::iter::IntoIterator,
Col: std::iter::FromIterator<::Item>,
D: Distribution<::Item>,
{
dist.sample_iter(rng).take(n).collect()
}
```