File tree Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Expand file tree Collapse file tree 1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change 1111//! The Cauchy distribution.
1212
1313use Rng ;
14- use distributions:: Distribution ;
14+ use distributions:: { Distribution , Uniform } ;
1515use std:: f64:: consts:: PI ;
1616
1717/// The Cauchy distribution `Cauchy(median, scale)`.
@@ -49,13 +49,15 @@ impl Cauchy {
4949
5050impl 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
You can’t perform that action at this time.
0 commit comments