Created
September 26, 2017 22:21
-
-
Save JiaruiTracy/c5bc011f1918828676704ec8e4208e4c to your computer and use it in GitHub Desktop.
The class to generate random number obey exponential distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package simulation.util; | |
import java.text.DecimalFormat; | |
/* | |
* The numbers generated by java Random is using Uniform Distribution. | |
* But recently, when I work on Timed-Petri net, it is better to use Exponential Distribution. | |
* Thus, I write a class which is like a exponential calculator. | |
* | |
* INPUT: lamda | |
* OUTPUT: random number based on Exponential Distribution | |
*/ | |
public class ExponentialDistribution { | |
public double getExponential(double time) { | |
double x, z; | |
double lamda = 1 / time; | |
z = Math.random(); | |
x = (-(1 / lamda) * Math.log(z)); | |
x = format(x); | |
return x; | |
} | |
public double format(double x) { | |
return Double.parseDouble(new DecimalFormat("##.##").format(x)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment