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  
18  package org.apache.commons.rng.examples.sampling;
19  
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.apache.commons.rng.simple.RandomSource;
22  import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
23  import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
24  import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
25  import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
26  
27  /**
28   * Creates 2D plot of sampling output.
29   * It is a "manual" check that could help ensure that no artefacts 
30   * exist in some tiny region of the expected range, due to loss of
31   * accuracy, e.g. when porting C code based on 32-bits "float" to
32   * "Commons RNG" that uses Java "double" (64-bits).
33   */
34  public class UniformSamplingVisualCheck {
35      /** RNG. */
36      private final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S);
37      /** Samplers. */
38      private final ContinuousSampler[] samplers = new ContinuousSampler[] {
39          new ZigguratNormalizedGaussianSampler(rng),
40          new MarsagliaNormalizedGaussianSampler(rng),
41          new BoxMullerNormalizedGaussianSampler(rng),
42      };
43  
44      /**
45       * Program entry point.
46       *
47       * @param args Unused.
48       */
49      public static void main(String[] args) {
50          final float lo = 0.1f;
51          final int bands = 2;
52          float hi = lo;
53          for (int i = 0; i < bands; i++) {
54              hi = Math.nextUp(hi);
55          }
56          System.out.printf("# lo=%.10e hi=%.10e", lo, hi);
57          System.out.println();
58  
59          final UniformSamplingVisualCheck app = new UniformSamplingVisualCheck();
60  
61          while (true) {
62              System.out.printf("%.16e\t", app.rng.nextDouble());
63  
64              for (ContinuousSampler s : app.samplers) {
65                  while (true) {
66                      final double r = s.sample();
67                      if (r < lo ||
68                          r > hi) {
69                          // Discard numbers outside the tiny region.
70                           continue;
71                       }
72  
73                      System.out.printf("%.16e ", r);
74                      break;
75                  }
76              }
77  
78              System.out.println();
79          }
80      }
81  }