org.cloudbus.cloudsim.distributions.ZipfDistr Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudsim-plus Show documentation
Show all versions of cloudsim-plus Show documentation
CloudSim Plus: A modern, highly extensible and easier-to-use Java 8 Framework for Modeling and Simulation of Cloud Computing Infrastructures and Services
/*
* Title: CloudSim Toolkit
* Descripimport java.util.Random;
mulation) Toolkit for Modeling and Simulation of Clouds
* Licence: GPL - http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2009-2012, The University of Melbourne, Australia
*/
package org.cloudbus.cloudsim.distributions;
import org.apache.commons.math3.distribution.UniformRealDistribution;
/**
* A pseudo random number generator following the
* Zipf distribution.
*
* @author Marcos Dias de Assuncao
* @since CloudSim Toolkit 1.0
*/
public class ZipfDistr extends ContinuousDistributionAbstract {
/**
* The shape.
*/
private final double shape;
/**
* The den.
*/
private double den;
/**
* Instantiates a new Zipf pseudo random number generator.
*
* @param seed the seed
* @param shape the shape
* @param population the population
*/
public ZipfDistr(long seed, double shape, int population) {
super(new UniformRealDistribution(0, 1), seed);
if (shape <= 0.0 || population < 1) {
throw new IllegalArgumentException("Mean must be greater than 0.0 and population greater than 0");
}
this.shape = shape;
computeDen(shape, population);
}
/**
* Instantiates a new Zipf pseudo random number generator.
*
* @param shape the shape
* @param population the population
*/
public ZipfDistr(double shape, int population) {
this(-1, shape, population);
}
@Override
public double sample() {
final double variate = super.sample();
double num = 1;
double nextNum = 1 + 1 / Math.pow(2, shape);
double j = 3;
while (variate > nextNum / den) {
num = nextNum;
nextNum += 1 / Math.pow(j, shape);
j++;
}
return num / den;
}
/**
* Compute the den.
*
* @param shape the shape
* @param population the population
*/
private void computeDen(double shape, int population) {
den = 0.0;
for (int j = 1; j <= population; j++) {
den += 1 / Math.pow(j, shape);
}
}
}