File tree 3 files changed +25
-9
lines changed
3 files changed +25
-9
lines changed Original file line number Diff line number Diff line change @@ -93,10 +93,17 @@ impl Distribution<u64> for Binomial {
93
93
// f(x) ~ 1/(1+x^2)
94
94
let cauchy = Cauchy :: new ( 0.0 , 1.0 ) ;
95
95
loop {
96
- // draw from the Cauchy distribution
97
- let comp_dev = rng. sample ( cauchy) ;
98
- // shift the peak of the comparison distribution
99
- lresult = expected + sq * comp_dev;
96
+ let mut comp_dev: f64 ;
97
+ loop {
98
+ // draw from the Cauchy distribution
99
+ comp_dev = rng. sample ( cauchy) ;
100
+ // shift the peak of the comparison ditribution
101
+ lresult = expected + sq * comp_dev;
102
+ // repeat the drawing until we are in the range of possible values
103
+ if lresult >= 0.0 && lresult < float_n + 1.0 {
104
+ break ;
105
+ }
106
+ }
100
107
101
108
// the result should be discrete
102
109
lresult = lresult. floor ( ) ;
Original file line number Diff line number Diff line change @@ -50,7 +50,7 @@ impl Cauchy {
50
50
impl Distribution < f64 > for Cauchy {
51
51
fn sample < R : Rng + ?Sized > ( & self , rng : & mut R ) -> f64 {
52
52
// sample from [0, 1)
53
- let mut x: f64 = rng. gen :: < f64 > ( ) ;
53
+ let mut x = rng. gen :: < f64 > ( ) ;
54
54
// guard against the extremely unlikely case we get the invalid 0.5
55
55
while x == 0.5 {
56
56
x = rng. gen :: < f64 > ( ) ;
Original file line number Diff line number Diff line change @@ -75,12 +75,21 @@ impl Distribution<u64> for Poisson {
75
75
// we use the Cauchy distribution as the comparison distribution
76
76
// f(x) ~ 1/(1+x^2)
77
77
let cauchy = Cauchy :: new ( 0.0 , 1.0 ) ;
78
+
78
79
loop {
79
- // draw from the Cauchy distribution
80
- let comp_dev = rng. sample ( cauchy) ;
81
- // shift the peak of the comparison ditribution
82
- let mut result = self . sqrt_2lambda * comp_dev + self . lambda ;
80
+ let mut result;
81
+ let mut comp_dev;
83
82
83
+ loop {
84
+ // draw from the Cauchy distribution
85
+ comp_dev = rng. sample ( cauchy) ;
86
+ // shift the peak of the comparison ditribution
87
+ result = self . sqrt_2lambda * comp_dev + self . lambda ;
88
+ // repeat the drawing until we are in the range of possible values
89
+ if result >= 0.0 {
90
+ break ;
91
+ }
92
+ }
84
93
// now the result is a random variable greater than 0 with Cauchy distribution
85
94
// the result should be an integer value
86
95
result = result. floor ( ) ;
You can’t perform that action at this time.
0 commit comments