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
10
11
12
13
14
15
16
17
18
19
20 public class Java2Random extends Random implements StreamInterface
21 {
22
23 private static final long serialVersionUID = 20140831L;
24
25
26
27
28
29 private long seed;
30
31
32 private final long originalSeed;
33
34
35
36
37 public Java2Random()
38 {
39 this(System.currentTimeMillis());
40 }
41
42
43
44
45
46 public Java2Random(final long seed)
47 {
48 super(seed);
49 this.seed = seed;
50 this.originalSeed = seed;
51 }
52
53
54 @Override
55 public void reset()
56 {
57 this.setSeed(this.seed);
58 }
59
60
61 @Override
62 public long getOriginalSeed()
63 {
64 return this.originalSeed;
65 }
66
67
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
75 @Override
76 public synchronized void setSeed(final long seed)
77 {
78 this.seed = seed;
79 super.setSeed(seed);
80 }
81
82
83 @Override
84 public long getSeed()
85 {
86 return this.seed;
87 }
88
89
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
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 }