source: xssim/trunk/src/simulator/stats/random/MinMaxDistribution.java @ 104

Revision 104, 1.9 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.stats.random;
2
3import cern.jet.random.AbstractDistribution;
4
5/**
6 * Represents a wrapper for a specific random distribution class. The purpose of this class is
7 * to provide the random values, with a given distribution, but within a given range [min; max] - inclusive.
8 * Min may be called a lower bound, and max an upper bound.
9 * @author Stanislaw Szczepanowski
10 *
11 */
12public class MinMaxDistribution extends AbstractDistribution {
13
14        /**
15         * The lower bound for the allowed random values (<code>null</code> if no bound was set)
16         */
17        protected Double min;
18       
19        /**
20         * The upper bound for the allowed random values (<code>null</code> if no bound was set)
21         */
22        protected Double max;
23       
24        /**
25         * The source distribution for random values
26         */
27        protected AbstractDistribution source;
28       
29        /**
30         * MinMax constructor.
31         * It constructs the class with a given original distribution and minimal and maximal values.
32         * @param origDistribution the original distribution, which will be the source for random numbers. It must not be <code>null</code>.
33         * @param min the minimal allowed value for the random numbers. If a <code>null</code> value is given, then no lower bound will be set.
34         * @param max the maximal allowed value for the random numbers. If a <code>null</code> value is given, then no upper bound will be set.
35         */
36        public MinMaxDistribution(AbstractDistribution origDistribution, Double min, Double max) {
37                source = origDistribution;
38               
39                if (min != null && max != null) {
40                        if (min > max)
41                                throw new IllegalArgumentException("ERROR: invalid parameters min > max");
42                }
43               
44                this.min = min;
45                this.max = max;
46        }
47       
48        @Override
49        public double nextDouble() {
50                double value;
51               
52                for (;;) {
53                        value = source.nextDouble();
54                       
55                        if (min != null && Double.compare(value, min) < 0)
56                                continue;
57                       
58                        if (max != null && Double.compare(value, max) > 0)
59                                continue;
60
61                        break;
62                }
63               
64                return value;
65        }
66}
Note: See TracBrowser for help on using the repository browser.