Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

SimpleRandomWalk.java

00001 /* 00002 * Created on Feb 7, 2004 00003 * 00004 * A source of numbers in a bounded random walk. 00005 */ 00006 package org.nees.rbnb; 00007 00008 import java.util.Random; 00009 00020 public class SimpleRandomWalk { 00021 00022 private static final double MIN_VALUE = -10.0; 00023 private static final double MAX_VALUE = 10.0; 00024 private static final boolean WRAP = true; 00025 00026 private double minValue; 00027 private double maxValue; 00028 private double current; 00029 private boolean wrap; 00030 00031 private Random source; 00032 00033 public SimpleRandomWalk() 00034 { 00035 init(MIN_VALUE,MAX_VALUE,WRAP); 00036 } 00037 00038 public SimpleRandomWalk(double min, double max, boolean wrap) 00039 { 00040 init(min,max,wrap); 00041 } 00042 00043 private void init(double min, double max, boolean flag) { 00044 minValue = min; 00045 maxValue = max; 00046 current = (min + max)/2.0; 00047 wrap = flag; 00048 source = new Random(); 00049 } 00050 00051 public double next() 00052 { 00053 double step = (2.0 * source.nextDouble()) - 1.0; 00054 if ((current+step) > maxValue) 00055 { 00056 if (wrap) current = minValue + ((current+step)-maxValue); 00057 else // reflect 00058 current = maxValue + (maxValue-(current+step)); 00059 } 00060 else if ((current+step) < minValue) 00061 { 00062 if (wrap) current = maxValue + ((current+step)-minValue); 00063 else // reflect 00064 current = minValue + (minValue - (current+step)); 00065 } 00066 else current += step; 00067 00068 return current; 00069 } 00070 00071 public static void main (String[] args) 00072 { 00073 SimpleRandomWalk w = new SimpleRandomWalk(); 00074 00075 for (int i = 0; i < 10000; i++) 00076 { 00077 System.out.println(i + ": " + w.next()); 00078 } 00079 } 00080 }

Generated on Tue Aug 24 11:12:26 2004 for Data turbine for NEESGrid by doxygen 1.3.7