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.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.Future;
31
32 import org.junit.Assert;
33 import org.junit.Test;
34
35 public class SynchronizedRandomGeneratorTest {
36
37 @Test
38 public void testAdapter() {
39 final int seed = 12345;
40 final RandomGenerator orig = new MersenneTwister(seed);
41 final RandomGenerator wrap
42 = new SynchronizedRandomGenerator(new MersenneTwister(seed));
43
44 final int bSize = 67;
45 final byte[] bOrig = new byte[bSize];
46 final byte[] bWrap = new byte[bSize];
47
48 for (int i = 0; i < 100; i++) {
49 orig.nextBytes(bOrig);
50 wrap.nextBytes(bWrap);
51 for (int k = 0; k < bSize; k++) {
52 Assert.assertEquals(bOrig[k], bWrap[k]);
53 }
54
55 Assert.assertEquals(orig.nextInt(), wrap.nextInt());
56
57 final int range = (i + 1) * 89;
58 Assert.assertEquals(orig.nextInt(range), wrap.nextInt(range));
59
60 Assert.assertEquals(orig.nextLong(), wrap.nextLong());
61 Assert.assertEquals(orig.nextBoolean(), wrap.nextBoolean());
62 Assert.assertEquals(orig.nextFloat(), wrap.nextFloat(), 0);
63 Assert.assertEquals(orig.nextDouble(), wrap.nextDouble(), 0);
64 Assert.assertEquals(orig.nextGaussian(), wrap.nextGaussian(), 0);
65
66 }
67 }
68
69 @Test
70 public void testMath899Sync() throws Throwable {
71 try {
72 final int numberOfThreads = 5;
73 final int numberOfGenerators = 5;
74 final int numberOfSamples = 100000;
75
76
77
78 for (int i = 0; i < 10; i++) {
79 doTestMath899(true, numberOfThreads, numberOfGenerators,
80 numberOfSamples);
81 }
82 } catch (InterruptedException e) {
83 Assert.fail(e.getMessage());
84 } catch (ExecutionException e) {
85 throw e.getCause();
86 }
87 }
88
89
90
91
92 private double[] doTestMath899(final boolean sync,
93 final int numThreads,
94 final int numGenerators,
95 final int numSamples)
96 throws InterruptedException,
97 ExecutionException {
98 final RandomGenerator rng = new MersenneTwister();
99 final RandomGenerator wrapper = sync ? new SynchronizedRandomGenerator(rng) : rng;
100
101 final List<Callable<Double>> tasks = new ArrayList<Callable<Double>>();
102 for (int i = 0; i < numGenerators; i++) {
103 tasks.add(new Callable<Double>() {
104 @Override
105 public Double call() {
106 Double lastValue = 0d;
107 for (int j = 0; j < numSamples; j++) {
108 lastValue = wrapper.nextGaussian();
109 }
110 return lastValue;
111 }
112 });
113 }
114
115 final ExecutorService exec = Executors.newFixedThreadPool(numThreads);
116 final List<Future<Double>> results = exec.invokeAll(tasks);
117
118 final double[] values = new double[numGenerators];
119 for (int i = 0; i < numGenerators; i++) {
120 values[i] = results.get(i).get();
121 }
122 return values;
123 }
124 }