Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 138   Methods: 13
NCLOC: 64   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
GaussianRandomGenerator.java 100% 100% 100% 100%
coverage
 1    /*
 2    * This file is part of JGAP.
 3    *
 4    * JGAP offers a dual license model containing the LGPL as well as the MPL.
 5    *
 6    * For licencing information please see the file license.txt included with JGAP
 7    * or have a look at the top of class org.jgap.Chromosome which representatively
 8    * includes the JGAP license policy applicable for any file delivered with JGAP.
 9    */
 10    package org.jgap.impl;
 11   
 12    import java.util.*;
 13    import java.io.*;
 14    import org.jgap.*;
 15   
 16    /**
 17    * Gaussian deviation serving as basis for randomly finding a number.
 18    *
 19    * @author Klaus Meffert
 20    * @since 1.1
 21    */
 22    public class GaussianRandomGenerator
 23    implements RandomGenerator {
 24    /** String containing the CVS revision. Read out via reflection!*/
 25    private final static String CVS_REVISION = "$Revision: 1.19 $";
 26   
 27    //delta for distinguishing whether a value is to be interpreted as zero
 28    private static final double DELTA = 0.0000001;
 29   
 30    private Random m_rn;
 31   
 32    /**
 33    * Standard deviation of the gaussian deviation
 34    */
 35    private double m_standardDeviation;
 36   
 37  4 public GaussianRandomGenerator() {
 38  4 this(1.0d);
 39    }
 40   
 41    /**
 42    * Constructor speicifying the (obliagtory) standard deviation.
 43    *
 44    * @param a_standardDeviation the standard deviation to use
 45    */
 46  14 public GaussianRandomGenerator(final double a_standardDeviation) {
 47  14 super();
 48  14 init();
 49  14 setGaussianStdDeviation(a_standardDeviation);
 50    }
 51   
 52    /**
 53    * Initializations on construction.
 54    */
 55  14 private void init() {
 56  14 m_rn = new Random();
 57    }
 58   
 59  14 public void setGaussianStdDeviation(final double a_standardDeviation) {
 60  14 if (a_standardDeviation <= DELTA) {
 61  1 throw new IllegalArgumentException(
 62    "Standard deviation must be greater 0!");
 63    }
 64  13 m_standardDeviation = a_standardDeviation;
 65    }
 66   
 67  1 public double getGaussianStdDeviation() {
 68  1 return m_standardDeviation;
 69    }
 70   
 71    /**
 72    * @return positive integer value
 73    */
 74  103 public int nextInt() {
 75  103 return Math.abs(Math.min(Integer.MAX_VALUE - 1,
 76    (int) Math.round(nextGaussian()
 77    * Integer.MAX_VALUE)));
 78    }
 79   
 80    /**
 81    * @param a_ceiling the upper boundary excluded
 82    * @return positive integer value between 0 and (ceiling - 1)
 83    */
 84  200 public int nextInt(final int a_ceiling) {
 85  200 return Math.min(a_ceiling - 1,
 86    (int) Math.round(nextGaussian() * a_ceiling / (5.8d * 2)));
 87    }
 88   
 89    /**
 90    * @return positive long value
 91    */
 92  101 public long nextLong() {
 93  101 long result = Math.min(Long.MAX_VALUE,
 94    (long) (nextGaussian() * Long.MAX_VALUE / (5.8d * 2)));
 95  101 return result;
 96    }
 97   
 98  101 public double nextDouble() {
 99  101 return nextGaussian();
 100    }
 101   
 102  101 public float nextFloat() {
 103  101 return (float) (nextGaussian());
 104    }
 105   
 106  1 public boolean nextBoolean() {
 107  1 return nextGaussian() >= 0.5d;
 108    }
 109   
 110    /**
 111    * @return the next randomly distributed gaussian with current standard
 112    * deviation, will be greater/equal zero
 113    */
 114  607 private double nextGaussian() {
 115    //scale to [0..1[
 116  607 double r = (m_rn.nextGaussian() + 5.8d) / (5.8d * 2.0d);
 117  607 return r;
 118    }
 119   
 120    /**
 121    * When deserializing, initialize the seed because otherwise we could get
 122    * duplicate evolution results when doing distributed computing!
 123    *
 124    * @param a_inputStream the ObjectInputStream provided for deserialzation
 125    *
 126    * @throws IOException
 127    * @throws ClassNotFoundException
 128    *
 129    * @author Klaus Meffert
 130    * @since 3.01
 131    */
 132  2 private void readObject(ObjectInputStream a_inputStream)
 133    throws IOException, ClassNotFoundException {
 134    //always perform the default de-serialization first
 135  2 a_inputStream.defaultReadObject();
 136  2 m_rn.setSeed(System.currentTimeMillis());
 137    }
 138    }