@@ -124,15 +124,15 @@ public double ChiSquared(int df) {
124124 if (df <= 0 ) {
125125 throw new IllegalArgumentException ("df (" + df + ") is not greater than 0" );
126126 }
127- return this .Gamma (df / 2.0 , 2 );
127+ return this .Gamma (df * 0.5 , 2 );
128128 }
129129
130130 /**
131131 * Not documented yet.
132132 * @return A 64-bit floating-point number.
133133 */
134134 public double Exponential () {
135- return -Math .log (this .NonZeroUniform ());
135+ return -Math .log (1.0 - this .Uniform ());
136136 }
137137
138138 /**
@@ -154,9 +154,8 @@ public double Gamma(double a, double b) {
154154 * @return A 64-bit floating-point number.
155155 */
156156 public double Gamma (double a ) {
157- if (a <= -1 ) {
158- throw new IllegalArgumentException ("a (" + a + ") is not greater than " +
159- (-1 ));
157+ if (a <= 0 ) {
158+ throw new IllegalArgumentException ("a (" + a + ") is not greater than 0" );
160159 }
161160 double v , x , u , x2 , d , c ;
162161 d = (a < 1 ? 1 + a : a ) - (1.0 / 3.0 );
@@ -166,9 +165,9 @@ public double Gamma(double a) {
166165 x = this .Normal ();
167166 v = Math .pow ((c * x ) + 1 , 3 );
168167 } while (v <= 0 );
169- u = this .Uniform ();
170- x2 = Math . pow ( x , 2 ) ;
171- } while (u >= 1 - (0.0331 * x2 * x2 ) &&
168+ u = 1.0 - this .Uniform ();
169+ x2 = x * x ;
170+ } while (u >= 1 - (0.0331 * x2 * x2 ) &&
172171 Math .log (u ) >= (0.5 * x2 ) + (d * (1 - v + Math .log (v ))));
173172 if (a < 1 ) {
174173 return d * v * Math .exp (this .Exponential () / -a );
@@ -245,7 +244,7 @@ public int Hypergeometric(int trials, int ones, int count) {
245244 * @return A 64-bit floating-point number.
246245 */
247246 public double LogNormal (double mean , double sd ) {
248- return Math .exp (( this .Normal () * sd ) + mean );
247+ return Math .exp (this .Normal (mean , sd ));
249248 }
250249
251250 /**
@@ -334,7 +333,7 @@ public double Normal() {
334333 return this .valueLastNormal ;
335334 }
336335 }
337- double x = this .NonZeroUniform ();
336+ double x = 1.0 - this .Uniform ();
338337 double y = this .Uniform ();
339338 double s = Math .sqrt (-2 * Math .log (x ));
340339 double t = 2 * Math .PI * y ;
@@ -370,13 +369,15 @@ public int Poisson(double mean) {
370369 ") is less than 0" );
371370 }
372371 double l = Math .exp (-mean );
373- int k = 0 ;
374- double p = 0 ;
375- do {
376- ++k ;
372+ int count = - 1 ;
373+ double p = 1. 0 ;
374+ while ( true ) {
375+ ++count ;
377376 p *= this .Uniform ();
378- } while (p > l );
379- return k - 1 ;
377+ if (p <= l ) {
378+ return count ;
379+ }
380+ }
380381 }
381382
382383 /**
@@ -409,7 +410,7 @@ public double Uniform(double max) {
409410 * @return A 64-bit floating-point number.
410411 */
411412 public double Uniform () {
412- return this .UniformLong (9007199254740992L )/ 9007199254740992.0 ;
413+ return this .UniformLong (9007199254740992L ) / 9007199254740992.0 ;
413414 }
414415
415416 /**
@@ -418,7 +419,7 @@ public double Uniform() {
418419 * @return A 32-bit floating-point number.
419420 */
420421 public double UniformSingle () {
421- return this .UniformInt (16777216 )/ 16777216.0f ;
422+ return this .UniformInt (16777216 ) / 16777216.0f ;
422423 }
423424
424425 /**
@@ -504,7 +505,7 @@ public int UniformInt(int maxExclusive) {
504505 throw new IllegalArgumentException ("maxExclusive (" + maxExclusive +
505506 ") is less than 0" );
506507}
507- if (maxExclusive <= 0 ) {
508+ if (maxExclusive <= 1 ) {
508509 return 0 ;
509510}
510511 byte [] b = new byte [4 ];
@@ -534,8 +535,7 @@ public int UniformInt(int maxExclusive) {
534535 return ib ;
535536 }
536537 int maxexc ;
537- maxexc = (maxExclusive <= 100 ) ? 2147483600 :
538- ((Integer .MAX_VALUE / maxExclusive ) * maxExclusive );
538+ maxexc = (Integer .MAX_VALUE / maxExclusive ) * maxExclusive ;
539539 while (true ) {
540540 this .valueIrg .GetBytes (b , 0 , 4 );
541541 ib = b [0 ] & 0xff ;
@@ -585,21 +585,4 @@ public long UniformLong(long maxExclusive) {
585585 }
586586 }
587587 }
588-
589- private double NonZeroUniform () {
590- byte [] b = new byte [7 ];
591- long lb = 0 ;
592- do {
593- this .valueIrg .GetBytes (b , 0 , 7 );
594- lb = b [0 ] & 0xffL ;
595- lb |= (b [1 ] & 0xffL ) << 8 ;
596- lb |= (b [2 ] & 0xffL ) << 16 ;
597- lb |= (b [3 ] & 0xffL ) << 24 ;
598- lb |= (b [4 ] & 0xffL ) << 32 ;
599- lb |= (b [5 ] & 0xffL ) << 40 ;
600- lb |= (b [6 ] & 0xfL ) << 48 ;
601- } while (lb == 0 );
602- lb |= 0x3ffL << 52 ;
603- return Double .longBitsToDouble (lb ) - 1.0 ;
604- }
605588 }
0 commit comments