Skip to content

Add rand_distr #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ num = "=0.2.0"
rand = { version = "=0.7.2", features = ["small_rng"] }
rand_chacha = "=0.2.1"
rand_pcg = "=0.2.0"
rand_distr = "=0.2.2"

# 正規表現
regex = "=1.3.1"
Expand Down
44 changes: 27 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ fn test_ascii() -> UnitResult {
// This code was taken from an example on: https://crates.io/crates/bitset-fixed
fn run_bitset_fixed() {
use bitset_fixed::BitSet;
use rand::distributions::Uniform;
use rand::prelude::*;
use rand_distr::Uniform;

let rng = StdRng::seed_from_u64(114514);
let dist = Uniform::from(0..2000);
Expand Down Expand Up @@ -367,22 +367,32 @@ fn test_rustc_hash() {
fn run_rand_family() -> UnitResult {
use rand::prelude::*;
use rand_chacha::ChaChaRng;
use rand_distr::{Normal, Uniform};
use rand_pcg::Pcg64Mcg;

let mut rng = SmallRng::from_rng(thread_rng())?;
let mean = calc_mean(&mut rng);
println!("SmallRng: mean = {:.4}", mean);
assert_eq!((mean * 10.0).round() as u32, 5);
macro_rules! test_mean {
($($rng:ident @ $distr:expr,)*) => {
$(
let mut rng = $rng::from_rng(thread_rng())?;
let mean = calc_mean(&mut rng, &$distr);
println!("{}: mean = {:.4}", stringify!($rng), mean);
assert_eq!((mean * 10.0).round() as u32, 5);
)*
};
}

let distr_normal = Normal::new(0.5, 1.0).unwrap();
let distr_uniform = Uniform::from(0.0..1.0);

let mut rng = Pcg64Mcg::from_rng(thread_rng())?;
let mean = calc_mean(&mut rng);
println!("ChaChaRng: mean = {:.4}", mean);
assert_eq!((mean * 10.0).round() as u32, 5);
test_mean! {
SmallRng @ distr_uniform,
ChaChaRng @ distr_uniform,
Pcg64Mcg @ distr_uniform,

let mut rng = ChaChaRng::from_rng(thread_rng())?;
let mean = calc_mean(&mut rng);
println!("Pcg64Mcg: mean = {:.4}", mean);
assert_eq!((mean * 10.0).round() as u32, 5);
SmallRng @ distr_normal,
ChaChaRng @ distr_normal,
Pcg64Mcg @ distr_normal,
}

Ok(())
}
Expand All @@ -392,13 +402,13 @@ fn test_rand_family() -> UnitResult {
run_rand_family()
}

fn calc_mean(rng: &mut impl rand::Rng) -> f64 {
// to see `impl Rng for &mut R where R: Rng`, which prevent moving in `rng.sample_iter()`
use rand::Rng as _;
fn calc_mean<D: rand_distr::Distribution<f64>>(rng: &mut impl rand::Rng, distr: &D) -> f64 {
use rand::Rng;

const ITERATIONS: usize = 10000;

// the stardard distribution for f64 generates a random rumber in interval [0, 1)
rng.sample_iter::<f64, _>(&rand::distributions::Standard)
rng.sample_iter::<f64, _>(distr)
.take(ITERATIONS)
.enumerate()
// calculate the mean iteratively. https://stackoverflow.com/a/1934266
Expand Down