KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > GaussianRandomGenerator


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 JavaDoc CVS_REVISION = "$Revision: 1.20 $";
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   public GaussianRandomGenerator() {
38     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   public GaussianRandomGenerator(final double a_standardDeviation) {
47     super();
48     init();
49     setGaussianStdDeviation(a_standardDeviation);
50   }
51
52   /**
53    * Initializations on construction.
54    */

55   private void init() {
56     m_rn = new Random();
57   }
58
59   public void setGaussianStdDeviation(final double a_standardDeviation) {
60     if (a_standardDeviation <= DELTA) {
61       throw new IllegalArgumentException JavaDoc(
62           "Standard deviation must be greater 0!");
63     }
64     m_standardDeviation = a_standardDeviation;
65   }
66
67   public double getGaussianStdDeviation() {
68     return m_standardDeviation;
69   }
70
71   /**
72    * @return positive integer value
73    */

74   public int nextInt() {
75     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   public int nextInt(final int a_ceiling) {
85     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   public long nextLong() {
93     long result = Math.min(Long.MAX_VALUE,
94                            (long) (nextGaussian() * Long.MAX_VALUE / (5.8d * 2)));
95     return result;
96   }
97
98   public double nextDouble() {
99     return nextGaussian();
100   }
101
102   public float nextFloat() {
103     return (float) (nextGaussian());
104   }
105
106   public boolean nextBoolean() {
107     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   private double nextGaussian() {
115     //scale to [0..1[
116
double r = (m_rn.nextGaussian() + 5.8d) / (5.8d * 2.0d);
117     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   private void readObject(ObjectInputStream a_inputStream)
133       throws IOException, ClassNotFoundException JavaDoc {
134     //always perform the default de-serialization first
135
a_inputStream.defaultReadObject();
136     m_rn.setSeed(System.currentTimeMillis());
137   }
138 }
139
Popular Tags