1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.distributions;
11
12 import nl.tudelft.simulation.jstats.streams.StreamInterface;
13
14 /***
15 * The discrete Uniform distribution. For more information on this distribution
16 * see <a href="http://mathworld.wolfram.com/UnifomrDistribution.html">
17 * http://mathworld.wolfram.com/UniformDistribution.html </a>
18 * <p>
19 * (c) copyright 2002-2004 <a href="http://www.simulation.tudelft.nl">Delft
20 * University of Technology </a>, the Netherlands. <br>
21 * See for project information <a href="http://www.simulation.tudelft.nl">
22 * www.simulation.tudelft.nl </a> <br>
23 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
24 * License (GPL) </a>, no warranty <br>
25 *
26 * @author <a href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">
27 * Alexander Verbraeck </a> <br>
28 * <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm"> Peter
29 * Jacobs </a>
30 * @version 1.9 2004-03-22
31 * @since 1.2
32 */
33 public class DistDiscreteUniform extends DistDiscrete
34 {
35 /*** min is the minimum value of this distribution */
36 private long min;
37
38 /*** max is the maximum value of this distribution */
39 private long max;
40
41 /***
42 * constructs a new uniform distribution. Random occurence with several
43 * possible outcomes, each of which is equally likely.
44 *
45 * @param stream the numberstream
46 * @param min the minimal value
47 * @param max the maximum value
48 */
49 public DistDiscreteUniform(final StreamInterface stream, final long min,
50 final long max)
51 {
52 super(stream);
53 this.min = min;
54 if (max >= this.min)
55 {
56 this.max = max;
57 } else
58 {
59 throw new IllegalArgumentException(
60 "Error Discrete Uniform - min >= max");
61 }
62 }
63
64 /***
65 * @see DistDiscrete#draw()
66 */
67 public long draw()
68 {
69 return this.stream.nextInt((int) this.min, (int) this.max);
70 }
71
72 /***
73 * @see nl.tudelft.simulation.jstats.distributions.DistDiscrete
74 * #probability(int)
75 */
76 public double probability(final int observation)
77 {
78 if (observation > this.min && observation < this.max)
79 {
80 return 1 / ((double) this.max - this.min + 1);
81 }
82 return 0.0;
83 }
84
85 /***
86 * @see java.lang.Object#toString()
87 */
88 public String toString()
89 {
90 return "DiscreteUniform(" + this.min + "," + this.max + ")";
91 }
92 }