1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import java.io.*; 14 15 import org.jgap.*; 16 17 23 public class CauchyRandomGenerator 24 implements RandomGenerator { 25 26 private final static String CVS_REVISION = "$Revision: 1.14 $"; 27 28 private double m_scale; 29 30 private double m_location; 31 32 private Random m_rn; 33 34 40 public CauchyRandomGenerator() { 41 this(0.0d, 1.0d); 42 } 43 44 51 public CauchyRandomGenerator(final double a_location, final double a_scale) { 52 m_location = a_location; 53 m_scale = a_scale; 54 m_rn = new Random(); 55 } 56 57 public int nextInt() { 58 return Math.min(Integer.MAX_VALUE - 1, 59 (int) Math.round(nextCauchy() * Integer.MAX_VALUE)); 60 } 61 62 public int nextInt(final int a_ceiling) { 63 return Math.min(a_ceiling - 1, 64 (int) Math.round(nextCauchy() * a_ceiling)); 65 } 66 67 public long nextLong() { 68 return Math.min(Long.MAX_VALUE - 1, 69 Math.round(nextCauchy() * Long.MAX_VALUE)); 70 } 71 72 public double nextDouble() { 73 return nextCauchy(); 74 } 75 76 public float nextFloat() { 77 return Math.min(Float.MAX_VALUE - 1, 78 (float) (nextCauchy() * Float.MAX_VALUE)); 79 } 80 81 public boolean nextBoolean() { 82 return nextCauchy() >= 0.5d; 83 } 84 85 93 public double nextCauchy() { 94 return 0.5 95 + Math.atan( (m_rn.nextDouble() - m_location) / m_scale) / Math.PI; 96 } 97 98 104 public double getCauchyStandardDeviation() { 105 return m_scale; 106 } 107 108 120 private void readObject(ObjectInputStream a_inputStream) 121 throws IOException, ClassNotFoundException { 122 a_inputStream.defaultReadObject(); 124 m_rn.setSeed(System.currentTimeMillis()); 125 } 126 } 127 | Popular Tags |