001package squidpony.squidmath; 002 003/** 004 * An IDistribution that implements the <a href="https://en.wikipedia.org/wiki/Exponential_distribution">Exponential 005 * distribution</a>. Takes lambda as a parameter during construction (default 1), and lambda also has getters/setters. 006 * <br> 007 * Created by Tommy Ettinger on 11/23/2019. 008 */ 009public class ExponentialDistribution implements IDistribution { 010 public static final ExponentialDistribution instance = new ExponentialDistribution(); 011 public static final ExponentialDistribution instance_0_5 = new ExponentialDistribution(0.5); 012 public static final ExponentialDistribution instance_1_5 = new ExponentialDistribution(1.5); 013 private double i_lambda; 014 public ExponentialDistribution() 015 { 016 i_lambda = 1.0; 017 } 018 public ExponentialDistribution(double lambda) 019 { 020 i_lambda = 1.0 / lambda; 021 } 022 023 public double getLambda() { 024 return 1.0 / i_lambda; 025 } 026 027 public void setLambda(double lambda) { 028 this.i_lambda = 1.0 / lambda; 029 } 030 031 @Override 032 public double nextDouble(IRNG rng) { 033 return Math.log(1 - rng.nextDouble()) * i_lambda; 034 } 035 /** 036 * The lower inclusive bound is 0 while lambda is positive; it is negative infinity if lambda is negative. 037 * @return zero, or negative infinity if lambda is negative. 038 */ 039 @Override 040 public double getLowerBound() { 041 return i_lambda < 0.0 ? Double.NEGATIVE_INFINITY : 0.0; 042 } 043 044 /** 045 * The upper inclusive bound is infinity while lambda is positive; it is 0 if lambda is negative. 046 * @return positive infinity, or zero if lambda is negative. 047 */ 048 @Override 049 public double getUpperBound() { 050 return i_lambda < 0.0 ? 0.0 : Double.POSITIVE_INFINITY; 051 } 052}