package jp.rel.jmtrandom;

public class Random {
	
	private final MTRandInterface mtrand;
	
	private int normal_sw;
	private double normal_save;

	public Random(long seed) {
		this.mtrand = new MT64(seed);
	}

	public Random(MTRandInterface mtrand) {
		this.mtrand = mtrand;
	}

	public double nextUnif() {
		return mtrand.genRandRealo0o1();
	}

	public double nextUnif(double low, double high) {
		return (high - low) *  mtrand.genRandRealo0o1() + low;
	}

	public double nextExp(double beta) {
		return -Math.log(this.nextUnif()) / beta;
	}

	/* Normal distribution */

	public double nextNormal() {
		if (normal_sw == 0) {
			double t = Math.sqrt(-2 * Math.log(this.nextUnif()));
			double u = Math.PI * this.nextUnif();
			normal_save = t * Math.sin(u);
			normal_sw = 1;
			return t * Math.cos(u);
		} else {
			normal_sw = 0;
			return normal_save;
		}
	}

	public double nextNormal(double mu, double sig) {
		return mu + sig * this.nextNormal();
	}

	public double nextTruncNormal(double mu, double sig) {
		double tmp;
		do {
			tmp = this.nextNormal(mu, sig);
		} while (tmp > 0.0);
		return tmp;
	}

	public double nextLogNormal(double mu, double sig) {
		return Math.exp(nextNormal(mu, sig));
	}

	/* Logistic distribution */

	public double nextLogistic() {
    	double x = this.nextUnif();
    	return Math.log(x/(1.0-x));
    }

    public double nextLogistic(double mu, double phi) {
		return mu + phi * this.nextLogistic();
    }

    public double nextTruncLogistic(double mu, double phi) {
		double tmp;
		do {
			tmp = this.nextLogistic(mu, phi);
		} while (tmp > 0.0);
		return tmp;
    }

	public double nextLogLogistic(double mu, double phi) {
		return Math.exp(nextLogistic(mu, phi));
	}
	
	/* Extreme distribution */

	// TODO
	
	/* Weibull distribution */

	public double nextWeibull(double alpha, double beta) {
		return Math.pow(-Math.log(this.nextUnif()) / beta, 1.0/alpha);
	}

}

@@ -370,19 +370,19 @@
 (double 
-low
+min
 , double
@@ -382,20 +382,19 @@
  double 
-high
+max
 ) %7B%0A%09%09re
@@ -403,18 +403,17 @@
 rn (
-high - low
+max - min
 ) * 
@@ -444,11 +444,11 @@
 ) + 
-low
+min
 ;%0A%09%7D
@@ -475,28 +475,28 @@
 tExp(double 
-beta
+rate
 ) %7B%0A%09%09return
@@ -525,20 +525,20 @@
 if()) / 
-beta
+rate
 ;%0A%09%7D%0A%0A%09/
@@ -880,33 +880,35 @@
 tNormal(double m
-u
+ean
 , double sig) %7B%0A
@@ -893,34 +893,33 @@
 e mean, double s
-ig
+d
 ) %7B%0A%09%09return mu 
@@ -920,15 +920,16 @@
 rn m
-u
+ean
  + s
-ig
+d
  * t
@@ -981,33 +981,35 @@
 cNormal(double m
-u
+ean
 , double sig) %7B%0A
@@ -998,26 +998,25 @@
 an, double s
-ig
+d
 ) %7B%0A%09%09double
@@ -1054,22 +1054,23 @@
 Normal(m
-u, sig
+ean, sd
 );%0A%09%09%7D w
@@ -1134,33 +1134,35 @@
 gNormal(double m
-u
+ean
 , double sig) %7B%0A
@@ -1155,18 +1155,17 @@
 double s
-ig
+d
 ) %7B%0A%09%09re
@@ -1194,14 +1194,15 @@
 al(m
-u, sig
+ean, sd
 ));%0A
@@ -1370,34 +1370,35 @@
 Logistic(double 
-mu
+loc
 , double phi) %7B%0A
@@ -1382,35 +1382,37 @@
 ble loc, double 
-phi
+scale
 ) %7B%0A%09%09return mu 
@@ -1412,16 +1412,19 @@
 urn 
-mu + phi
+loc + scale
  * t
@@ -1485,34 +1485,35 @@
 Logistic(double 
-mu
+loc
 , double phi) %7B%0A
@@ -1501,27 +1501,29 @@
 loc, double 
-phi
+scale
 ) %7B%0A%09%09double
@@ -1562,23 +1562,26 @@
 ogistic(
-mu, phi
+loc, scale
 );%0A%09%09%7D w
@@ -1657,18 +1657,19 @@
 (double 
-mu
+loc
 , double
@@ -1669,19 +1669,21 @@
  double 
-phi
+scale
 ) %7B%0A%09%09re
@@ -1713,15 +1713,18 @@
 tic(
-mu, phi
+loc, scale
 ));%0A
@@ -1788,32 +1788,33 @@
 distribution */%0A
+%09
 %0A%09public double 
@@ -1832,21 +1832,21 @@
 (double 
-alpha
+shape
 , double
@@ -1846,20 +1846,21 @@
  double 
-beta
+scale
 ) %7B%0A%09%09re
@@ -1859,24 +1859,32 @@
 ) %7B%0A%09%09return
+ scale *
  Math.pow(-M
@@ -1911,26 +1911,19 @@
 f())
- / beta
 , 1.0/
-alpha
+shape
 );%0A%09

change dist parameters