Skip to content

Commit f8149ab

Browse files
committed
Binomial: Move distribution initialization out of loop
1 parent 6e82023 commit f8149ab

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/distributions/binomial.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! The binomial distribution.
1111
1212
use Rng;
13-
use distributions::Distribution;
13+
use distributions::{Distribution, Uniform};
1414

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

142+
let gen_u = Uniform::new(0., p4);
143+
let gen_v = Uniform::new(0., 1.);
144+
142145
loop {
143146
// Step 1: Generate `u` for selecting the region. If region 1 is
144147
// selected, generate a triangularly distributed variate.
145-
let u = rng.gen_range(0., p4);
146-
let mut v = rng.gen_range(0., 1.);
148+
let u = gen_u.sample(rng);
149+
let mut v = gen_v.sample(rng);
147150
if !(u > p1) {
148151
y = f64_to_i64(x_m - p1 * v + u);
149152
break;

0 commit comments

Comments
 (0)