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  import java.security.SecureRandom;
24  
25  import org.apache.commons.rng.core.source32.JDKRandom;
26  import org.apache.commons.rng.core.source32.Well512a;
27  import org.apache.commons.rng.core.source32.Well1024a;
28  import org.apache.commons.rng.core.source32.Well19937a;
29  import org.apache.commons.rng.core.source32.Well19937c;
30  import org.apache.commons.rng.core.source32.Well44497a;
31  import org.apache.commons.rng.core.source32.Well44497b;
32  import org.apache.commons.rng.core.source32.ISAACRandom;
33  import org.apache.commons.rng.core.source32.MersenneTwister;
34  import org.apache.commons.rng.core.source32.MultiplyWithCarry256;
35  import org.apache.commons.rng.core.source32.KISSRandom;
36  import org.apache.commons.rng.core.source64.SplitMix64;
37  import org.apache.commons.rng.core.source64.XorShift1024Star;
38  import org.apache.commons.rng.core.source64.TwoCmres;
39  import org.apache.commons.rng.core.source64.MersenneTwister64;
40  import org.apache.commons.rng.RestorableUniformRandomProvider;
41  
42  /**
43   * The purpose of this class is to provide the list of all generators
44   * implemented in the library.
45   * The list must be updated with each new RNG implementation.
46   *
47   * @see #list()
48   * @see #list32()
49   * @see #list64()
50   */
51  public class ProvidersList {
52      /** List of all RNGs implemented in the library. */
53      private static final List<RestorableUniformRandomProvider[]> LIST =
54          new ArrayList<RestorableUniformRandomProvider[]>();
55      /** List of 32-bits based RNGs. */
56      private static final List<RestorableUniformRandomProvider[]> LIST32 =
57          new ArrayList<RestorableUniformRandomProvider[]>();
58      /** List of 64-bits based RNGs. */
59      private static final List<RestorableUniformRandomProvider[]> LIST64 =
60          new ArrayList<RestorableUniformRandomProvider[]>();
61  
62      static {
63          // External generator for creating a random seed.
64          final SecureRandom g = new SecureRandom();
65  
66          try {
67              // "int"-based RNGs.
68              add(LIST32, new JDKRandom(g.nextLong()));
69              add(LIST32, new MersenneTwister(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
70              add(LIST32, new Well512a(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
71              add(LIST32, new Well1024a(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
72              add(LIST32, new Well19937a(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
73              add(LIST32, new Well19937c(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
74              add(LIST32, new Well44497a(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
75              add(LIST32, new Well44497b(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
76              add(LIST32, new ISAACRandom(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
77              add(LIST32, new MultiplyWithCarry256(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
78              add(LIST32, new KISSRandom(new int[] { g.nextInt(), g.nextInt(), g.nextInt() }));
79              // ... add more here.
80  
81              // "long"-based RNGs.
82              add(LIST64, new SplitMix64(g.nextLong()));
83              add(LIST64, new XorShift1024Star(new long[] { g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong() }));
84              add(LIST64, new TwoCmres(g.nextInt()));
85              add(LIST64, new TwoCmres(g.nextInt(), 5, 8));
86              add(LIST64, new MersenneTwister64(new long[] { g.nextLong(), g.nextLong(), g.nextLong(), g.nextLong() }));
87              // ... add more here.
88  
89              // Do not modify the remaining statements.
90              // Complete list.
91              LIST.addAll(LIST32);
92              LIST.addAll(LIST64);
93          } catch (Exception e) {
94              System.err.println("Unexpected exception while creating the list of generators: " + e);
95              e.printStackTrace(System.err);
96              throw new RuntimeException(e);
97          }
98      }
99  
100     /**
101      * Class contains only static methods.
102      */
103     private ProvidersList() {}
104 
105     /**
106      * Helper to statisfy Junit requirement that each parameter set contains
107      * the same number of objects.
108      */
109     private static void add(List<RestorableUniformRandomProvider[]> list,
110                             RestorableUniformRandomProvider rng) {
111         list.add(new RestorableUniformRandomProvider[] { rng });
112     }
113 
114     /**
115      * Subclasses that are "parametric" tests can forward the call to
116      * the "@Parameters"-annotated method to this method.
117      *
118      * @return the list of all generators.
119      */
120     public static Iterable<RestorableUniformRandomProvider[]> list() {
121         return Collections.unmodifiableList(LIST);
122     }
123 
124     /**
125      * Subclasses that are "parametric" tests can forward the call to
126      * the "@Parameters"-annotated method to this method.
127      *
128      * @return the list of 32-bits based generators.
129      */
130     public static Iterable<RestorableUniformRandomProvider[]> list32() {
131         return Collections.unmodifiableList(LIST32);
132     }
133 
134     /**
135      * Subclasses that are "parametric" tests can forward the call to
136      * the "@Parameters"-annotated method to this method.
137      *
138      * @return the list of 64-bits based generators.
139      */
140     public static Iterable<RestorableUniformRandomProvider[]> list64() {
141         return Collections.unmodifiableList(LIST64);
142     }
143 }