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.sampling.distribution;
18  
19  import org.apache.commons.rng.UniformRandomProvider;
20  
21  /**
22   * Sampling from a uniform distribution.
23   *
24   * <p>Sampling uses {@link UniformRandomProvider#nextDouble()}.</p>
25   *
26   * @since 1.0
27   */
28  public class ContinuousUniformSampler
29      extends SamplerBase
30      implements SharedStateContinuousSampler {
31      /** Lower bound. */
32      private final double lo;
33      /** Higher bound. */
34      private final double hi;
35      /** Underlying source of randomness. */
36      private final UniformRandomProvider rng;
37  
38      /**
39       * @param rng Generator of uniformly distributed random numbers.
40       * @param lo Lower bound.
41       * @param hi Higher bound.
42       */
43      public ContinuousUniformSampler(UniformRandomProvider rng,
44                                      double lo,
45                                      double hi) {
46          super(null);
47          this.rng = rng;
48          this.lo = lo;
49          this.hi = hi;
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public double sample() {
55          final double u = rng.nextDouble();
56          return u * hi + (1 - u) * lo;
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public String toString() {
62          return "Uniform deviate [" + rng.toString() + "]";
63      }
64  
65      /**
66       * {@inheritDoc}
67       *
68       * @since 1.3
69       */
70      @Override
71      public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider rng) {
72          return new ContinuousUniformSampler(rng, lo, hi);
73      }
74  
75      /**
76       * Creates a new continuous uniform distribution sampler.
77       *
78       * @param rng Generator of uniformly distributed random numbers.
79       * @param lo Lower bound.
80       * @param hi Higher bound.
81       * @return the sampler
82       * @since 1.3
83       */
84      public static SharedStateContinuousSampler of(UniformRandomProvider rng,
85                                                    double lo,
86                                                    double hi) {
87          return new ContinuousUniformSampler(rng, lo, hi);
88      }
89  }