1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import java.io.*; 14 import org.jgap.*; 15 16 22 public class GaussianRandomGenerator 23 implements RandomGenerator { 24 25 private final static String CVS_REVISION = "$Revision: 1.20 $"; 26 27 private static final double DELTA = 0.0000001; 29 30 private Random m_rn; 31 32 35 private double m_standardDeviation; 36 37 public GaussianRandomGenerator() { 38 this(1.0d); 39 } 40 41 46 public GaussianRandomGenerator(final double a_standardDeviation) { 47 super(); 48 init(); 49 setGaussianStdDeviation(a_standardDeviation); 50 } 51 52 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 ( 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 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 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 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 114 private double nextGaussian() { 115 double r = (m_rn.nextGaussian() + 5.8d) / (5.8d * 2.0d); 117 return r; 118 } 119 120 132 private void readObject(ObjectInputStream a_inputStream) 133 throws IOException, ClassNotFoundException { 134 a_inputStream.defaultReadObject(); 136 m_rn.setSeed(System.currentTimeMillis()); 137 } 138 } 139 | Popular Tags |