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.simple;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  
24  /**
25   * The purpose of this class is to provide the list of all generators
26   * implemented in the library.
27   * The list must be updated with each new RNG implementation.
28   *
29   * @see #list()
30   * @see #list32()
31   * @see #list64()
32   */
33  public final class ProvidersList {
34      /** List of all RNGs implemented in the library. */
35      private static final List<Data[]> LIST = new ArrayList<Data[]>();
36      /** List of 32-bits based RNGs. */
37      private static final List<Data[]> LIST32 = new ArrayList<Data[]>();
38      /** List of 64-bits based RNGs. */
39      private static final List<Data[]> LIST64 = new ArrayList<Data[]>();
40  
41      static {
42          try {
43              // "int"-based RNGs.
44              add(LIST32, RandomSource.JDK, -122333444455555L);
45              add(LIST32, RandomSource.MT, new int[] {-123, -234, -345});
46              add(LIST32, RandomSource.WELL_512_A, new int[] {-23, -34, -45});
47              add(LIST32, RandomSource.WELL_1024_A, new int[] {-1234, -2345, -3456});
48              add(LIST32, RandomSource.WELL_19937_A, new int[] {-2123, -3234, -4345});
49              add(LIST32, RandomSource.WELL_19937_C, new int[] {-123, -234, -345, -456});
50              add(LIST32, RandomSource.WELL_44497_A, new int[] {-12345, -23456, -34567});
51              add(LIST32, RandomSource.WELL_44497_B, new int[] {123, 234, 345});
52              add(LIST32, RandomSource.ISAAC, new int[] {123, -234, 345, -456});
53              add(LIST32, RandomSource.MWC_256, new int[] {12, -1234, -3456, 45678});
54              add(LIST32, RandomSource.KISS, new int[] {12, 1234, 23456, 345678});
55              add(LIST32, RandomSource.XO_RO_SHI_RO_64_S, new int[] {42, 12345});
56              add(LIST32, RandomSource.XO_RO_SHI_RO_64_SS, new int[] {78942, 134});
57              add(LIST32, RandomSource.XO_SHI_RO_128_PLUS, new int[] {565642, 1234, 4534});
58              add(LIST32, RandomSource.XO_SHI_RO_128_SS, new int[] {89, 1234, 6787});
59              add(LIST32, RandomSource.PCG_XSH_RR_32, new long[] {1738L, 1234L});
60              add(LIST32, RandomSource.PCG_XSH_RS_32, new long[] {259L, 2861L});
61              add(LIST32, RandomSource.PCG_MCG_XSH_RS_32, 9678L);
62              add(LIST32, RandomSource.PCG_MCG_XSH_RR_32, 2578291L);
63              // Ensure a high complexity increment is used for the Weyl sequence otherwise
64              // it will not output random data.
65              add(LIST32, RandomSource.MSWS, new long[] {687233648L, 678656564562300L, 0xb5ad4eceda1ce2a9L});
66              add(LIST32, RandomSource.SFC_32, new int[] {-23574234, 7654343});
67              add(LIST32, RandomSource.XO_SHI_RO_128_PP, new int[] {8796823, -3244890, -263842});
68              // ... add more here.
69  
70              // "long"-based RNGs.
71              add(LIST64, RandomSource.SPLIT_MIX_64, -988777666655555L);
72              add(LIST64, RandomSource.XOR_SHIFT_1024_S, new long[] {123456L, 234567L, -345678L});
73              add(LIST64, RandomSource.XOR_SHIFT_1024_S_PHI, new long[] {-234567L, -345678L, 3456789L});
74              add(LIST64, RandomSource.TWO_CMRES, 55443322);
75              add(LIST64, RandomSource.TWO_CMRES_SELECT, -987654321, 5, 8);
76              add(LIST64, RandomSource.MT_64, new long[] {1234567L, 2345678L, -3456789L});
77              add(LIST64, RandomSource.XO_RO_SHI_RO_128_PLUS, new long[] {55646L, -456659L, 565656L});
78              add(LIST64, RandomSource.XO_RO_SHI_RO_128_SS, new long[] {45655L, 5454544L, 4564659L});
79              add(LIST64, RandomSource.XO_SHI_RO_256_PLUS, new long[] {11222L, -568989L, -456789L});
80              add(LIST64, RandomSource.XO_SHI_RO_256_SS, new long[] {98765L, -2345678L, -3456789L});
81              add(LIST64, RandomSource.XO_SHI_RO_512_PLUS, new long[] {89932L, -545669L, 4564689L});
82              add(LIST64, RandomSource.XO_SHI_RO_512_SS, new long[] {123L, -654654L, 45646789L});
83              add(LIST64, RandomSource.PCG_RXS_M_XS_64, new long[] {42088L, 69271L});
84              add(LIST64, RandomSource.SFC_64, new long[] {-2357423478979842L, 76543434515L});
85              add(LIST64, RandomSource.XO_RO_SHI_RO_128_PP, new long[] {789741321465L, -461321684612L, -12301654794L});
86              add(LIST64, RandomSource.XO_SHI_RO_256_PP, new long[] {2374243L, -8097397345383L, -223479293943L});
87              add(LIST64, RandomSource.XO_SHI_RO_512_PP, new long[] {-1210684761321465L, -485132198745L, 89942134798523L});
88              add(LIST64, RandomSource.XO_RO_SHI_RO_1024_PP, new long[] {236424345654L, 781544546164721L, -85235476312346L});
89              add(LIST64, RandomSource.XO_RO_SHI_RO_1024_S, new long[] {-1574314L, 7879874453221215L, -7894343883216L});
90              add(LIST64, RandomSource.XO_RO_SHI_RO_1024_SS, new long[] {-41514541234654321L, -12146412316546L, 7984134134L});
91              // ... add more here.
92  
93              // Do not modify the remaining statements.
94              // Complete list.
95              LIST.addAll(LIST32);
96              LIST.addAll(LIST64);
97          } catch (Exception e) {
98              // CHECKSTYLE: stop Regexp
99              System.err.println("Unexpected exception while creating the list of generators: " + e);
100             e.printStackTrace(System.err);
101             // CHECKSTYLE: resume Regexp
102             throw new RuntimeException(e);
103         }
104     }
105 
106     /**
107      * Class contains only static methods.
108      */
109     private ProvidersList() {}
110 
111     /**
112      * Helper to statisfy Junit requirement that each parameter set contains
113      * the same number of objects.
114      */
115     private static void add(List<Data[]> list,
116                             RandomSource source,
117                             Object... data) {
118         final RandomSource rng = source;
119         final Object seed = data.length > 0 ? data[0] : null;
120         final Object[] args = data.length > 1 ? Arrays.copyOfRange(data, 1, data.length) : null;
121 
122         list.add(new Data[] {new Data(rng, seed, args)});
123     }
124 
125     /**
126      * Subclasses that are "parametric" tests can forward the call to
127      * the "@Parameters"-annotated method to this method.
128      *
129      * @return the list of all generators.
130      */
131     public static Iterable<Data[]> list() {
132         return Collections.unmodifiableList(LIST);
133     }
134 
135     /**
136      * Subclasses that are "parametric" tests can forward the call to
137      * the "@Parameters"-annotated method to this method.
138      *
139      * @return the list of 32-bits based generators.
140      */
141     public static Iterable<Data[]> list32() {
142         return Collections.unmodifiableList(LIST32);
143     }
144 
145     /**
146      * Subclasses that are "parametric" tests can forward the call to
147      * the "@Parameters"-annotated method to this method.
148      *
149      * @return the list of 64-bits based generators.
150      */
151     public static Iterable<Data[]> list64() {
152         return Collections.unmodifiableList(LIST64);
153     }
154 
155     /**
156      * Helper.
157      * Better not to mix Junit assumptions of the usage of "Object[]".
158      */
159     public static class Data {
160         private final RandomSource source;
161         private final Object seed;
162         private final Object[] args;
163 
164         public Data(RandomSource source,
165                     Object seed,
166                     Object[] args) {
167             this.source = source;
168             this.seed = seed;
169             this.args = args;
170         }
171 
172         public RandomSource getSource() {
173             return source;
174         }
175 
176         public Object getSeed() {
177             return seed;
178         }
179 
180         public Object[] getArgs() {
181             return args == null ? null : Arrays.copyOf(args, args.length);
182         }
183 
184         @Override
185         public String toString() {
186             return source.toString() + " seed=" + seed + " args=" + Arrays.toString(args);
187         }
188     }
189 }