Skip to content

Commit e2adefc

Browse files
authored
Merge pull request #19 from statiolake/ja-all-enabled
Add rand_distr
2 parents 9f06fe9 + 6101fb7 commit e2adefc

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

Cargo.lock

+29-29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ num = "=0.2.0"
6060
rand = { version = "=0.7.2", features = ["small_rng"] }
6161
rand_chacha = "=0.2.1"
6262
rand_pcg = "=0.2.0"
63+
rand_distr = "=0.2.2"
6364

6465
# 正規表現
6566
regex = "=1.3.1"

src/main.rs

+27-17
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ fn test_ascii() -> UnitResult {
252252
// This code was taken from an example on: https://crates.io/crates/bitset-fixed
253253
fn run_bitset_fixed() {
254254
use bitset_fixed::BitSet;
255-
use rand::distributions::Uniform;
256255
use rand::prelude::*;
256+
use rand_distr::Uniform;
257257

258258
let rng = StdRng::seed_from_u64(114514);
259259
let dist = Uniform::from(0..2000);
@@ -367,22 +367,32 @@ fn test_rustc_hash() {
367367
fn run_rand_family() -> UnitResult {
368368
use rand::prelude::*;
369369
use rand_chacha::ChaChaRng;
370+
use rand_distr::{Normal, Uniform};
370371
use rand_pcg::Pcg64Mcg;
371372

372-
let mut rng = SmallRng::from_rng(thread_rng())?;
373-
let mean = calc_mean(&mut rng);
374-
println!("SmallRng: mean = {:.4}", mean);
375-
assert_eq!((mean * 10.0).round() as u32, 5);
373+
macro_rules! test_mean {
374+
($($rng:ident @ $distr:expr,)*) => {
375+
$(
376+
let mut rng = $rng::from_rng(thread_rng())?;
377+
let mean = calc_mean(&mut rng, &$distr);
378+
println!("{}: mean = {:.4}", stringify!($rng), mean);
379+
assert_eq!((mean * 10.0).round() as u32, 5);
380+
)*
381+
};
382+
}
383+
384+
let distr_normal = Normal::new(0.5, 1.0).unwrap();
385+
let distr_uniform = Uniform::from(0.0..1.0);
376386

377-
let mut rng = Pcg64Mcg::from_rng(thread_rng())?;
378-
let mean = calc_mean(&mut rng);
379-
println!("ChaChaRng: mean = {:.4}", mean);
380-
assert_eq!((mean * 10.0).round() as u32, 5);
387+
test_mean! {
388+
SmallRng @ distr_uniform,
389+
ChaChaRng @ distr_uniform,
390+
Pcg64Mcg @ distr_uniform,
381391

382-
let mut rng = ChaChaRng::from_rng(thread_rng())?;
383-
let mean = calc_mean(&mut rng);
384-
println!("Pcg64Mcg: mean = {:.4}", mean);
385-
assert_eq!((mean * 10.0).round() as u32, 5);
392+
SmallRng @ distr_normal,
393+
ChaChaRng @ distr_normal,
394+
Pcg64Mcg @ distr_normal,
395+
}
386396

387397
Ok(())
388398
}
@@ -392,13 +402,13 @@ fn test_rand_family() -> UnitResult {
392402
run_rand_family()
393403
}
394404

395-
fn calc_mean(rng: &mut impl rand::Rng) -> f64 {
396-
// to see `impl Rng for &mut R where R: Rng`, which prevent moving in `rng.sample_iter()`
397-
use rand::Rng as _;
405+
fn calc_mean<D: rand_distr::Distribution<f64>>(rng: &mut impl rand::Rng, distr: &D) -> f64 {
406+
use rand::Rng;
407+
398408
const ITERATIONS: usize = 10000;
399409

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

0 commit comments

Comments
 (0)