nl.tudelft.simulation.jstats.streams
Class RandomNumberGenerator

java.lang.Object
  extended by nl.tudelft.simulation.jstats.streams.RandomNumberGenerator
All Implemented Interfaces:
Serializable, StreamInterface
Direct Known Subclasses:
DX120Generator, MersenneTwister

public abstract class RandomNumberGenerator
extends Object
implements StreamInterface

The RandomNumberGenerator class provides an abstract for all pseudo random number generators.

(c) copyright 2004 Delft University of Technology , the Netherlands.
See for project information www.simulation.tudelft.nl
License of use: Lesser General Public License (LGPL) , no warranty.

Since:
1.0
Version:
$Revision: 1.1 $ $Date: 2007/01/06 13:25:41 $
Author:
Peter Jacobs
See Also:
Serialized Form

Field Summary
protected  long seed
          the seed of the generator
 
Constructor Summary
RandomNumberGenerator()
          constructs a new RandomNumberGenerator.
RandomNumberGenerator(long seed)
          constructs a new RandomNumberGenerator.
 
Method Summary
 long getSeed()
          returns the seed of the generator
protected abstract  long next(int bits)
          returns the next value in the stream.
 boolean nextBoolean()
          Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
 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.
 int nextInt()
          Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence.
 int nextInt(int i, int j)
          Returns a pseudorandom, uniformly distributed int value between i (inclusive) and j, 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 reset()
          resets the stream
abstract  void setSeed(long seed)
          sets the seed of the generator
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

seed

protected long seed
the seed of the generator

Constructor Detail

RandomNumberGenerator

public RandomNumberGenerator()
constructs a new RandomNumberGenerator. The seed value used in the rng is set to System.currentTimeMillis();


RandomNumberGenerator

public RandomNumberGenerator(long seed)
constructs a new RandomNumberGenerator.

Parameters:
seed - the seed of the generator.
Method Detail

reset

public void reset()
Description copied from interface: StreamInterface
resets the stream

Specified by:
reset in interface StreamInterface
See Also:
StreamInterface.reset()

next

protected abstract long next(int bits)
returns the next value in the stream.

Parameters:
bits - the number of bits used
Returns:
the next value.

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;
 }
 

Specified by:
nextBoolean in interface StreamInterface
Returns:
the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.
Since:
1.2

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 2 64 possible float values of the form m x  2 -64 , where m is a positive integer less than 2 64 , are produced with (approximately) equal probability.

Specified by:
nextDouble in interface StreamInterface
Returns:
the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

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 2 24 possible float values of the form m x  2 -24 , where m is a positive integer less than 2 24 , 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.

Specified by:
nextFloat in interface StreamInterface
Returns:
the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

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 2 32 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);
 }
 

Specified by:
nextInt in interface StreamInterface
Returns:
the next pseudorandom, uniformly distributed int value from this random number generator's sequence.

nextInt

public int nextInt(int i,
                   int j)
Returns a pseudorandom, uniformly distributed int value between i (inclusive) and j, 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.

Specified by:
nextInt in interface StreamInterface
Parameters:
i - the lower value
j - the higher value
Returns:
the result

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 2 64 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);
        }
      
     
    
   
  
 

Specified by:
nextLong in interface StreamInterface
Returns:
the next pseudorandom, uniformly distributed long value from this random number generator's sequence.

setSeed

public abstract void setSeed(long seed)
Description copied from interface: StreamInterface
sets the seed of the generator

Specified by:
setSeed in interface StreamInterface
Parameters:
seed - the new seed
See Also:
StreamInterface.setSeed(long)

getSeed

public long getSeed()
Description copied from interface: StreamInterface
returns the seed of the generator

Specified by:
getSeed in interface StreamInterface
Returns:
long the seed
See Also:
StreamInterface.getSeed()

toString

public String toString()
Overrides:
toString in class Object
See Also:
Object.toString()


Copyright © 2002-2011 Delft University of Technology, the Netherlands. All Rights Reserved.