FileDocCategorySizeDatePackage
DistributedRandom.javaAPI DocphoneME MR2 API (J2ME)3025Wed May 02 18:00:42 BST 2007gov.nist.microedition.io.j2me.sip

DistributedRandom

public class DistributedRandom extends Random
Generate a random number. This code is in the public domain.

Fields Summary
Constructors Summary
Methods Summary
public intnextCommon(int n, boolean confirmPositive)
Method copied from the jdk src 1.4.2_03.

param
n the seed for the next random number
param
confirmPositive boolean flag that is set to true when the random number to be returned has to be a positive integer
return
the random number
exception
IllegalArgumentException if seed is less than zero

        if (n <= 0)
            throw new IllegalArgumentException("n must be positive");
        if ((n & -n) == n) // i.e. n is a power of 2
            return (int)((n * (long)next(31)) >> 31);
        
        int bits, val;
        boolean condition;
        do {
            bits = next(31);
            val = bits%n;
            condition = (bits - val + (n - 1) < 0);
            if (confirmPositive) {
                condition = (condition&&(val > 0));
            }
        } while (condition);    
        return val;
    
public intnextInt(int n)
Returns a random integer.

param
n the seed for the next random number
return
the random number
exception
IllegalArgumentException if seed is less than zero

        return nextCommon(n, false);
    
public intnextPositiveInt(int n)
Returns a positive random integer.

param
n the seed for the next random number
return
the random number
exception
IllegalArgumentException if seed is less than zero

        return nextCommon(n, true);