1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang.math;
  55. import java.util.Random;
  56. /**
  57. * <p><code>JVMRandom</code> is a wrapper that supports all possible
  58. * Random methods via the {@link java.lang.Math#random()} method
  59. * and its system-wide {@link Random} object.</p>
  60. *
  61. * @author Henri Yandell
  62. * @since 2.0
  63. * @version $Id: JVMRandom.java,v 1.8 2003/08/18 02:22:24 bayard Exp $
  64. */
  65. public final class JVMRandom extends Random {
  66. /**
  67. * Ensures that only the constructor can call reseed.
  68. */
  69. private boolean constructed = false;
  70. public JVMRandom() {
  71. this.constructed = true;
  72. }
  73. /**
  74. * Unsupported in 2.0.
  75. */
  76. public synchronized void setSeed(long seed) {
  77. if (this.constructed) {
  78. throw new UnsupportedOperationException();
  79. }
  80. }
  81. /**
  82. * Unsupported in 2.0.
  83. */
  84. public synchronized double nextGaussian() {
  85. throw new UnsupportedOperationException();
  86. }
  87. /**
  88. * Unsupported in 2.0.
  89. */
  90. public void nextBytes(byte[] byteArray) {
  91. throw new UnsupportedOperationException();
  92. }
  93. /**
  94. * <p>Returns the next pseudorandom, uniformly distributed int value
  95. * from the Math.random() sequence.</p>
  96. *
  97. * @return the random int
  98. */
  99. public int nextInt() {
  100. return nextInt(Integer.MAX_VALUE);
  101. }
  102. /**
  103. * <p>Returns a pseudorandom, uniformly distributed int value between
  104. * <code>0</code> (inclusive) and the specified value (exclusive), from
  105. * the Math.random() sequence.</p>
  106. *
  107. * @param n the specified exclusive max-value
  108. * @return the random int
  109. * @throws IllegalArgumentException when <code>n <= 0</code>
  110. */
  111. public int nextInt(int n) {
  112. if (n <= 0) {
  113. throw new IllegalArgumentException(
  114. "Upper bound for nextInt must be positive"
  115. );
  116. }
  117. // TODO: check this cannot return 'n'
  118. return (int)(Math.random() * n);
  119. }
  120. /**
  121. * <p>Returns the next pseudorandom, uniformly distributed long value
  122. * from the Math.random() sequence.</p>
  123. * @return the random long
  124. */
  125. public long nextLong() {
  126. // possible loss of precision?
  127. return nextLong(Long.MAX_VALUE);
  128. }
  129. /**
  130. * <p>Returns a pseudorandom, uniformly distributed long value between
  131. * <code>0</code> (inclusive) and the specified value (exclusive), from
  132. * the Math.random() sequence.</p>
  133. *
  134. * @param n the specified exclusive max-value
  135. * @return the random long
  136. * @throws IllegalArgumentException when <code>n <= 0</code>
  137. */
  138. public static long nextLong(long n) {
  139. if (n <= 0) {
  140. throw new IllegalArgumentException(
  141. "Upper bound for nextInt must be positive"
  142. );
  143. }
  144. // TODO: check this cannot return 'n'
  145. return (long)(Math.random() * n);
  146. }
  147. /**
  148. * <p>Returns the next pseudorandom, uniformly distributed boolean value
  149. * from the Math.random() sequence.</p>
  150. *
  151. * @return the random boolean
  152. */
  153. public boolean nextBoolean() {
  154. return (Math.random() > 0.5);
  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 float
  162. */
  163. public float nextFloat() {
  164. return (float)Math.random();
  165. }
  166. /**
  167. * <p>Synonymous to the Math.random() call.</p>
  168. *
  169. * @return the random double
  170. */
  171. public double nextDouble() {
  172. return Math.random();
  173. }
  174. }