Skip to content

Commit

Permalink
Binomial: Move distribution initialization out of loop
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed Mar 29, 2019
1 parent 6e82023 commit f8149ab
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/distributions/binomial.rs
Expand Up @@ -10,7 +10,7 @@
//! The binomial distribution.

use Rng;
use distributions::Distribution;
use distributions::{Distribution, Uniform};

/// The binomial distribution `Binomial(n, p)`.
///
Expand Down Expand Up @@ -139,11 +139,14 @@ impl Distribution<u64> for Binomial {
// return value
let mut y: i64;

let gen_u = Uniform::new(0., p4);
let gen_v = Uniform::new(0., 1.);

loop {
// Step 1: Generate `u` for selecting the region. If region 1 is
// selected, generate a triangularly distributed variate.
let u = rng.gen_range(0., p4);
let mut v = rng.gen_range(0., 1.);
let u = gen_u.sample(rng);
let mut v = gen_v.sample(rng);
if !(u > p1) {
y = f64_to_i64(x_m - p1 * v + u);
break;
Expand Down

0 comments on commit f8149ab

Please sign in to comment.