1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.random;
23
24 import java.io.Serializable;
25 import java.util.Random;
26
27 import org.hipparchus.exception.LocalizedCoreFormats;
28 import org.hipparchus.exception.MathIllegalArgumentException;
29 import org.hipparchus.util.MathUtils;
30
31
32
33
34
35 public class JDKRandomGenerator extends IntRandomGenerator implements Serializable {
36
37
38 private static final long serialVersionUID = 20151227L;
39
40
41 private final Random delegate;
42
43
44
45
46 public JDKRandomGenerator() {
47 delegate = new Random();
48 }
49
50
51
52
53
54
55 public JDKRandomGenerator(long seed) {
56 delegate = new Random(seed);
57 }
58
59
60
61
62
63
64
65
66 public JDKRandomGenerator(Random random) {
67 MathUtils.checkNotNull(random);
68 delegate = random;
69 }
70
71
72 @Override
73 public void setSeed(int seed) {
74 delegate.setSeed(seed);
75 }
76
77
78 @Override
79 public void setSeed(long seed) {
80 delegate.setSeed(seed);
81 }
82
83
84 @Override
85 public void setSeed(int[] seed) {
86 delegate.setSeed(convertToLong(seed));
87 }
88
89
90 @Override
91 public void nextBytes(byte[] bytes) {
92 delegate.nextBytes(bytes);
93 }
94
95
96 @Override
97 public int nextInt() {
98 return delegate.nextInt();
99 }
100
101
102 @Override
103 public long nextLong() {
104 return delegate.nextLong();
105 }
106
107
108 @Override
109 public boolean nextBoolean() {
110 return delegate.nextBoolean();
111 }
112
113
114 @Override
115 public float nextFloat() {
116 return delegate.nextFloat();
117 }
118
119
120 @Override
121 public double nextDouble() {
122 return delegate.nextDouble();
123 }
124
125
126 @Override
127 public double nextGaussian() {
128 return delegate.nextGaussian();
129 }
130
131
132 @Override
133 public int nextInt(int n) {
134 try {
135 return delegate.nextInt(n);
136 } catch (IllegalArgumentException e) {
137 throw new MathIllegalArgumentException(e, LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
138 n, 0);
139 }
140 }
141
142
143
144
145
146
147
148 private static long convertToLong(int[] seed) {
149
150
151 final long prime = 4294967291l;
152
153 long combined = 0l;
154 for (int s : seed) {
155 combined = combined * prime + s;
156 }
157
158 return combined;
159 }
160
161 }