Skip to content

Instantly share code, notes, and snippets.

@JiaruiTracy
Created September 26, 2017 22:21
Show Gist options
  • Save JiaruiTracy/c5bc011f1918828676704ec8e4208e4c to your computer and use it in GitHub Desktop.
Save JiaruiTracy/c5bc011f1918828676704ec8e4208e4c to your computer and use it in GitHub Desktop.
The class to generate random number obey exponential distribution
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