View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.core;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  
24  import org.apache.commons.rng.core.source32.JDKRandom;
25  import org.apache.commons.rng.core.source32.Well512a;
26  import org.apache.commons.rng.core.source32.Well1024a;
27  import org.apache.commons.rng.core.source32.Well19937a;
28  import org.apache.commons.rng.core.source32.Well19937c;
29  import org.apache.commons.rng.core.source32.Well44497a;
30  import org.apache.commons.rng.core.source32.Well44497b;
31  import org.apache.commons.rng.core.source32.ISAACRandom;
32  import org.apache.commons.rng.core.source32.MersenneTwister;
33  import org.apache.commons.rng.core.source32.MultiplyWithCarry256;
34  import org.apache.commons.rng.core.source32.KISSRandom;
35  import org.apache.commons.rng.core.source64.SplitMix64;
36  import org.apache.commons.rng.core.source64.XorShift1024Star;
37  import org.apache.commons.rng.core.source64.TwoCmres;
38  import org.apache.commons.rng.core.source64.MersenneTwister64;
39  import org.apache.commons.rng.RestorableUniformRandomProvider;
40  
41  /**
42   * The purpose of this class is to provide the list of all generators
43   * implemented in the library.
44   * The list must be updated with each new RNG implementation.
45   *
46   * @see #list()
47   * @see #list32()
48   * @see #list64()
49   */
50  public class ProvidersList {
51      /** List of all RNGs implemented in the library. */
52      private static final List<RestorableUniformRandomProvider[]> LIST =
53          new ArrayList<RestorableUniformRandomProvider[]>();
54      /** List of 32-bits based RNGs. */
55      private static final List<RestorableUniformRandomProvider[]> LIST32 =
56          new ArrayList<RestorableUniformRandomProvider[]>();
57      /** List of 64-bits based RNGs. */
58      private static final List<RestorableUniformRandomProvider[]> LIST64 =
59          new ArrayList<RestorableUniformRandomProvider[]>();
60  
61      static {
62          try {
63              // "int"-based RNGs.
64              add(LIST32, new JDKRandom(-122333444455555L));
65              add(LIST32, new MersenneTwister(new int[] { -123, -234, -345 }));
66              add(LIST32, new Well512a(new int[] { -23, -34, -45 }));
67              add(LIST32, new Well1024a(new int[] { -1234, -2345, -3456 }));
68              add(LIST32, new Well19937a(new int[] { -2123, -3234, -4345 }));
69              add(LIST32, new Well19937c(new int[] { -123, -234, -345, -456 }));
70              add(LIST32, new Well44497a(new int[] { -12345, -23456, -34567 }));
71              add(LIST32, new Well44497b(new int[] { 123, 234, 345 }));
72              add(LIST32, new ISAACRandom(new int[] { 123, -234, 345, -456 }));
73              add(LIST32, new MultiplyWithCarry256(new int[] { 12, -1234, -3456, 45679 }));
74              add(LIST32, new KISSRandom(new int[] { 12, 1234, 23456, 345678 }));
75              // ... add more here.
76  
77              // "long"-based RNGs.
78              add(LIST64, new SplitMix64(-98877766544333L));
79              add(LIST64, new XorShift1024Star(new long[] { 123456L, 234567L, -345678L }));
80              add(LIST64, new TwoCmres(55443322));
81              add(LIST64, new TwoCmres(-987654321, 5, 8));
82              add(LIST64, new MersenneTwister64(new long[] { 1234567L, 2345678L, -3456789L }));
83              // ... add more here.
84  
85              // Do not modify the remaining statements.
86              // Complete list.
87              LIST.addAll(LIST32);
88              LIST.addAll(LIST64);
89          } catch (Exception e) {
90              System.err.println("Unexpected exception while creating the list of generators: " + e);
91              e.printStackTrace(System.err);
92              throw new RuntimeException(e);
93          }
94      }
95  
96      /**
97       * Class contains only static methods.
98       */
99      private ProvidersList() {}
100 
101     /**
102      * Helper to statisfy Junit requirement that each parameter set contains
103      * the same number of objects.
104      */
105     private static void add(List<RestorableUniformRandomProvider[]> list,
106                             RestorableUniformRandomProvider rng) {
107         list.add(new RestorableUniformRandomProvider[] { rng });
108     }
109 
110     /**
111      * Subclasses that are "parametric" tests can forward the call to
112      * the "@Parameters"-annotated method to this method.
113      *
114      * @return the list of all generators.
115      */
116     public static Iterable<RestorableUniformRandomProvider[]> list() {
117         return Collections.unmodifiableList(LIST);
118     }
119 
120     /**
121      * Subclasses that are "parametric" tests can forward the call to
122      * the "@Parameters"-annotated method to this method.
123      *
124      * @return the list of 32-bits based generators.
125      */
126     public static Iterable<RestorableUniformRandomProvider[]> list32() {
127         return Collections.unmodifiableList(LIST32);
128     }
129 
130     /**
131      * Subclasses that are "parametric" tests can forward the call to
132      * the "@Parameters"-annotated method to this method.
133      *
134      * @return the list of 64-bits based generators.
135      */
136     public static Iterable<RestorableUniformRandomProvider[]> list64() {
137         return Collections.unmodifiableList(LIST64);
138     }
139 }