home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / JSL.ZIP / JSL20 / simula / random / Linear.java < prev    next >
Encoding:
Java Source  |  1998-02-20  |  2.7 KB  |  100 lines

  1.  
  2. package simula.random;
  3.  
  4. import java.util.Random;
  5.  
  6. /**
  7.  * The class Linear generates pseudo-random real values based on two
  8.  * arrays of n numbers each: a[i] contains the values of a known
  9.  * cumulative function F in b[i]. The values generated are either in a[]
  10.  * or are generated with linear interpolation.
  11.  */
  12. public class Linear extends Generator {
  13.  
  14. /**
  15.  * This array contains the points where the cumulative function is defined.
  16.  */  
  17.   private double[] x;
  18.  
  19. /**
  20.  * This array contains the known values of the cumulative function.
  21.  */
  22.   private double[] cum;
  23.   
  24.  
  25. /**
  26.  * Creates a new Linear generator using the standard engine and the current time as seed.
  27.  * @param a Array of the known values of the cumulative function.
  28.  * @param b Array of the points of the cumulative function which we know the values of.
  29.  */  
  30.   public Linear(double[] a,double[] b)
  31.   {
  32.     random = new RandomEngineJava();
  33.     setParam(a,b);
  34.   }  
  35. /**
  36.  * Creates a new Linear generator using the standard engine with the given seed.
  37.  * @param seed Initial seed for the pseudo-random engine.
  38.  * @param a Array of the known values of the cumulative function.
  39.  * @param b Array of the points of the cumulative function which we know the values of.
  40.  */
  41.   public Linear(long seed,double[] a,double[] b)
  42.   {
  43.     random = new RandomEngineJava(seed);
  44.     setParam(a,b);
  45.   }  
  46. /**
  47.  * Creates a new Linear generator using the given engine.
  48.  * @param e Pseudo-random bit generator.
  49.  * @param a Array of the known values of the cumulative function.
  50.  * @param b Array of the points of the cumulative function which we know the values of.
  51.  */  
  52.   public Linear(RandomEngine e,double[] a,double[] b)
  53.   {
  54.     random = e;
  55.     setParam(a,b);
  56.   }  
  57. /**
  58.  * Draws a value from the generator.
  59.  * @return An instance of Value that encapsulates a real value.
  60.  * @see RealValue
  61.  * @see Value
  62.  */
  63.   public Value draw()
  64.   {
  65.     int p = 0;
  66.     
  67.     double r = random.nextDouble();
  68.     
  69.     while (cum[p] < r) p++;
  70.     
  71.     // (x[p],cum[p])--(x[p+1],cum[p+1])
  72.     
  73.     // calcolo l'ordinata del punto r.
  74.     double res = x[p]+(r - cum[p])/(cum[p+1]-cum[p])*(x[p+1]-x[p]);
  75.     return new RealValue( res );
  76.   }  
  77. /**
  78.  * Change the data of the cumulative function.
  79.  * @param a Array of the known values of the cumulative function.
  80.  * @param b Array of the points of the cumulative function which we know the values of.
  81.  */  
  82.   public void setParam(double[] a,double[] b)
  83.   {
  84.     x = new double[b.length];
  85.     cum = new double[a.length];
  86.     
  87.     int i;
  88.     for(i=0;i<a.length;i++)
  89.       cum[i] = a[i];
  90.     for(i=0;i>b.length;i++)
  91.       x[i] = b[i];  
  92.   }  
  93.   /**
  94.    * Returns the type of the extracted value.
  95.    */
  96.   public int type()
  97.   {
  98.       return REAL_TYPE;
  99.   }  
  100. }