|
| 1 | +/* |
| 2 | + * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne |
| 3 | + * and others contributors as indicated |
| 4 | + * by the @authors tag. All rights reserved. |
| 5 | + * See the copyright.txt in the distribution for a |
| 6 | + * full listing of individual contributors. |
| 7 | + * This copyrighted material is made available to anyone wishing to use, |
| 8 | + * modify, copy, or redistribute it subject to the terms and conditions |
| 9 | + * of the GNU Lesser General Public License, v. 2.1. |
| 10 | + * This program is distributed in the hope that it will be useful, but WITHOUT A |
| 11 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 13 | + * You should have received a copy of the GNU Lesser General Public License, |
| 14 | + * v.2.1 along with this distribution; if not, write to the Free Software |
| 15 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 16 | + * MA 02110-1301, USA. |
| 17 | + * |
| 18 | + * (C) 1990-2008, |
| 19 | + */ |
| 20 | + |
| 21 | +package org.javasim.streams; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +/** |
| 26 | + * Returns a number drawn from a triangular distribution with lower limit a, upper limit b and mode c, where a < b and a ≤ c ≤ b. |
| 27 | + */ |
| 28 | + |
| 29 | +public class TriangularStream extends RandomStream { |
| 30 | + /** |
| 31 | + * Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. |
| 32 | + */ |
| 33 | + |
| 34 | + public TriangularStream(double a, double b, double c) { |
| 35 | + super(); |
| 36 | + |
| 37 | + this.a = a; |
| 38 | + this.b = b; |
| 39 | + this.c = c; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. Skip the first 'StreamSelect' values before returning numbers from the stream. |
| 44 | + */ |
| 45 | + |
| 46 | + public TriangularStream(double a, double b, double c, int StreamSelect) { |
| 47 | + super(); |
| 48 | + |
| 49 | + this.a = a; |
| 50 | + this.b = b; |
| 51 | + this.c = c; |
| 52 | + |
| 53 | + for (int i = 0; i < StreamSelect * 1000; i++) |
| 54 | + uniform(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. Skip the first 'StreamSelect' values before returning numbers from the stream. Pass the seeds 'MGSeed' and 'LCGSeed' to |
| 59 | + * the base class. |
| 60 | + */ |
| 61 | + |
| 62 | + public TriangularStream(double a, double b, double c, int StreamSelect, long MGSeed, long LCGSeed) { |
| 63 | + super(MGSeed, LCGSeed); |
| 64 | + |
| 65 | + this.a = a; |
| 66 | + this.b = b; |
| 67 | + this.c = c; |
| 68 | + |
| 69 | + for (int i = 0; i < StreamSelect * 1000; i++) |
| 70 | + uniform(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return a number from the stream. |
| 75 | + */ |
| 76 | + |
| 77 | + public double getNumber() throws IOException, ArithmeticException { |
| 78 | + |
| 79 | + double F = (c - a) / (b - a); |
| 80 | + double rand = uniform(); |
| 81 | + if (rand < F) { |
| 82 | + return a + Math.sqrt(rand * (b - a) * (c - a)); |
| 83 | + } else { |
| 84 | + return b - Math.sqrt((1 - rand) * (b - a) * (b - c)); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + private double a; |
| 90 | + private double b; |
| 91 | + private double c; |
| 92 | +} |
0 commit comments