1 package nl.tudelft.simulation.jstats.streams;
2
3 import java.util.Random;
4
5 import nl.tudelft.simulation.language.DSOLException;
6 import nl.tudelft.simulation.language.reflection.StateSaver;
7
8 /**
9 * The Java2Random is an extension of the <code>java.util.Random</code> class which implements the StreamInterface.
10 * <p>
11 * Copyright (c) 2002-2023 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
12 * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
13 * project is distributed under a three-clause BSD-style license, which can be found at
14 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
15 * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
16 * </p>
17 * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank"> Alexander Verbraeck</a>
18 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
19 */
20 public class Java2Random extends Random implements StreamInterface
21 {
22 /** */
23 private static final long serialVersionUID = 20140831L;
24
25 /**
26 * Seed is a link to the current seed value. The reason to store the seed in this variable is that there is no getSeed() on
27 * the Random class in Java.
28 */
29 private long seed;
30
31 /** The original seed of the generator. */
32 private final long originalSeed;
33
34 /**
35 * Create a new Java2Random and initializes with System.currentTimeMillis.
36 */
37 public Java2Random()
38 {
39 this(System.currentTimeMillis());
40 }
41
42 /**
43 * Create a new Java2Random and initialize with a given seed.
44 * @param seed long; the seed to use.
45 */
46 public Java2Random(final long seed)
47 {
48 super(seed);
49 this.seed = seed;
50 this.originalSeed = seed;
51 }
52
53 /** {@inheritDoc} */
54 @Override
55 public void reset()
56 {
57 this.setSeed(this.seed);
58 }
59
60 /** {@inheritDoc} */
61 @Override
62 public long getOriginalSeed()
63 {
64 return this.originalSeed;
65 }
66
67 /** {@inheritDoc} */
68 @Override
69 public int nextInt(final int i, final int j)
70 {
71 return i + (int) Math.floor((j - i + 1) * this.nextDouble());
72 }
73
74 /** {@inheritDoc} */
75 @Override
76 public synchronized void setSeed(final long seed)
77 {
78 this.seed = seed;
79 super.setSeed(seed);
80 }
81
82 /** {@inheritDoc} */
83 @Override
84 public long getSeed()
85 {
86 return this.seed;
87 }
88
89 /** {@inheritDoc} */
90 @Override
91 public byte[] saveState() throws StreamException
92 {
93 try
94 {
95 return StateSaver.saveState(this);
96 }
97 catch (DSOLException exception)
98 {
99 throw new StreamException(exception);
100 }
101 }
102
103 /** {@inheritDoc} */
104 @Override
105 public void restoreState(final byte[] state) throws StreamException
106 {
107 try
108 {
109 StateSaver.restoreState(this, state);
110 }
111 catch (DSOLException exception)
112 {
113 throw new StreamException(exception);
114 }
115 }
116
117 }