package montecarlo;

import java.util.Random;

/** 
 * Extension of the java Random class to permit user-defined transformations
 * of a uniform random deviate.  
 *
 * An instance of MyRandom can be constructed with an anonymous MyRandom.Function class
 * as its argument to define the transformation.  For example, the following will perform
 * a square-root operation on uniform deviate, thereby generating a random deviate on (0,1)
 * with a linear distribution:
 *
 *   MyRandom rand = new MyRandom(new Function() { //anonymous class
 *       public double f(double x) {
 *           return Math.sqrt(x);      //here is the definition of the transformation function
 *                                     //replace line with any desired transformation
 *       }
 *   });
 *
 * Instantiating with no argument results in a uniform deviate.
 *
 */
 
public class MyRandom extends java.util.Random {
    
    private final Random uniform = new Random();  //generator of uniform random deviate in (0,1)
    private final Function t;

    public MyRandom() {         //define a null transformation for no-argument constructor
        t = new Function() {
            public double f(double x) {
                return x;
            }
        };
    }
    public MyRandom(Function trans) {  //constructor with a transformation passed in
        t = trans;
    }
    
    public final double nextDouble() {return t.f(uniform.nextDouble());}
}