Probability distributions implemented in CloudSim replaced by similar functions…

Probability distributions implemented in CloudSim replaced by similar functions from Apache math commons.
parent 29c3edf5
......@@ -8,7 +8,7 @@
package org.cloudbus.cloudsim.distributions;
import java.util.Random;
import org.apache.commons.math3.distribution.ExponentialDistribution;
/**
* An exponential number generator.
......@@ -19,10 +19,8 @@ import java.util.Random;
public class ExponentialDistr implements ContinuousDistribution {
/** The num gen. */
private final Random numGen;
private final ExponentialDistribution numGen;
/** The mean. */
private final double mean;
/**
* Creates a new exponential number generator.
......@@ -31,11 +29,8 @@ public class ExponentialDistr implements ContinuousDistribution {
* @param mean the mean for the distribution.
*/
public ExponentialDistr(long seed, double mean) {
if (mean <= 0.0) {
throw new IllegalArgumentException("Mean must be greater than 0.0");
}
numGen = new Random(seed);
this.mean = mean;
this(mean);
numGen.reseedRandomGenerator(seed);
}
/**
......@@ -44,11 +39,7 @@ public class ExponentialDistr implements ContinuousDistribution {
* @param mean the mean for the distribution.
*/
public ExponentialDistr(double mean) {
if (mean <= 0.0) {
throw new IllegalArgumentException("Mean must be greated than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.mean = mean;
numGen = new ExponentialDistribution(mean);
}
/**
......@@ -58,7 +49,7 @@ public class ExponentialDistr implements ContinuousDistribution {
*/
@Override
public double sample() {
return -mean * Math.log(numGen.nextDouble());
return numGen.sample();
}
}
......@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random;
import org.apache.commons.math3.distribution.GammaDistribution;
/**
* The Class GammaDistr.
*
......@@ -20,13 +22,7 @@ import java.util.Random;
public class GammaDistr implements ContinuousDistribution {
/** The num gen. */
private final Random numGen;
/** The alpha. */
private final int alpha;
/** The beta. */
private final double beta;
private final GammaDistribution numGen;
/**
* Instantiates a new gamma distr.
......@@ -35,14 +31,9 @@ public class GammaDistr implements ContinuousDistribution {
* @param alpha the alpha
* @param beta the beta
*/
public GammaDistr(Random seed, int alpha, double beta) {
if (alpha <= 0 || beta <= 0.0) {
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0");
}
numGen = seed;
this.alpha = alpha;
this.beta = beta;
public GammaDistr(Random seed, int shape, double scale) {
this(shape, scale);
numGen.reseedRandomGenerator(seed.nextLong());
}
/**
......@@ -51,14 +42,8 @@ public class GammaDistr implements ContinuousDistribution {
* @param alpha the alpha
* @param beta the beta
*/
public GammaDistr(int alpha, double beta) {
if (alpha <= 0 || beta <= 0.0) {
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.alpha = alpha;
this.beta = beta;
public GammaDistr(int shape, double scale) {
numGen = new GammaDistribution(shape, scale);
}
/*
......@@ -67,12 +52,7 @@ public class GammaDistr implements ContinuousDistribution {
*/
@Override
public double sample() {
double sum = 0.0;
for (int i = 0; i < alpha; i++) {
sum += Math.log(numGen.nextDouble());
}
return -beta * sum;
return numGen.sample();
}
}
......@@ -10,6 +10,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random;
import org.apache.commons.math3.distribution.LogNormalDistribution;
/**
* The Class LognormalDistr.
*
......@@ -18,14 +20,10 @@ import java.util.Random;
*/
public class LognormalDistr implements ContinuousDistribution {
/** The num gen. */
private final Random numGen;
/** The mean. */
private final double mean;
private final LogNormalDistribution numGen;
/** The dev. */
private final double dev;
/**
* Instantiates a new lognormal distr.
......@@ -34,14 +32,9 @@ public class LognormalDistr implements ContinuousDistribution {
* @param mean the mean
* @param dev the dev
*/
public LognormalDistr(Random seed, double mean, double dev) {
if (mean <= 0.0 || dev <= 0.0) {
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0");
}
numGen = seed;
this.mean = mean;
this.dev = dev;
public LognormalDistr(Random seed, double shape, double scale) {
this(shape, scale);
numGen.reseedRandomGenerator(seed.nextLong());
}
/**
......@@ -50,14 +43,8 @@ public class LognormalDistr implements ContinuousDistribution {
* @param mean the mean
* @param dev the dev
*/
public LognormalDistr(double mean, double dev) {
if (mean <= 0.0 || dev <= 0.0) {
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.mean = mean;
this.dev = dev;
public LognormalDistr(double shape, double scale) {
numGen = new LogNormalDistribution(scale, shape);
}
/*
......@@ -66,12 +53,7 @@ public class LognormalDistr implements ContinuousDistribution {
*/
@Override
public double sample() {
// generate a normal variate from a uniform variate
double n = Math.sqrt(-2 * Math.log(numGen.nextDouble()))
* Math.sin(2 * Math.PI * numGen.nextDouble());
// use it to generate the lognormal variate
return Math.pow(Math.E, mean + dev * n);
return numGen.sample();
}
}
......@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random;
import org.apache.commons.math3.distribution.ParetoDistribution;
/**
* The Class ParetoDistr.
*
......@@ -20,13 +22,7 @@ import java.util.Random;
public class ParetoDistr implements ContinuousDistribution {
/** The num gen. */
private final Random numGen;
/** The shape. */
private final double shape;
/** The location. */
private final double location;
private final ParetoDistribution numGen;
/**
* Instantiates a new pareto distr.
......@@ -36,13 +32,8 @@ public class ParetoDistr implements ContinuousDistribution {
* @param location the location
*/
public ParetoDistr(Random seed, double shape, double location) {
if (shape <= 0.0 || location <= 0.0) {
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0");
}
numGen = seed;
this.shape = shape;
this.location = location;
this(shape, location);
numGen.reseedRandomGenerator(seed.nextLong());
}
/**
......@@ -52,13 +43,7 @@ public class ParetoDistr implements ContinuousDistribution {
* @param location the location
*/
public ParetoDistr(double shape, double location) {
if (shape <= 0.0 || location <= 0.0) {
throw new IllegalArgumentException("Mean and deviation must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.shape = shape;
this.location = location;
numGen = new ParetoDistribution(location, shape);
}
/*
......@@ -67,7 +52,7 @@ public class ParetoDistr implements ContinuousDistribution {
*/
@Override
public double sample() {
return location / Math.pow(numGen.nextDouble(), 1 / shape);
return numGen.sample();
}
}
......@@ -10,6 +10,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random;
import org.apache.commons.math3.distribution.UniformRealDistribution;
/**
* A random number generator based on the Uniform distribution.
*
......@@ -19,10 +21,7 @@ import java.util.Random;
public class UniformDistr implements ContinuousDistribution {
/** The num gen. */
private final Random numGen;
/** The min. */
private final double mag, min;
private final UniformRealDistribution numGen;
/**
* Creates new uniform distribution.
......@@ -31,12 +30,7 @@ public class UniformDistr implements ContinuousDistribution {
* @param max maximum value
*/
public UniformDistr(double min, double max) {
if (min >= max) {
throw new IllegalArgumentException("Maximum must be greater than the minimum.");
}
numGen = new Random();
mag = max - min;
this.min = min;
numGen = new UniformRealDistribution(min, max);
}
/**
......@@ -47,13 +41,8 @@ public class UniformDistr implements ContinuousDistribution {
* @param seed simulation seed to be used
*/
public UniformDistr(double min, double max, long seed) {
if (min >= max) {
throw new IllegalArgumentException("Maximum must be greater than the minimum.");
}
numGen = new Random(seed);
mag = max - min;
this.min = min;
this(min, max);
numGen.reseedRandomGenerator(seed);
}
/**
......@@ -63,7 +52,7 @@ public class UniformDistr implements ContinuousDistribution {
*/
@Override
public double sample() {
return (numGen.nextDouble() * (mag)) + min;
return numGen.sample();
}
/**
......@@ -89,7 +78,7 @@ public class UniformDistr implements ContinuousDistribution {
* @param seed the new seed for the generator
*/
public void setSeed(long seed) {
numGen.setSeed(seed);
numGen.reseedRandomGenerator(seed);
}
}
\ No newline at end of file
......@@ -11,6 +11,8 @@ package org.cloudbus.cloudsim.distributions;
import java.util.Random;
import org.apache.commons.math3.distribution.WeibullDistribution;
/**
* The Class WeibullDistr.
*
......@@ -20,13 +22,7 @@ import java.util.Random;
public class WeibullDistr implements ContinuousDistribution {
/** The num gen. */
private final Random numGen;
/** The alpha. */
private final double alpha;
/** The beta. */
private final double beta;
private final WeibullDistribution numGen;
/**
* Instantiates a new weibull distr.
......@@ -36,13 +32,8 @@ public class WeibullDistr implements ContinuousDistribution {
* @param beta the beta
*/
public WeibullDistr(Random seed, double alpha, double beta) {
if (alpha <= 0.0 || beta <= 0.0) {
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0");
}
numGen = seed;
this.alpha = alpha;
this.beta = beta;
this(alpha, beta);
numGen.reseedRandomGenerator(seed.nextLong());
}
/**
......@@ -52,13 +43,7 @@ public class WeibullDistr implements ContinuousDistribution {
* @param beta the beta
*/
public WeibullDistr(double alpha, double beta) {
if (alpha <= 0.0 || beta <= 0.0) {
throw new IllegalArgumentException("Alpha and beta must be greater than 0.0");
}
numGen = new Random(System.currentTimeMillis());
this.alpha = alpha;
this.beta = beta;
numGen = new WeibullDistribution(alpha, beta);
}
/*
......@@ -67,7 +52,7 @@ public class WeibullDistr implements ContinuousDistribution {
*/
@Override
public double sample() {
return beta * Math.pow(-Math.log(numGen.nextDouble()), 1 / alpha);
return numGen.sample();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment