/**
 *  Benchmark #5 - Needle-throwing simulation.
 *   @author  Josef.Pelikan@mff.cuni.cz
 *   @version 0.007 (2001/06/17)
 *   @since   0.007
 */
public class BenchmarkNeedle extends Benchmark {

    /** Number of throws. */
    protected final static int NEEDLE_MAX = 100000000;

    /** Number of hits. */
    protected int hit;

    /**
     *  Initializing constructor.
     */
    public BenchmarkNeedle () {
        hit = 0;
    }

    /**
     *  Test (benchmark) ordinal number.
     *   @return Ordinal number of the test.
     */
    public int testNumber () {
        return 5;
    }

    /**
     *  The benchmark algorithm itself. Needs not be re-entrant but can be iterated.
     */
    protected void test () {
        int i;
        double y, height, angle;
        RandomJames rnd = new RandomJames();
        hit = 0;
        for ( i = 0; i++ < NEEDLE_MAX; ) {
            y = rnd.uniformNumber() * 2.0;
            angle = rnd.uniformNumber() * 3.141592653589793;
            height = Math.sin(angle);
            if ( y + height >= 2.0 || y - height < 0.0 ) hit++;
            }
    }

    /**
     *  Result string of the test. Should be called after the <code>runTest()</code>
     *  was finished.
     *   @return Result of the test.
     */
    public String testResult () {
        return( "Needle simulation: N = " + (NEEDLE_MAX/1000000) +
                "M, ratio = " + ((double)hit/NEEDLE_MAX) );
    }

}
