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