Skip to content

Commit f514fd6

Browse files
committed
Zipf: Simplify calculation of ratio
This should improve performance slightly.
1 parent 139e898 commit f514fd6

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

rand_distr/src/zipf.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,13 @@ where F: Float, Standard: Distribution<F>
187187
loop {
188188
let inv_b = self.inv_cdf(rng.sample(Standard));
189189
let x = (inv_b + one).floor();
190-
let numer = x.powf(-self.s);
191-
let denom = if x <= one {
192-
one / self.t
193-
} else {
194-
inv_b.powf(-self.s) / self.t
190+
let mut ratio = x.powf(-self.s) * self.t;
191+
if x > one {
192+
ratio = ratio * inv_b.powf(self.s)
195193
};
196194

197195
let y = rng.sample(Standard);
198-
if y < numer / denom {
196+
if y < ratio {
199197
return x;
200198
}
201199
}

0 commit comments

Comments
 (0)