View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.lang.math;
20  
21  import java.util.Random;
22  
23  /**
24   * <p><code>RandomUtils</code> is a wrapper that supports all possible 
25   * {@link java.util.Random} methods via the {@link java.lang.Math#random()}
26   * method and its system-wide <code>Random</code> object.
27   * 
28   * @author Gary D. Gregory
29   * @since 2.0
30   * @version $Id: RandomUtils.java 471626 2006-11-06 04:02:09Z bayard $
31   */
32  public class RandomUtils {
33  
34      /**
35       * An instance of {@link JVMRandom}.
36       */
37      public static final Random JVM_RANDOM = new JVMRandom();
38  
39  // should be possible for JVM_RANDOM?
40  //    public static void nextBytes(byte[]) {
41  //    public synchronized double nextGaussian();
42  //    }
43  
44      /**
45       * <p>Returns the next pseudorandom, uniformly distributed int value
46       * from the Math.random() sequence.</p>
47       *
48       * @return the random int
49       */
50      public static int nextInt() {
51          return nextInt(JVM_RANDOM);
52      }
53      
54      /**
55       * <p>Returns the next pseudorandom, uniformly distributed int value
56       * from the given <code>random</code> sequence.</p>
57       *
58       * @param random the Random sequence generator.
59       * @return the random int
60       */
61      public static int nextInt(Random random) {
62          return random.nextInt();
63      }
64      
65      /**
66       * <p>Returns a pseudorandom, uniformly distributed int value
67       * between <code>0</code> (inclusive) and the specified value
68       * (exclusive), from the Math.random() sequence.</p>
69       *
70       * @param n  the specified exclusive max-value
71       * @return the random int
72       */
73      public static int nextInt(int n) {
74          return nextInt(JVM_RANDOM, n);
75      }
76      
77      /**
78       * <p>Returns a pseudorandom, uniformly distributed int value
79       * between <code>0</code> (inclusive) and the specified value
80       * (exclusive), from the given Random sequence.</p>
81       *
82       * @param random the Random sequence generator.
83       * @param n  the specified exclusive max-value
84       * @return the random int
85       */
86      public static int nextInt(Random random, int n) {
87          // check this cannot return 'n'
88          return random.nextInt(n);
89      }
90      
91      /**
92       * <p>Returns the next pseudorandom, uniformly distributed long value
93       * from the Math.random() sequence.</p>
94       *
95       * @return the random long
96       */
97      public static long nextLong() {
98          return nextLong(JVM_RANDOM);
99      }
100 
101     /**
102      * <p>Returns the next pseudorandom, uniformly distributed long value
103      * from the given Random sequence.</p>
104      *
105      * @param random the Random sequence generator.
106      * @return the random long
107      */
108     public static long nextLong(Random random) {
109         return random.nextLong();
110     }
111     
112     /**
113      * <p>Returns the next pseudorandom, uniformly distributed boolean value
114      * from the Math.random() sequence.</p>
115      *
116      * @return the random boolean
117      */
118     public static boolean nextBoolean() {
119         return nextBoolean(JVM_RANDOM);
120     }
121 
122     /**
123      * <p>Returns the next pseudorandom, uniformly distributed boolean value
124      * from the given random sequence.</p>
125      *
126      * @param random the Random sequence generator.
127      * @return the random boolean
128      */
129     public static boolean nextBoolean(Random random) {
130         return random.nextBoolean();
131     }
132     
133     /**
134      * <p>Returns the next pseudorandom, uniformly distributed float value
135      * between <code>0.0</code> and <code>1.0</code> from the Math.random()
136      * sequence.</p>
137      *
138      * @return the random float
139      */
140     public static float nextFloat() {
141         return nextFloat(JVM_RANDOM);
142     }
143 
144     /**
145      * <p>Returns the next pseudorandom, uniformly distributed float value
146      * between <code>0.0</code> and <code>1.0</code> from the given Random
147      * sequence.</p>
148      *
149      * @param random the Random sequence generator.
150      * @return the random float
151      */
152     public static float nextFloat(Random random) {
153         return random.nextFloat();
154     }
155     
156     /**
157      * <p>Returns the next pseudorandom, uniformly distributed float value
158      * between <code>0.0</code> and <code>1.0</code> from the Math.random()
159      * sequence.</p>
160      *
161      * @return the random double
162      */
163     public static double nextDouble() {
164         return nextDouble(JVM_RANDOM);
165     }
166 
167     /**
168      * <p>Returns the next pseudorandom, uniformly distributed float value
169      * between <code>0.0</code> and <code>1.0</code> from the given Random
170      * sequence.</p>
171      *
172      * @param random the Random sequence generator.
173      * @return the random double
174      */
175     public static double nextDouble(Random random) {
176         return random.nextDouble();
177     }
178     
179 }