KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > math > RandomUtils


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.math;
17
18 import java.util.Random JavaDoc;
19
20 /**
21  * <p><code>RandomUtils</code> is a wrapper that supports all possible
22  * {@link java.util.Random} methods via the {@link java.lang.Math#random()}
23  * method and its system-wide <code>Random</code> object.
24  *
25  * @author Henri Yandell
26  * @author Gary D. Gregory
27  * @since 2.0
28  * @version $Id: RandomUtils.java 155423 2005-02-26 13:08:30Z dirkv $
29  */

30 public class RandomUtils {
31
32     /**
33      * An instance of {@link JVMRandom}.
34      */

35     public static final Random JavaDoc JVM_RANDOM = new JVMRandom();
36
37 // should be possible for JVM_RANDOM?
38
// public static void nextBytes(byte[]) {
39
// public synchronized double nextGaussian();
40
// }
41

42     /**
43      * <p>Returns the next pseudorandom, uniformly distributed int value
44      * from the Math.random() sequence.</p>
45      *
46      * @return the random int
47      */

48     public static int nextInt() {
49         return nextInt(JVM_RANDOM);
50     }
51     
52     /**
53      * <p>Returns the next pseudorandom, uniformly distributed int value
54      * from the given <code>random</code> sequence.</p>
55      *
56      * @param random the Random sequence generator.
57      * @return the random int
58      */

59     public static int nextInt(Random JavaDoc random) {
60         return random.nextInt();
61     }
62     
63     /**
64      * <p>Returns a pseudorandom, uniformly distributed int value
65      * between <code>0</code> (inclusive) and the specified value
66      * (exclusive), from the Math.random() sequence.</p>
67      *
68      * @param n the specified exclusive max-value
69      * @return the random int
70      */

71     public static int nextInt(int n) {
72         return nextInt(JVM_RANDOM, n);
73     }
74     
75     /**
76      * <p>Returns a pseudorandom, uniformly distributed int value
77      * between <code>0</code> (inclusive) and the specified value
78      * (exclusive), from the given Random sequence.</p>
79      *
80      * @param random the Random sequence generator.
81      * @param n the specified exclusive max-value
82      * @return the random int
83      */

84     public static int nextInt(Random JavaDoc random, int n) {
85         // check this cannot return 'n'
86
return random.nextInt(n);
87     }
88     
89     /**
90      * <p>Returns the next pseudorandom, uniformly distributed long value
91      * from the Math.random() sequence.</p>
92      *
93      * @return the random long
94      */

95     public static long nextLong() {
96         return nextLong(JVM_RANDOM);
97     }
98
99     /**
100      * <p>Returns the next pseudorandom, uniformly distributed long value
101      * from the given Random sequence.</p>
102      *
103      * @param random the Random sequence generator.
104      * @return the random long
105      */

106     public static long nextLong(Random JavaDoc random) {
107         return random.nextLong();
108     }
109     
110     /**
111      * <p>Returns the next pseudorandom, uniformly distributed boolean value
112      * from the Math.random() sequence.</p>
113      *
114      * @return the random boolean
115      */

116     public static boolean nextBoolean() {
117         return nextBoolean(JVM_RANDOM);
118     }
119
120     /**
121      * <p>Returns the next pseudorandom, uniformly distributed boolean value
122      * from the given random sequence.</p>
123      *
124      * @param random the Random sequence generator.
125      * @return the random boolean
126      */

127     public static boolean nextBoolean(Random JavaDoc random) {
128         return random.nextBoolean();
129     }
130     
131     /**
132      * <p>Returns the next pseudorandom, uniformly distributed float value
133      * between <code>0.0</code> and <code>1.0</code> from the Math.random()
134      * sequence.</p>
135      *
136      * @return the random float
137      */

138     public static float nextFloat() {
139         return nextFloat(JVM_RANDOM);
140     }
141
142     /**
143      * <p>Returns the next pseudorandom, uniformly distributed float value
144      * between <code>0.0</code> and <code>1.0</code> from the given Random
145      * sequence.</p>
146      *
147      * @param random the Random sequence generator.
148      * @return the random float
149      */

150     public static float nextFloat(Random JavaDoc random) {
151         return random.nextFloat();
152     }
153     
154     /**
155      * <p>Returns the next pseudorandom, uniformly distributed float value
156      * between <code>0.0</code> and <code>1.0</code> from the Math.random()
157      * sequence.</p>
158      *
159      * @return the random double
160      */

161     public static double nextDouble() {
162         return nextDouble(JVM_RANDOM);
163     }
164
165     /**
166      * <p>Returns the next pseudorandom, uniformly distributed float value
167      * between <code>0.0</code> and <code>1.0</code> from the given Random
168      * sequence.</p>
169      *
170      * @param random the Random sequence generator.
171      * @return the random double
172      */

173     public static double nextDouble(Random JavaDoc random) {
174         return random.nextDouble();
175     }
176     
177 }
178
Popular Tags