All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sim.util.distribution.Uniform Maven / Gradle / Ivy

Go to download

MASON is a fast discrete-event multiagent simulation library core in Java, designed to be the foundation for large custom-purpose Java simulations, and also to provide more than enough functionality for many lightweight simulation needs. MASON contains both a model library and an optional suite of visualization tools in 2D and 3D.

The newest version!
/*
  Copyright � 1999 CERN - European Organization for Nuclear Research.
  Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
  is hereby granted without fee, provided that the above copyright notice appear in all copies and 
  that both that copyright notice and this permission notice appear in supporting documentation. 
  CERN makes no representations about the suitability of this software for any purpose. 
  It is provided "as is" without expressed or implied warranty.
*/
package sim.util.distribution;
import ec.util.MersenneTwisterFast;

/**
 * Uniform distribution;  Math definition
 * and  animated definition.
 * 

* Instance methods operate on a user supplied uniform random number generator; they are unsynchronized. *

* Static methods operate on a default uniform random number generator; they are synchronized. *

* @author [email protected] * @version 1.0, 09/24/99 */ public class Uniform extends AbstractContinousDistribution { private static final long serialVersionUID = 1; protected double min; protected double max; /** * Constructs a uniform distribution with the given minimum and maximum. */ public Uniform(double min, double max, MersenneTwisterFast randomGenerator) { setRandomGenerator(randomGenerator); setState(min,max); } /** * Constructs a uniform distribution with min=0.0 and max=1.0. */ public Uniform(MersenneTwisterFast randomGenerator) { this(0,1,randomGenerator); } /** * Returns the cumulative distribution function (assuming a continous uniform distribution). */ public double cdf(double x) { if (x <= min) return 0.0; if (x >= max) return 1.0; return (x-min) / (max-min); } /** * Returns a uniformly distributed random boolean. */ public boolean nextBoolean() { return randomGenerator.nextDouble() > 0.5; } /** * Returns a uniformly distributed random number in the open interval (min,max) (excluding min and max). */ public double nextDouble() { return min+(max-min)*randomGenerator.nextDouble(); } /** * Returns a uniformly distributed random number in the open interval (from,to) (excluding from and to). * Pre conditions: from <= to. */ public double nextDoubleFromTo(double from, double to) { return from+(to-from)*randomGenerator.nextDouble(); } /** * Returns a uniformly distributed random number in the open interval (from,to) (excluding from and to). * Pre conditions: from <= to. */ public float nextFloatFromTo(float from, float to) { return (float) nextDoubleFromTo(from,to); } /** * Returns a uniformly distributed random number in the closed interval [min,max] (including min and max). */ public int nextInt() { return nextIntFromTo((int)Math.round(min), (int)Math.round(max)); } /** * Returns a uniformly distributed random number in the closed interval [from,to] (including from and to). * Pre conditions: from <= to. */ public int nextIntFromTo(int from, int to) { return (int) ((long)from + (long)((1L + (long)to - (long)from)*randomGenerator.nextDouble())); } /** * Returns a uniformly distributed random number in the closed interval [from,to] (including from and to). * Pre conditions: from <= to. */ public long nextLongFromTo(long from, long to) { /* Doing the thing turns out to be more tricky than expected. avoids overflows and underflows. treats cases like from=-1, to=1 and the like right. the following code would NOT solve the problem: return (long) (Doubles.randomFromTo(from,to)); rounding avoids the unsymmetric behaviour of casts from double to long: (long) -0.7 = 0, (long) 0.7 = 0. checking for overflows and underflows is also necessary. */ // first the most likely and also the fastest case. if (from>=0 && to to) random = from; } else { random = Math.round(nextDoubleFromTo(from-1,to)); if (random < from) random = to; } return random; } /** * Returns the probability distribution function (assuming a continous uniform distribution). */ public double pdf(double x) { if (x <= min || x >= max) return 0.0; return 1.0 / (max-min); } /** * Sets the internal state. */ public void setState(double min, double max) { if (max





© 2015 - 2025 Weber Informatics LLC | Privacy Policy