Changing poisson sampling to sample from time::Duration

This commit is contained in:
Jedrzej Stuczynski
2020-01-31 16:24:14 +00:00
parent 94479cdeec
commit 12fc906542
+4 -3
View File
@@ -1,9 +1,10 @@
use rand_distr::{Distribution, Exp};
use std::time;
pub fn sample(average_delay: f64) -> f64 {
pub fn sample_from_duration(average_duration: time::Duration) -> time::Duration {
// this is our internal code used by our traffic streams
// the error is only thrown if average delay is less than 0, which will never happen
// so call to unwrap is perfectly safe here
let exp = Exp::new(1.0 / average_delay).unwrap();
exp.sample(&mut rand::thread_rng())
let exp = Exp::new(1.0 / average_duration.as_nanos() as f64).unwrap();
time::Duration::from_nanos(exp.sample(&mut rand::thread_rng()).round() as u64)
}