Skip to content

Commit b9b7a20

Browse files
committed
Update RandomVariatesTests.java
1 parent 7dda32e commit b9b7a20

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

tests/org/cicirello/math/rand/RandomVariatesTests.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,48 @@ public void testNextBinomialRandom() {
130130
public void testNextBinomialLargeN_Splittable() {
131131
SplittableRandom r = new SplittableRandom(42);
132132
final int N = 1000;
133-
for (int i = 0; i < 20; i++) {
133+
final int K = 20;
134+
int t01 = 0;
135+
int t25 = 0;
136+
int t50 = 0;
137+
for (int i = 0; i < K; i++) {
134138
int b01 = RandomVariates.nextBinomial(N, 0.01, r);
135139
int b25 = RandomVariates.nextBinomial(N, 0.25, r);
136140
int b50 = RandomVariates.nextBinomial(N, 0.5, r);
137141
assertTrue(b01 >= 0 && b01 <= N);
138142
assertTrue(b25 >= 0 && b25 <= N);
139143
assertTrue(b50 >= 0 && b50 <= N);
144+
t01 += b01;
145+
t25 += b25;
146+
t50 += b50;
140147
}
148+
assertTrue(Math.abs(N*0.5 - 1.0*t50/K) <= N*0.125);
149+
assertTrue(Math.abs(N*0.25 - 1.0*t25/K) <= N*0.125);
150+
assertTrue(Math.abs(N*0.01 - 1.0*t01/K) <= N*0.125);
141151
}
142152

143153
@Test
144154
public void testNextBinomialLargeN_Random() {
145155
Random r = new Random(42);
146156
final int N = 3000;
147-
for (int i = 0; i < 20; i++) {
157+
final int K = 20;
158+
int t01 = 0;
159+
int t25 = 0;
160+
int t50 = 0;
161+
for (int i = 0; i < K; i++) {
148162
int b01 = RandomVariates.nextBinomial(N, 0.01, r);
149163
int b25 = RandomVariates.nextBinomial(N, 0.25, r);
150164
int b50 = RandomVariates.nextBinomial(N, 0.5, r);
151165
assertTrue(b01 >= 0 && b01 <= N);
152166
assertTrue(b25 >= 0 && b25 <= N);
153167
assertTrue(b50 >= 0 && b50 <= N);
168+
t01 += b01;
169+
t25 += b25;
170+
t50 += b50;
154171
}
172+
assertTrue(Math.abs(N*0.5 - 1.0*t50/K) <= N*0.125);
173+
assertTrue(Math.abs(N*0.25 - 1.0*t25/K) <= N*0.125);
174+
assertTrue(Math.abs(N*0.01 - 1.0*t01/K) <= N*0.125);
155175
}
156176

157177
@Test

0 commit comments

Comments
 (0)