commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/math JVMRandom.java
Date Mon, 12 May 2003 04:41:41 GMT
bayard      2003/05/11 21:41:40

  Modified:    lang/src/java/org/apache/commons/lang/math JVMRandom.java
  Log:
  Block nextInt(int) and nextLong(long) from being <= 0.
  
  Revision  Changes    Path
  1.4       +31 -3     jakarta-commons/lang/src/java/org/apache/commons/lang/math/JVMRandom.java
  
  Index: JVMRandom.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/math/JVMRandom.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JVMRandom.java	12 May 2003 04:29:26 -0000	1.3
  +++ JVMRandom.java	12 May 2003 04:41:40 -0000	1.4
  @@ -98,11 +98,17 @@
        * Math.random() sequence. 
        *
        * @param n  the specified exclusive max-value
  +     * @throws IllegalArgumentException when n <= 0
        *
        * @return the random int
        */
       public int nextInt(int n) {
  -        // check this cannot return 'n'
  +        if (n <= 0) {
  +            throw new IllegalArgumentException(
  +                "Upper bound for nextInt must be positive"
  +            );
  +        }
  +        // TODO: check this cannot return 'n'
           return (int)(Math.random() * n);
       }
       /**
  @@ -113,8 +119,30 @@
        */
       public long nextLong() {
           // possible loss of precision?
  -        return (long)(Math.random() * Long.MAX_VALUE);
  +        return nextLong(Long.MAX_VALUE);
       }
  +
  +
  +    /**
  +     * Returns a pseudorandom, uniformly distributed long value between 0 
  +     * (inclusive) and the specified value (exclusive), from the 
  +     * Math.random() sequence.
  +     *
  +     * @param n  the specified exclusive max-value
  +     * @throws IllegalArgumentException when n <= 0
  +     *
  +     * @return the random long
  +     */
  +    public static long nextLong(long n) {
  +        if (n <= 0) {
  +            throw new IllegalArgumentException(
  +                "Upper bound for nextInt must be positive"
  +            );
  +        }
  +        // TODO: check this cannot return 'n'
  +        return (long)(Math.random() * n);
  +     }
  +
       /**
        * Returns the next pseudorandom, uniformly distributed boolean value 
        * from the Math.random() sequence.
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message