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.List;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.security.SecureRandom;
23  
24  import org.apache.commons.rng.core.source32.JDKRandom;
25  import org.apache.commons.rng.core.source32.JenkinsSmallFast32;
26  import org.apache.commons.rng.core.source32.Well512a;
27  import org.apache.commons.rng.core.source32.XoRoShiRo64Star;
28  import org.apache.commons.rng.core.source32.XoRoShiRo64StarStar;
29  import org.apache.commons.rng.core.source32.XoShiRo128Plus;
30  import org.apache.commons.rng.core.source32.XoShiRo128PlusPlus;
31  import org.apache.commons.rng.core.source32.XoShiRo128StarStar;
32  import org.apache.commons.rng.core.source32.Well1024a;
33  import org.apache.commons.rng.core.source32.Well19937a;
34  import org.apache.commons.rng.core.source32.Well19937c;
35  import org.apache.commons.rng.core.source32.Well44497a;
36  import org.apache.commons.rng.core.source32.Well44497b;
37  import org.apache.commons.rng.core.source32.ISAACRandom;
38  import org.apache.commons.rng.core.source32.MersenneTwister;
39  import org.apache.commons.rng.core.source32.MiddleSquareWeylSequence;
40  import org.apache.commons.rng.core.source32.MultiplyWithCarry256;
41  import org.apache.commons.rng.core.source32.KISSRandom;
42  import org.apache.commons.rng.core.source32.PcgXshRr32;
43  import org.apache.commons.rng.core.source32.PcgXshRs32;
44  import org.apache.commons.rng.core.source32.DotyHumphreySmallFastCounting32;
45  import org.apache.commons.rng.core.source32.PcgMcgXshRr32;
46  import org.apache.commons.rng.core.source32.PcgMcgXshRs32;
47  import org.apache.commons.rng.core.source64.SplitMix64;
48  import org.apache.commons.rng.core.source64.XorShift1024Star;
49  import org.apache.commons.rng.core.source64.XorShift1024StarPhi;
50  import org.apache.commons.rng.core.source64.TwoCmres;
51  import org.apache.commons.rng.core.source64.XoRoShiRo1024PlusPlus;
52  import org.apache.commons.rng.core.source64.XoRoShiRo1024Star;
53  import org.apache.commons.rng.core.source64.XoRoShiRo1024StarStar;
54  import org.apache.commons.rng.core.source64.XoRoShiRo128Plus;
55  import org.apache.commons.rng.core.source64.XoRoShiRo128PlusPlus;
56  import org.apache.commons.rng.core.source64.XoRoShiRo128StarStar;
57  import org.apache.commons.rng.core.source64.XoShiRo256Plus;
58  import org.apache.commons.rng.core.source64.XoShiRo256PlusPlus;
59  import org.apache.commons.rng.core.source64.XoShiRo256StarStar;
60  import org.apache.commons.rng.core.source64.XoShiRo512Plus;
61  import org.apache.commons.rng.core.source64.XoShiRo512PlusPlus;
62  import org.apache.commons.rng.core.source64.XoShiRo512StarStar;
63  import org.apache.commons.rng.core.source64.JenkinsSmallFast64;
64  import org.apache.commons.rng.core.source64.MersenneTwister64;
65  import org.apache.commons.rng.core.source64.PcgRxsMXs64;
66  import org.apache.commons.rng.core.source64.DotyHumphreySmallFastCounting64;
67  import org.apache.commons.rng.JumpableUniformRandomProvider;
68  import org.apache.commons.rng.RestorableUniformRandomProvider;
69  
70  /**
71   * The purpose of this class is to provide the list of all generators
72   * implemented in the library.
73   * The list must be updated with each new RNG implementation.
74   *
75   * @see #list()
76   * @see #list32()
77   * @see #list64()
78   */
79  public final class ProvidersList {
80      /** List of all RNGs implemented in the library. */
81      private static final List<RestorableUniformRandomProvider[]> LIST =
82          new ArrayList<RestorableUniformRandomProvider[]>();
83      /** List of 32-bits based RNGs. */
84      private static final List<RestorableUniformRandomProvider[]> LIST32 =
85          new ArrayList<RestorableUniformRandomProvider[]>();
86      /** List of 64-bits based RNGs. */
87      private static final List<RestorableUniformRandomProvider[]> LIST64 =
88          new ArrayList<RestorableUniformRandomProvider[]>();
89      /** List of {@link JumpableUniformRandomProvider} RNGs. */
90      private static final List<JumpableUniformRandomProvider[]> LIST_JUMP =
91          new ArrayList<JumpableUniformRandomProvider[]>();
92  
93      static {
94          // External generator for creating a random seed.
95          final SecureRandom g = new SecureRandom();
96  
97          try {
98              // "int"-based RNGs.
99              add(LIST32, new JDKRandom(g.nextLong()));
100             add(LIST32, new MersenneTwister(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
101             add(LIST32, new Well512a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
102             add(LIST32, new Well1024a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
103             add(LIST32, new Well19937a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
104             add(LIST32, new Well19937c(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
105             add(LIST32, new Well44497a(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
106             add(LIST32, new Well44497b(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
107             add(LIST32, new ISAACRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
108             add(LIST32, new MultiplyWithCarry256(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
109             add(LIST32, new KISSRandom(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
110             add(LIST32, new XoRoShiRo64Star(new int[] {g.nextInt(), g.nextInt()}));
111             add(LIST32, new XoRoShiRo64StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
112             add(LIST32, new XoShiRo128Plus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
113             add(LIST32, new XoShiRo128StarStar(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
114             add(LIST32, new PcgXshRr32(new long[] {g.nextLong()}));
115             add(LIST32, new PcgXshRs32(new long[] {g.nextLong()}));
116             add(LIST32, new PcgMcgXshRr32(g.nextLong()));
117             add(LIST32, new PcgMcgXshRs32(g.nextLong()));
118             // Ensure a high complexity increment is used for the Weyl sequence
119             add(LIST32, new MiddleSquareWeylSequence(new long[] {g.nextLong(), g.nextLong(), 0xb5ad4eceda1ce2a9L}));
120             add(LIST32, new DotyHumphreySmallFastCounting32(new int[] {g.nextInt(), g.nextInt()}));
121             add(LIST32, new JenkinsSmallFast32(g.nextInt()));
122             add(LIST32, new XoShiRo128PlusPlus(new int[] {g.nextInt(), g.nextInt(), g.nextInt()}));
123             // ... add more here.
124 
125             // "long"-based RNGs.
126             add(LIST64, new SplitMix64(g.nextLong()));
127             add(LIST64, new XorShift1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
128             add(LIST64, new XorShift1024StarPhi(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
129             add(LIST64, new TwoCmres(g.nextInt()));
130             add(LIST64, new TwoCmres(g.nextInt(), 5, 8));
131             add(LIST64, new MersenneTwister64(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
132             add(LIST64, new XoRoShiRo128Plus(new long[] {g.nextLong(), g.nextLong()}));
133             add(LIST64, new XoRoShiRo128StarStar(new long[] {g.nextLong(), g.nextLong()}));
134             add(LIST64, new XoShiRo256Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
135             add(LIST64, new XoShiRo256StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
136             add(LIST64, new XoShiRo512Plus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
137             add(LIST64, new XoShiRo512StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
138             add(LIST64, new PcgRxsMXs64(new long[] {g.nextLong()}));
139             add(LIST64, new DotyHumphreySmallFastCounting64(new long[] {g.nextLong(), g.nextLong()}));
140             add(LIST64, new JenkinsSmallFast64(g.nextLong()));
141             add(LIST64, new XoRoShiRo128PlusPlus(new long[] {g.nextLong(), g.nextLong()}));
142             add(LIST64, new XoShiRo256PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
143             add(LIST64, new XoShiRo512PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
144             add(LIST64, new XoRoShiRo1024PlusPlus(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
145             add(LIST64, new XoRoShiRo1024Star(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
146             add(LIST64, new XoRoShiRo1024StarStar(new long[] {g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong()}));
147             // ... add more here.
148 
149             // Do not modify the remaining statements.
150             // Complete list.
151             LIST.addAll(LIST32);
152             LIST.addAll(LIST64);
153             // Dynamically identify the Jumpable RNGs
154             for (RestorableUniformRandomProvider[] rng : LIST) {
155                 if (rng[0] instanceof JumpableUniformRandomProvider) {
156                     add(LIST_JUMP, (JumpableUniformRandomProvider) rng[0]);
157                 }
158             }
159         } catch (Exception e) {
160             // CHECKSTYLE: stop Regexp
161             System.err.println("Unexpected exception while creating the list of generators: " + e);
162             e.printStackTrace(System.err);
163             // CHECKSTYLE: resume Regexp
164             throw new RuntimeException(e);
165         }
166     }
167 
168     /**
169      * Class contains only static methods.
170      */
171     private ProvidersList() {}
172 
173     /**
174      * Helper to statisfy Junit requirement that each parameter set contains
175      * the same number of objects.
176      */
177     private static void add(List<RestorableUniformRandomProvider[]> list,
178                             RestorableUniformRandomProvider rng) {
179         list.add(new RestorableUniformRandomProvider[] {rng});
180     }
181 
182     /**
183      * Helper to statisfy Junit requirement that each parameter set contains
184      * the same number of objects.
185      */
186     private static void add(List<JumpableUniformRandomProvider[]> list,
187                             JumpableUniformRandomProvider rng) {
188         list.add(new JumpableUniformRandomProvider[] {rng});
189     }
190 
191     /**
192      * Subclasses that are "parametric" tests can forward the call to
193      * the "@Parameters"-annotated method to this method.
194      *
195      * @return the list of all generators.
196      */
197     public static Iterable<RestorableUniformRandomProvider[]> list() {
198         return Collections.unmodifiableList(LIST);
199     }
200 
201     /**
202      * Subclasses that are "parametric" tests can forward the call to
203      * the "@Parameters"-annotated method to this method.
204      *
205      * @return the list of 32-bits based generators.
206      */
207     public static Iterable<RestorableUniformRandomProvider[]> list32() {
208         return Collections.unmodifiableList(LIST32);
209     }
210 
211     /**
212      * Subclasses that are "parametric" tests can forward the call to
213      * the "@Parameters"-annotated method to this method.
214      *
215      * @return the list of 64-bits based generators.
216      */
217     public static Iterable<RestorableUniformRandomProvider[]> list64() {
218         return Collections.unmodifiableList(LIST64);
219     }
220 
221     /**
222      * Subclasses that are "parametric" tests can forward the call to
223      * the "@Parameters"-annotated method to this method.
224      *
225      * @return the list of {@link JumpableUniformRandomProvider} generators.
226      */
227     public static Iterable<JumpableUniformRandomProvider[]> listJumpable() {
228         return Collections.unmodifiableList(LIST_JUMP);
229     }
230 }