001package squidpony.squidmath;
002
003/**
004 * An IDistribution that allows a parameter to determine how many calls to {@link IRNG#nextDouble()} to make and average
005 * whenever a double is requested. When this parameter {@code degree} is 1, this is uniform; when it is 2, this is a
006 * triangular distribution, and when it is 3 or more it is an increasingly centralized bell curve. The average value is
007 * always very close to 0.5, and the bounds are the same as {@link IRNG#nextDouble()}.
008 * <br>
009 * Created by Tommy Ettinger on 11/27/2019.
010 */
011public class CurvedBoundedDistribution extends IDistribution.SimpleDistribution implements IDistribution {
012    public static final CurvedBoundedDistribution instance = new CurvedBoundedDistribution(3);
013    public static final CurvedBoundedDistribution instanceTriangular = new CurvedBoundedDistribution(2);
014    public static final CurvedBoundedDistribution instanceGaussianLike = new CurvedBoundedDistribution(6);
015    private int degree;
016    private double i_degree;
017    public CurvedBoundedDistribution()
018    {
019        this(3);
020    }
021    public CurvedBoundedDistribution(int degree)
022    {
023        this.degree = Math.max(degree, 1);
024        i_degree = 1.0 / this.degree;
025    }
026
027    public int getDegree() {
028        return degree;
029    }
030
031    public void setDegree(int degree) {
032        this.degree = Math.max(degree, 1);
033        i_degree = 1.0 / this.degree;
034    }
035
036    @Override
037    public double nextDouble(IRNG rng) {
038        double sum = 0.0;
039        for (int i = 0; i < degree; i++) {
040            sum += rng.nextDouble();
041        }
042        return sum * i_degree;
043    }
044}