Skip to content

Commit 482633a

Browse files
committed
Fixed mistake in the domain getting passed into the tangent function
1 parent a391f2c commit 482633a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/distributions/cauchy.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! The Cauchy distribution.
1212
1313
use Rng;
14-
use distributions::Distribution;
14+
use distributions::{Distribution, Uniform};
1515
use std::f64::consts::PI;
1616

1717
/// The Cauchy distribution `Cauchy(median, scale)`.
@@ -49,13 +49,15 @@ impl Cauchy {
4949

5050
impl Distribution<f64> for Cauchy {
5151
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
52-
let mut x = rng.gen::<f64>();
53-
// guard against the extremely unlikely event we get 0 or 1 from the generator
54-
while x <= 0.0 || x >= 1.0 {
55-
x = rng.gen::<f64>();
52+
// sample from [0, 1]
53+
let closed01 = Uniform::new_inclusive(0.0, 1.0);
54+
let mut x: f64 = rng.sample(closed01);
55+
// guard against the extremely unlikely case we get the invalid 0.5
56+
while x == 0.5 {
57+
x = rng.sample(closed01);
5658
}
5759
// get standard cauchy random number
58-
let comp_dev = (2.0 * PI * x).tan();
60+
let comp_dev = (PI * x).tan();
5961
// shift and scale according to parameters
6062
let result = self.median + self.scale * comp_dev;
6163
result

0 commit comments

Comments
 (0)