1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.streams;
11
12 import java.util.Random;
13
14 /***
15 * The Java2Random is an extention of the <code>java.util.Random</code> class
16 * which implements the StreamInterface.
17 * <p>
18 * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl">Delft
19 * University of Technology </a>, the Netherlands. <br>
20 * See for project information <a href="http://www.simulation.tudelft.nl">
21 * www.simulation.tudelft.nl </a> <br>
22 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
23 * License (GPL) </a>, no warranty <br>
24 *
25 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
26 * Jacobs </a>
27 * @version 1.11 2004-03-18
28 * @since 1.0
29 */
30 public class Java2Random extends Random implements StreamInterface
31 {
32 /***
33 * seed is a link to the seed value. The reason to store the seed in this
34 * variable is that there is no getSeed() on the Java2Random
35 */
36 protected long seed;
37
38 /***
39 * creates a new Java2Random and in initializes with
40 * System.currentTimeMillis constructs a new Java2Random
41 *
42 */
43 public Java2Random()
44 {
45 this(System.currentTimeMillis());
46 }
47
48 /***
49 * @see java.util.Random#Random(long)
50 */
51 public Java2Random(final long seed)
52 {
53 super(seed);
54 this.seed = seed;
55 }
56
57 /***
58 * resets a stream
59 *
60 * @see nl.tudelft.simulation.jstats.streams.StreamInterface#reset()
61 */
62 public void reset()
63 {
64 this.setSeed(this.seed);
65 }
66
67 /***
68 * @see nl.tudelft.simulation.jstats.streams.StreamInterface#nextInt(int,
69 * int)
70 */
71 public int nextInt(final int i, final int j)
72 {
73 return i + (int) Math.floor((j - i + 1) * this.nextDouble());
74 }
75
76 /***
77 * @see java.util.Random#setSeed(long)
78 */
79 public synchronized void setSeed(final long seed)
80 {
81 this.seed = seed;
82 super.setSeed(seed);
83 }
84
85 /***
86 * @see nl.tudelft.simulation.jstats.streams.StreamInterface#getSeed()
87 */
88 public long getSeed()
89 {
90 return this.seed;
91 }
92 }