java.util

Class Random

Implemented Interfaces:
Serializable
Known Direct Subclasses:
JDKRandomGenerator, JVMRandom, RandomAdaptor, SecureRandom

public class Random
extends Object
implements Serializable

An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code. However, subclasses of class Random are permitted to use other algorithms, so long as they adhere to the general contracts for all the methods.

The algorithms implemented by class Random use a protected utility method that on each invocation can supply up to 32 pseudorandomly generated bits.

Many applications will find the random method in class Math simpler to use.

Since:
JDK1.0
See Also:
Math.random(), Serialized Form

Constructor Summary

Random()
Creates a new random number generator.
Random(long seed)
Creates a new random number generator using a single long seed:
 public Random(long seed) { setSeed(seed); }
Used by method next to hold the state of the pseudorandom number generator.

Method Summary

protected int
next(int bits)
Generates the next pseudorandom number.
boolean
nextBoolean()
Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
void
nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array.
double
nextDouble()
Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
float
nextFloat()
Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
double
nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.
int
nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
int
nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
long
nextLong()
Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
void
setSeed(long seed)
Sets the seed of this random number generator using a single long seed.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Random

public Random()
Creates a new random number generator. This constructor sets the seed of the random number generator to a value very likely to be distinct from any other invocation of this constructor.
Usages and Demos :

View More Examples of Random()
   1: {
   2:     public Random()
   3:     {
   4:         ...
   5:     {
   6:         inStack.push(new Double(Math.random()));
   7:         return;
   8:     }
   9: }

View Full Code Here
   1:   public String getPassWordKey(User user) {
   2:     String key = Digest.getDigest(Integer.toString((new Random()).nextInt()));
   3:     authKeys.put(key, user);
   4:     return key;
   5:   }

View Full Code Here
   1: 
   2:     private int _counter = (new Random()).nextInt();
   3:     private static final String _LOB_PREFIX = "LOB";
   4:     private static final String _LOB_SUFFIX = ".lob";

View Full Code Here
   1:         UnibrowPrefsUtil.handleBSE(null, bse);
   2:         name = Integer.toString((new Random()).nextInt());
   3:       }
   4:       p = getPreferencesNode().node(name);
   5:       p.put(KEY_URL, url);

View Full Code Here
   1:         byte[] fakeQueryKey = new byte[8];
   2:         (new Random()).nextBytes(fakeQueryKey);
   3: 
   4:         QueryRequest crapQuery = 
   5:             queryRequestFactory.createQueryKeyQuery("susheel", 

View Full Code Here

Random

public Random(long seed)
Creates a new random number generator using a single long seed:
 public Random(long seed) { setSeed(seed); }
Used by method next to hold the state of the pseudorandom number generator.
Parameters:
seed - the initial seed.
See Also:
setSeed(long)
Usages and Demos :

View More Examples of Random(long seed)
   1:     {
   2:         Random initializer = new Random(1) ;
   3:         LifeDisplayWindow reducer = new LifeDisplayWindow(SurfaceSize,
   4:                                                           SurfaceSize) ;

View Full Code Here
   1: 
   2:     private final Random random = new Random(0);
   3: 
   4:     public FiniteRandomValueGenerator(FiniteValues values) {
   5:         this.values = values;

View Full Code Here
   1: {
   2:     private Random r = new Random(0);
   3:     
   4:     public SlowInputStream(InputStream in) {
   5:         super(in);

View Full Code Here
   1:     try {
   2:       random = new Random((long)(Math.random()*1E8));;
   3:     }
   4:     catch(Exception e){

View Full Code Here
   1: 
   2:         Random rand = new Random(1);
   3:         byte [] key = new byte [20];
   4:         rand.nextBytes (key);
   5:         params.add (key);

View Full Code Here

Method Details

next

protected int next(int bits)
Generates the next pseudorandom number. Subclass should override this, as this is used by all other methods.

The general contract of next is that it returns an int value and if the argument bits is between 1 and 32 (inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be 0 or 1. The method next is implemented by class Random as follows:

 synchronized protected int next(int bits) {
       seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
       return (int)(seed >>> (48 - bits));
 }
This is a linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.
Parameters:
bits - random bits
Returns:
the next pseudorandom value from this random number generator's sequence.
Since:
JDK1.1

nextBoolean

public boolean nextBoolean()
Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence. The general contract of nextBoolean is that one boolean value is pseudorandomly generated and returned. The values true and false are produced with (approximately) equal probability. The method nextBoolean is implemented by class Random as follows:
 public boolean nextBoolean() {return next(1) != 0;}
 
Returns:
the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
Since:
1.2
Usages and Demos :

View More Examples of nextBoolean()
   1: public class RandomGenerator {
   2:   private static Random r = new Random(47);
   3:   public static class
   4:         ...
   5:     public java.lang.Boolean next() {
   6:       return r.nextBoolean();
   7:     }
   8:   }
   9:   public static class

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import net.i2p.I2PAppContext;
   4:         ...
   5:             _pool[i] = new DummyRandomSource(context);
   6:             _pool[i].nextBoolean();
   7:         }
   8:         ...
   9:     private class DummyRandomSource extends RandomSource {
  10:         private Random _prng;
  11:         public DummyRandomSource(I2PAppContext context) {
  12:         ...
  13:             super(context);
  14:             _prng = new Random();

View Full Code Here
   1: 
   2: import java.util.Random;
   3: 
   4:         ...
   5: public class RandomUtils {
   6:     public static final Random RANDOM = new Random( System.currentTimeMillis() );
   7:     
   8:         ...
   9:     {
  10:         return RANDOM.nextBoolean() ? -1:1;
  11:     }

View Full Code Here
   1: package de.uniwue.ki.util.math;
   2: import java.util.Random;
   3: 
   4:         ...
   5: public final class RandomDistributionHelper implements IRandomWrapper {
   6:     private final Random _random;
   7: 
   8:         ...
   9:     public RandomDistributionHelper(Random random) {
  10:         super();
  11:         ...
  12:     public synchronized boolean nextBoolean() {
  13:         return _random.nextBoolean();

View Full Code Here
}
private static Random r = new Random();
public static class RandBooleanGenerator
...
public boolean next() {
return r.nextBoolean();
}
}
public static class RandByteGenerator

View Full Code Here

nextBytes

public void nextBytes(byte[] bytes)
Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.
Parameters:
bytes - the non-null byte array in which to put the random bytes.
Since:
JDK1.1
Usages and Demos :

View More Examples of nextBytes(byte[] bytes)
   1: import java.util.Random ;
   2: import java.util.Arrays ;
   3:         ...
   4:         long        seed = System.currentTimeMillis() ;
   5:         Random      rand = new Random(seed) ;
   6:         
   7:         
   8:         for (int iter = 0 ; iter < ITERS ; iter++)

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import java.util.Arrays;
   4:         ...
   5: {
   6:     private Random _r = null;
   7:     
   8:         ...
   9:     protected void setUp() {
  10:         _r = new Random();
  11:     }
  12:         ...
  13:         byte[] _orig1024 = new byte[1024];
  14:         _r.nextBytes(_orig1024);

View Full Code Here
   1:         try {
   2:             Random _r = new Random();
   3:             
   4:         ...
   5:             byte[] _orig1024 = new byte[1024];
   6:             _r.nextBytes(_orig1024);
   7:             
   8:         ...
   9:             byte[] _orig2048 = new byte[2048];
  10:             _r.nextBytes(_orig2048);
  11:             
  12:         ...
  13:             byte[] _orig4096 = new byte[4096];
  14:             _r.nextBytes(_orig4096);

View Full Code Here
   1: import java.util.Arrays;
   2: import java.util.Random;
   3: 
   4:         ...
   5: 
   6:     private static final Random GENERATOR = new Random();
   7: 
   8:         ...
   9:     static {
  10:         GENERATOR.nextBytes(RANDOM_PAD);
  11:     }
  12:         ...
  13:         byte[] messageId = new byte[LENGTH];
  14:         GENERATOR.nextBytes(messageId);

View Full Code Here
   1: import java.security.interfaces.RSAKey;
   2: import java.util.Random;
   3: 
   4:         ...
   5:     final byte[] PS = new byte[k - M.length - 3];
   6:     prng.nextBytes(PS);
   7:     int i = 0;
   8:         ...
   9: 
  10:   public byte[] encode(final byte[] M, final Random rnd)
  11:   {
  12:         ...
  13:     final byte[] PS = new byte[k - M.length - 3];
  14:     rnd.nextBytes(PS);

View Full Code Here

nextDouble

public double nextDouble()
Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

The general contract of nextDouble is that one double value, chosen (approximately) uniformly from the range 0.0d (inclusive) to 1.0d (exclusive), is pseudorandomly generated and returned. All 253 possible float values of the form m x 2-53 , where m is a positive integer less than 253, are produced with (approximately) equal probability. The method nextDouble is implemented by class Random as follows:

 public double nextDouble() {
       return (((long)next(26) << 27) + next(27))
           / (double)(1L << 53);
 }

The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source or randomly chosen bits, then the algorithm shown would choose double values from the stated range with perfect uniformity.

[In early versions of Java, the result was incorrectly calculated as:

  return (((long)next(27) << 27) + next(27))
      / (double)(1L << 54);
This might seem to be equivalent, if not better, but in fact it introduced a large nonuniformity because of the bias in the rounding of floating-point numbers: it was three times as likely that the low-order bit of the significand would be 0 than that it would be 1! This nonuniformity probably doesn't matter much in practice, but we strive for perfection.]
Returns:
the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.
Usages and Demos :

View More Examples of nextDouble()
   1: public class RandomDoubles {
   2:   private static Random rand = new Random(47);
   3:         ...
   4:   public double next() { return rand.nextDouble(); }
   5:   public static void main(String[] args) {
   6:     RandomDoubles rd = new RandomDoubles();
   7:     for(int i = 0; i < 7; i ++)

View Full Code Here
   1: package org.apache.commons.math.random;
   2:         ...
   3: import java.util.Random;
   4: 
   5:         ...
   6:     private static final long serialVersionUID = -9161426374178114548L;
   7:     private Random random = new Random();
   8:     
   9:         ...
  10:     public double nextDouble() {
  11:         return random.nextDouble();

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import net.i2p.I2PAppContext;
   4:         ...
   5:     private class DummyRandomSource extends RandomSource {
   6:         private Random _prng;
   7:         public DummyRandomSource(I2PAppContext context) {
   8:         ...
   9:             super(context);
  10:             _prng = new Random();
  11:         }
  12:         ...
  13:         public void nextBytes(byte buf[]) { _prng.nextBytes(buf); }
  14:         public double nextDouble() { return _prng.nextDouble(); }

View Full Code Here
   1: 
   2: import java.util.Random;
   3: 
   4:         ...
   5: 
   6:     private final Random random = new Random(0);
   7: 
   8:         ...
   9:     private boolean getRandom(double probability) {
  10:         return random.nextDouble() < probability;
  11:     }

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import javax.vecmath.Point3d;
   4:         ...
   5:     public void testFit() {
   6:         final Random  rd = new Random(457821698762354L);
   7:         final Plane plan = new Plane();
   8:         ...
   9:         final Point3d P1 = new Point3d(100*rd.nextDouble()+25, 100*rd.nextDouble()+25, Math.rint(100*rd.nextDouble()+40));
  10:         ...
  11:         final Point3d P2 = new Point3d(100*rd.nextDouble()+25, 100*rd.nextDouble()+25, Math.rint(100*rd.nextDouble()+40));

View Full Code Here

nextFloat

public float nextFloat()
Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

The general contract of nextFloat is that one float value, chosen (approximately) uniformly from the range 0.0f (inclusive) to 1.0f (exclusive), is pseudorandomly generated and returned. All 224 possible float values of the form m x 2-24, where m is a positive integer less than 224 , are produced with (approximately) equal probability. The method nextFloat is implemented by class Random as follows:

 public float nextFloat() {
      return next(24) / ((float)(1 << 24));
 }
The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source or randomly chosen bits, then the algorithm shown would choose float values from the stated range with perfect uniformity.

[In early versions of Java, the result was incorrectly calculated as:

 return next(30) / ((float)(1 << 30));
This might seem to be equivalent, if not better, but in fact it introduced a slight nonuniformity because of the bias in the rounding of floating-point numbers: it was slightly more likely that the low-order bit of the significand would be 0 than that it would be 1.]
Returns:
the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.
Usages and Demos :

View More Examples of nextFloat()
   1: import org.ariane.net.NetHost;
   2: import java.util.Random;
   3: import org.omg.PortableServer.POA;
   4:         ...
   5: 
   6: private Random generator;
   7: static private POA poa = null;
   8:         ...
   9:       super();
  10:       generator = new Random();
  11:       this.poa = poa;
  12:         ...
  13:       nbr++;
  14:       return generator.nextFloat();

View Full Code Here
   1: import java.util.Iterator;
   2: import java.util.Random;
   3: 
   4:         ...
   5:         final PLayer layer = getLayer();
   6:         Random r = new Random();
   7:         for (int i = 0; i < 1000; i++) {
   8:         ...
   9:             final PNode n = PPath.createRectangle(0, 0, 100, 80);    
  10:             n.translate(10000 * r.nextFloat(), 10000 * r.nextFloat());
  11:         ...
  12:             n.setPaint(new Color(r.nextFloat(), r.nextFloat(),r.nextFloat()));

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import net.i2p.I2PAppContext;
   4:         ...
   5:     private class DummyRandomSource extends RandomSource {
   6:         private Random _prng;
   7:         public DummyRandomSource(I2PAppContext context) {
   8:         ...
   9:             super(context);
  10:             _prng = new Random();
  11:         }
  12:         ...
  13:         public double nextDouble() { return _prng.nextDouble(); }
  14:         public float nextFloat() { return _prng.nextFloat(); }

View Full Code Here
   1: 
   2: import java.util.Random;
   3: 
   4:         ...
   5: public class RandomUtils {
   6:     public static final Random RANDOM = new Random( System.currentTimeMillis() );
   7:     
   8:         ...
   9:     {
  10:         return RANDOM.nextFloat();
  11:     }

View Full Code Here
   1:     
   2:     Random rnd;
   3:     Vector<float[]> points;
   4:         ...
   5:     public void init() {
   6:         rnd = new Random();
   7:         points = new Vector<float[]>();
   8:         ...
   9:         sum = new float[20];
  10:         camx = rnd.nextFloat() *  80f - 10f;
  11:         ...
  12:         camy = rnd.nextFloat() *  40f + 20f;

View Full Code Here

nextGaussian

public double nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.

The general contract of nextGaussian is that one double value, chosen from (approximately) the usual normal distribution with mean 0.0 and standard deviation 1.0, is pseudorandomly generated and returned. The method nextGaussian is implemented by class Random as follows:

 synchronized public double nextGaussian() {
    if (haveNextNextGaussian) {
            haveNextNextGaussian = false;
            return nextNextGaussian;
    } else {
            double v1, v2, s;
            do { 
                    v1 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
                    v2 = 2 * nextDouble() - 1;   // between -1.0 and 1.0
                    s = v1 * v1 + v2 * v2;
            } while (s >= 1 || s == 0);
            double multiplier = Math.sqrt(-2 * Math.log(s)/s);
            nextNextGaussian = v2 * multiplier;
            haveNextNextGaussian = true;
            return v1 * multiplier;
    }
 }
This uses the polar method of G. E. P. Box, M. E. Muller, and G. Marsaglia, as described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.4.1, subsection C, algorithm P. Note that it generates two independent values at the cost of only one call to Math.log and one call to Math.sqrt.
Returns:
the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and standard deviation 1.0 from this random number generator's sequence.
Usages and Demos :

View More Examples of nextGaussian()
   1: 
   2: import java.util.Random;
   3: 
   4:         ...
   5: 
   6:         Random r = new Random();
   7:         for (int i = 0; i < 10000; i++) {
   8:         ...
   9:             lr.report(Math.round((r.nextGaussian() * 20 + 80)));
  10:             Thread.sleep(15);

View Full Code Here
   1:   public double price(String s) throws NotFound {
   2:     Random rnd = new Random();
   3:     for (String t: prices.keySet())
   4:         ...
   5:       if (t.equals(s))
   6:         return prices.get(t) + 1.5*(rnd.nextGaussian());
   7:       throw new NotFound(s);
   8:   }
   9:   public String[] shares() {

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import java.util.concurrent.TimeUnit;
   4:         ...
   5:                 "my.app:name=PacketObserver");
   6:         Random r = new Random();
   7:         for (int i = 0; i < 10000; i++) {
   8:         ...
   9:             lr.report(Math.round((r.nextGaussian() * 256 + 2048)));
  10:         ...
  11:             Thread.sleep((long) Math.abs(r.nextGaussian() * 15));

View Full Code Here
   1: import hep.aida.*;
   2: import java.util.Random;
   3: 
   4:         ...
   5:     
   6:     Random r = new Random();
   7:     int events = 100;
   8:         ...
   9:       for ( int j = 0; j<tracks; j++ ) {
  10:         momentum.fill(0,r.nextGaussian());
  11:         ...
  12:         momentum.fill(1,r.nextGaussian());

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import net.i2p.I2PAppContext;
   4:         ...
   5:     private class DummyRandomSource extends RandomSource {
   6:         private Random _prng;
   7:         public DummyRandomSource(I2PAppContext context) {
   8:         ...
   9:             super(context);
  10:             _prng = new Random();
  11:         }
  12:         ...
  13:         public float nextFloat() { return _prng.nextFloat(); }
  14:         public double nextGaussian() { return _prng.nextGaussian(); }

View Full Code Here

nextInt

public int nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 232 possible int values are produced with (approximately) equal probability. The method nextInt is implemented by class Random as follows:
 public int nextInt() {  return next(32); }
Returns:
the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
Usages and Demos :

View More Examples of nextInt()
   1:   public static void main(String[] args) {
   2:     Random rand = new Random();
   3:     int i, j, k;
   4:         ...
   5:     j = rand.nextInt() % 100;
   6:         ...
   7:     k = rand.nextInt() % 100;
   8:     pInt("j",j);  pInt("k",k);

View Full Code Here
   1: import java.io.*;
   2: import java.util.Random;
   3: import java.util.Vector;
   4:         ...
   5: 
   6:     private static Random rand = new Random();
   7:     private static Vector words;
   8:         ...
   9:     private int randInt() {
  10:     return rand.nextInt() & 0x7fffffff;
  11:     }

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import net.i2p.I2PAppContext;
   4:         ...
   5:     private class DummyRandomSource extends RandomSource {
   6:         private Random _prng;
   7:         public DummyRandomSource(I2PAppContext context) {
   8:         ...
   9:             super(context);
  10:             _prng = new Random();
  11:         }
  12:         ...
  13:         public double nextGaussian() { return _prng.nextGaussian(); }
  14:         public int nextInt() { return _prng.nextInt(); }

View Full Code Here
   1:     
   2:     protected Random aleatorio = null;
   3:     
   4:         ...
   5:     public Aleatorio() {
   6:         aleatorio = new Random(new Date().getTime());
   7:     }
   8:         ...
   9:     public int getEntero() {
  10:         return aleatorio.nextInt();
  11:     }

View Full Code Here
   1: 
   2: import java.util.Random;
   3: 
   4:         ...
   5: public class RandomUtils {
   6:     public static final Random RANDOM = new Random( System.currentTimeMillis() );
   7:     
   8:         ...
   9:     {
  10:         return (byte)RANDOM.nextInt();
  11:     }
  12:         ...
  13:     {
  14:         return RANDOM.nextInt();

View Full Code Here

nextInt

public int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability. The method nextInt(int n) is implemented by class Random as follows:
 public int nextInt(int n) {
     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;
     do {
         bits = next(31);
         val = bits % n;
     } while(bits - val + (n-1) <320);
     return val;
 }
 

The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm shown would choose int values from the stated range with perfect uniformity.

The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2.

The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number of low-order bits would be returned. Linear congruential pseudo-random number generators such as the one implemented by this class are known to have short periods in the sequence of values of their low-order bits. Thus, this special case greatly increases the length of the sequence of values returned by successive calls to this method if n is a small power of two.

Parameters:
n - the bound on the random number to be returned. Must be positive.
Returns:
a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
Throws:
IllegalArgumentException - n is not positive.
Since:
1.2
Usages and Demos :

View More Examples of nextInt(int n)
   1: 
   2: import java.util.Random;
   3: import javax.ejb.Stateless;
   4:         ...
   5:     public int play() {
   6:     Random random = new Random();
   7:         ...
   8:     return random.nextInt(10);
   9:     }

View Full Code Here
   1:     new ThreadLocal<Integer>() {
   2:       private Random rand = new Random(47);
   3:       protected synchronized Integer initialValue() {
   4:         ...
   5:         return rand.nextInt(10000);
   6:       }
   7:     };
   8:   public static void increment() {

View Full Code Here
   1: import org.kde.qt.*;
   2: import java.util.Random;
   3: 
   4:         ...
   5: private    QColor[]    colors = new QColor[numColors];
   6: private    Random generator = new Random(System.currentTimeMillis());
   7: 
   8:         ...
   9:     for (int a=0; a<numColors; a++) {
  10:     colors[a] = new QColor( generator.nextInt(255),
  11:         ...
  12:                 generator.nextInt(255),

View Full Code Here
   1: import java.util.HashSet;
   2: import java.util.Random;
   3: import java.util.StringTokenizer;
   4:         ...
   5:     private static Generator instance;
   6:     private Random random = new Random();
   7:     private HashSet identifiers = new HashSet();
   8:         ...
   9:     public boolean getBoolean(int percent) {
  10:         return random.nextInt(100) <= percent;
  11:     }
  12:         ...
  13:     public int getInt(int max) {
  14:         return max==0 ? 0 : random.nextInt(max);

View Full Code Here
   1: 
   2: import java.util.Random;
   3: 
   4:         ...
   5: public class TestDocSet extends TestCase {
   6:   Random rand = new Random();
   7: 
   8:         ...
   9:     for (int i=0; i<bitsToSet; i++) {
  10:       bs.fastSet(rand.nextInt(sz));
  11:     }
  12:         ...
  13:   public DocSet getDocSet(OpenBitSet bs) {
  14:     return rand.nextInt(2)==0 ? getHashDocSet(bs) : getBitDocSet(bs);

View Full Code Here

nextLong

public long nextLong()
Returns the next pseudorandom, uniformly distributed long value from this random number generator's sequence. The general contract of nextLong is that one long value is pseudorandomly generated and returned. All 264 possible long values are produced with (approximately) equal probability. The method nextLong is implemented by class Random as follows:
 public long nextLong() {
       return ((long)next(32) << 32) + next(32);
 }
Returns:
the next pseudorandom, uniformly distributed long value from this random number generator's sequence.
Usages and Demos :

View More Examples of nextLong()
   1:   public static void main(String[] args) {
   2:     Random rand = new Random(47);
   3:     int i = rand.nextInt();
   4:         ...
   5: 
   6:     long l = rand.nextLong();
   7:         ...
   8:     long m = rand.nextLong();
   9:     printBinaryLong("-1L", -1L);

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import net.i2p.I2PAppContext;
   4:         ...
   5:     private class DummyRandomSource extends RandomSource {
   6:         private Random _prng;
   7:         public DummyRandomSource(I2PAppContext context) {
   8:         ...
   9:             super(context);
  10:             _prng = new Random();
  11:         }
  12:         ...
  13:         public long nextLong(long n) {
  14:             long v = _prng.nextLong();

View Full Code Here
   1: import java.io.Serializable;
   2: import java.util.Random;
   3: 
   4:         ...
   5:    protected Object                 mHandback;
   6:    private Random                   mRandom = new Random();
   7:    
   8:         ...
   9:          try {
  10:             lName = new ObjectName( "JMX:type=listener,id=" + mRandom.nextLong() );
  11:             ObjectInstance lInstance = pConnector.createMBean(

View Full Code Here
   1: import java.rmi.RemoteException;
   2: import java.util.Random;
   3: 
   4:         ...
   5:    protected Object mHandback;
   6:    private Random mRandom = new Random();
   7: 
   8:         ...
   9:          {
  10:             lName = new ObjectName("JMX:type=listener,id=" + mRandom.nextLong());
  11:             ObjectInstance lInstance = pConnector.createMBean(

View Full Code Here
   1:     }
   2:     final Random r = (seed == -1) ? new Random() : new Random(seed);
   3: 
   4:         ...
   5:       ia[i] = r.nextInt();
   6:       la[i] = r.nextLong();
   7:       sa[i] = (short) r.nextInt();
   8:         ...
   9:     int ikey = r.nextInt();
  10:     long lkey = r.nextLong();
  11:     short skey = (short) r.nextInt();

View Full Code Here

setSeed

public void setSeed(long seed)
Sets the seed of this random number generator using a single long seed. The general contract of setSeed is that it alters the state of this random number generator object so as to be in exactly the same state as if it had just been created with the argument seed as a seed. The method setSeed is implemented by class Random as follows:
 synchronized public void setSeed(long seed) {
       this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
       haveNextNextGaussian = false;
 }
The implementation of setSeed by class Random happens to use only 48 bits of the given seed. In general, however, an overriding method may use all 64 bits of the long argument as a seed value. Note: Although the seed value is an AtomicLong, this method must still be synchronized to ensure correct semantics of haveNextNextGaussian.
Parameters:
seed - the initial seed.
Usages and Demos :

View More Examples of setSeed(long seed)
   1: package org.apache.commons.math.random;
   2: import junit.framework.Test;
   3:         ...
   4: import junit.framework.TestSuite;
   5: import java.util.Random;
   6: 
   7:         ...
   8:         ConstantGenerator generator = new ConstantGenerator();
   9:         Random random = RandomAdaptor.createAdaptor(generator);
  10:         checkConstant(random);
  11:         ...
  12:         assertEquals(0, random.nextLong());
  13:         random.setSeed(100);

View Full Code Here
   1: 
   2: import java.util.Random;
   3: import java.util.Arrays;
   4:         ...
   5:         long currentTime = System.currentTimeMillis() * 10 + 0x01B21DD213814000L;
   6:         Random randomGen = new Random();
   7:         ...
   8:         randomGen.setSeed(((currentTime >> 32) ^ currentTime) & 0xffffffffL);
   9:         ourUUIDStateSeqNum = randomGen.nextLong() & 0x0FFFFL;

View Full Code Here
   1: import java.awt.*;
   2: import java.util.Random;
   3: public class PercolationApp extends AbstractCalculation
   4:         ...
   5:     int NONE = -99, selectedCluster = NONE;
   6:     Random random;
   7: 
   8:         ...
   9:     {
  10:         random.setSeed(1239012312);
  11:         L = control.getInt("L");

View Full Code Here
   1: import java.util.ConcurrentModificationException;
   2: import java.util.Random;
   3: 
   4:         ...
   5: 
   6:     private final Random random = new Random(0);
   7: 
   8:         ...
   9:         super.paintComponent(g);
  10:         random.setSeed(0);
  11:         try {

View Full Code Here
   1: 
   2: import java.util.Random;
   3: 
   4:         ...
   5:   static final int POW = 13;
   6:   static final int RANDOM = 14;
   7:   static final int ROUND = 15;
   8:         ...
   9:     put(math, "tan", TAN, 1);
  10:     put(math, "random", RANDOM, 0);
  11:     put(math, "setSeed", SET_SEED, 1);
  12:         ...
  13:       if (length != 0)
  14:     random.setSeed((long) eval.getArg(0).toNum());

View Full Code Here