home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-20 | 2.7 KB | 100 lines |
-
- package simula.random;
-
- import java.util.Random;
-
- /**
- * The class Linear generates pseudo-random real values based on two
- * arrays of n numbers each: a[i] contains the values of a known
- * cumulative function F in b[i]. The values generated are either in a[]
- * or are generated with linear interpolation.
- */
- public class Linear extends Generator {
-
- /**
- * This array contains the points where the cumulative function is defined.
- */
- private double[] x;
-
- /**
- * This array contains the known values of the cumulative function.
- */
- private double[] cum;
-
-
- /**
- * Creates a new Linear generator using the standard engine and the current time as seed.
- * @param a Array of the known values of the cumulative function.
- * @param b Array of the points of the cumulative function which we know the values of.
- */
- public Linear(double[] a,double[] b)
- {
- random = new RandomEngineJava();
- setParam(a,b);
- }
- /**
- * Creates a new Linear generator using the standard engine with the given seed.
- * @param seed Initial seed for the pseudo-random engine.
- * @param a Array of the known values of the cumulative function.
- * @param b Array of the points of the cumulative function which we know the values of.
- */
- public Linear(long seed,double[] a,double[] b)
- {
- random = new RandomEngineJava(seed);
- setParam(a,b);
- }
- /**
- * Creates a new Linear generator using the given engine.
- * @param e Pseudo-random bit generator.
- * @param a Array of the known values of the cumulative function.
- * @param b Array of the points of the cumulative function which we know the values of.
- */
- public Linear(RandomEngine e,double[] a,double[] b)
- {
- random = e;
- setParam(a,b);
- }
- /**
- * Draws a value from the generator.
- * @return An instance of Value that encapsulates a real value.
- * @see RealValue
- * @see Value
- */
- public Value draw()
- {
- int p = 0;
-
- double r = random.nextDouble();
-
- while (cum[p] < r) p++;
-
- // (x[p],cum[p])--(x[p+1],cum[p+1])
-
- // calcolo l'ordinata del punto r.
- double res = x[p]+(r - cum[p])/(cum[p+1]-cum[p])*(x[p+1]-x[p]);
- return new RealValue( res );
- }
- /**
- * Change the data of the cumulative function.
- * @param a Array of the known values of the cumulative function.
- * @param b Array of the points of the cumulative function which we know the values of.
- */
- public void setParam(double[] a,double[] b)
- {
- x = new double[b.length];
- cum = new double[a.length];
-
- int i;
- for(i=0;i<a.length;i++)
- cum[i] = a[i];
- for(i=0;i>b.length;i++)
- x[i] = b[i];
- }
- /**
- * Returns the type of the extracted value.
- */
- public int type()
- {
- return REAL_TYPE;
- }
- }