KickJava   Java API By Example, From Geeks To Geeks.

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


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>JVMRandom</code> is a wrapper that supports all possible
22  * Random methods via the {@link java.lang.Math#random()} method
23  * and its system-wide {@link Random} object.</p>
24  *
25  * @author Henri Yandell
26  * @since 2.0
27  * @version $Id: JVMRandom.java 161229 2005-04-13 22:36:48Z ggregory $
28  */

29 public final class JVMRandom extends Random JavaDoc {
30
31     /**
32      * Ensures that only the constructor can call reseed.
33      */

34     private boolean constructed = false;
35
36     /**
37      * Constructs a new instance.
38      */

39     public JVMRandom() {
40         this.constructed = true;
41     }
42     
43     /**
44      * Unsupported in 2.0.
45      *
46      * @param seed ignored
47      * @throws UnsupportedOperationException
48      */

49     public synchronized void setSeed(long seed) {
50         if (this.constructed) {
51             throw new UnsupportedOperationException JavaDoc();
52         }
53     }
54
55     /**
56      * Unsupported in 2.0.
57      *
58      * @return Nothing, this method always throws an UnsupportedOperationException.
59      * @throws UnsupportedOperationException
60      */

61     public synchronized double nextGaussian() {
62         throw new UnsupportedOperationException JavaDoc();
63     }
64
65     /**
66      * Unsupported in 2.0.
67      *
68      * @param byteArray ignored
69      * @throws UnsupportedOperationException
70      */

71     public void nextBytes(byte[] byteArray) {
72         throw new UnsupportedOperationException JavaDoc();
73     }
74
75     /**
76      * <p>Returns the next pseudorandom, uniformly distributed int value
77      * from the Math.random() sequence.</p>
78      *
79      * @return the random int
80      */

81     public int nextInt() {
82         return nextInt(Integer.MAX_VALUE);
83     }
84     /**
85      * <p>Returns a pseudorandom, uniformly distributed int value between
86      * <code>0</code> (inclusive) and the specified value (exclusive), from
87      * the Math.random() sequence.</p>
88      *
89      * @param n the specified exclusive max-value
90      * @return the random int
91      * @throws IllegalArgumentException when <code>n &lt;= 0</code>
92      */

93     public int nextInt(int n) {
94         if (n <= 0) {
95             throw new IllegalArgumentException JavaDoc(
96                 "Upper bound for nextInt must be positive"
97             );
98         }
99         // TODO: check this cannot return 'n'
100
return (int)(Math.random() * n);
101     }
102     /**
103      * <p>Returns the next pseudorandom, uniformly distributed long value
104      * from the Math.random() sequence.</p>
105      * @return the random long
106      */

107     public long nextLong() {
108         // possible loss of precision?
109
return nextLong(Long.MAX_VALUE);
110     }
111
112
113     /**
114      * <p>Returns a pseudorandom, uniformly distributed long value between
115      * <code>0</code> (inclusive) and the specified value (exclusive), from
116      * the Math.random() sequence.</p>
117      *
118      * @param n the specified exclusive max-value
119      * @return the random long
120      * @throws IllegalArgumentException when <code>n &lt;= 0</code>
121      */

122     public static long nextLong(long n) {
123         if (n <= 0) {
124             throw new IllegalArgumentException JavaDoc(
125                 "Upper bound for nextInt must be positive"
126             );
127         }
128         // TODO: check this cannot return 'n'
129
return (long)(Math.random() * n);
130      }
131
132     /**
133      * <p>Returns the next pseudorandom, uniformly distributed boolean value
134      * from the Math.random() sequence.</p>
135      *
136      * @return the random boolean
137      */

138     public boolean nextBoolean() {
139         return Math.random() > 0.5;
140     }
141     /**
142      * <p>Returns the next pseudorandom, uniformly distributed float value
143      * between <code>0.0</code> and <code>1.0</code> from the Math.random()
144      * sequence.</p>
145      *
146      * @return the random float
147      */

148     public float nextFloat() {
149         return (float)Math.random();
150     }
151     /**
152      * <p>Synonymous to the Math.random() call.</p>
153      *
154      * @return the random double
155      */

156     public double nextDouble() {
157         return Math.random();
158     }
159     
160 }
161
Popular Tags