Skip to content

Commit

Permalink
Add uniformity test for 65355u16
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed May 29, 2019
1 parent 3b02c5c commit f687253
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ rand_isaac = { path = "rand_isaac", version = "0.1" }
rand_chacha = { path = "rand_chacha", version = "0.2" }
rand_xorshift = { path = "rand_xorshift", version = "0.1" }
rand_distr = { path = "rand_distr", version = "0.1" }
# Histogram implementation for testing uniformity
average = "0.9.2"

[build-dependencies]
autocfg = "0.1"
Expand Down
56 changes: 56 additions & 0 deletions tests/uniformity.rs
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);
}

0 comments on commit f687253

Please sign in to comment.