-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2018 Developers of the Rand project. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#[macro_use] | ||
extern crate average; | ||
extern crate rand; | ||
extern crate core; | ||
|
||
use average::Histogram; | ||
use rand::prelude::*; | ||
|
||
// 65355 = 3 * 5 * 4357 produced the zone with largest deviation from a multiple | ||
// of itself (deviation = 32579). | ||
const RANGE: u16 = 65355; | ||
// We want a reasonably small number of bins which is a divisor of the range | ||
const N_BINS: usize = 15; | ||
// Even with 100M samples no pattern is obvious! | ||
const N_SAMPLES: u32 = 1_000_000; | ||
const SCALE: f64 = 0.05; // adjust with N_SAMPLES to scale bar lengths | ||
define_histogram!(hist, 15); | ||
use hist::Histogram as Histogram15; | ||
|
||
#[test] | ||
fn uniform() { | ||
// 65175 = 3*25*11*79 has the largest difference in | ||
let distr = rand::distributions::Uniform::new(0, RANGE); | ||
let mut rng = rand::rngs::SmallRng::from_entropy(); | ||
let mut h = Histogram15::with_const_width(0., RANGE as f64); | ||
for _ in 0..N_SAMPLES { | ||
let x = distr.sample(&mut rng); | ||
h.add(x as f64).unwrap(); | ||
} | ||
|
||
let expected = (N_SAMPLES as f64) / (N_BINS as f64); | ||
let spaces = " "; | ||
let stars = "***********************************************************"; | ||
assert_eq!(spaces.len(), stars.len()); | ||
let len = stars.len() as isize; | ||
println!("Deviations per bin:"); | ||
for &b in h.bins() { | ||
let diff = (b as f64) - expected; | ||
let n = (diff * SCALE).round() as isize; | ||
if n < 0 { | ||
println!("{}\t{}{}|", diff.abs(), &spaces[0..((len + n) as usize)], &stars[0..(-n as usize)]); | ||
} else { | ||
println!("{}\t{}|{}", diff.abs(), spaces, &stars[0..(n as usize)]); | ||
} | ||
} | ||
// TODO: results are too variable to make any useful assertions? | ||
// assert!(false); | ||
} |