Package jsynoptic.builtin

Source Code of jsynoptic.builtin.UniformRandomSource$OptionPanel

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*
* $Id: UniformRandomSource.java,v 1.3 2004/01/29 11:09:42 brodu Exp $
*
* Changes
* -------
* 21-Jan-2004 : Creation date (NB);
*
*/
package jsynoptic.builtin;

import simtools.data.DataSource;
import simtools.util.NumberStringComparator;
import jsynoptic.data.DataSourceAnimator;

/**
* Generate numbers using a uniform distribution, bounds included
*/
public class UniformRandomSource extends RandomSource {
   
    public static final String ID_PREFIX = "UniformRandomSource:";
   
    protected void setInfo() {
        info.comment = resources.getString("uniformSourceComment");
        info.id = ID_PREFIX + info.id;
    }
   
    /**
     * @param lowerBound
     * @param higherBound
     */
    public UniformRandomSource(String name, double lowerBound, double higherBound) {
        super(name, lowerBound, higherBound);
        setInfo();
    }

    /**
     * @param lowerBound
     * @param higherBound
     * @param nSamples
     */
    public UniformRandomSource(String name, double lowerBound, double higherBound, long nSamples) {
        super(name, lowerBound, higherBound, nSamples);
        setInfo();
    }

    /**
     * @param seed
     * @param lowerBound
     * @param higherBound
     */
    public UniformRandomSource(String name, long seed, double lowerBound, double higherBound) {
        super(name, seed, lowerBound, higherBound);
        setInfo();
    }

    /**
     * @param seed
     * @param lowerBound
     * @param higherBound
     * @param nSamples
     */
    public UniformRandomSource(String name, long seed, double lowerBound, double higherBound,
            long nSamples) {
        super(name, seed, lowerBound, higherBound, nSamples);
        setInfo();
    }

    /* (non-Javadoc)
     * @see jsynoptic.builtin.RandomSource#nextRandomValue()
     */
    protected double nextRandomValue() {
        double d = generator.nextDouble();
        // ensure we are in the interval, bounds included
        // so, drop the values above a threshold, and take the next one
        // keep the threshold high so not many values are dropped
        if (d>0.96875) return nextRandomValue();
        return d*(param2 - param1)/0.96875+param1;
    }

    /**
     * @return Returns the Lower Bound.
     */
    public double getLowerBound() {
        return param1;
    }

    /**
     * @return Returns Higher Bound.
     */
    public double getHigherBound() {
        return param2;
    }
   
   
    public static class OptionPanel extends RandomSource.OptionPanel {

        /* (non-Javadoc)
         * @see jsynoptic.builtin.RandomSource.OptionPanel#param1Name()
         */
        protected String param1Name() {
            return resources.getString("lowerBound");
        }

        /* (non-Javadoc)
         * @see jsynoptic.builtin.RandomSource.OptionPanel#param2Name()
         */
        protected String param2Name() {
            return resources.getString("higherBound");
        }

        /* (non-Javadoc)
         * @see jsynoptic.builtin.RandomSource.OptionPanel#createSource()
         */
        public DataSource createSource(String name) {
            
            DataSource urs;
            Number seed = null;
            if (cbSeed.isSelected()) seed = NumberStringComparator.stringToNumber(tfSeed.getText());
            if (rbDynamic.isSelected()) {
                Number period = NumberStringComparator.stringToNumber(tfPeriod.getText());
                if (period==null) period = new Long(1000);
                if (seed!=null) urs = new DataSourceAnimator(new UniformRandomSource(name, seed.longValue(), nfParam1.getDoubleValue(), nfParam2.getDoubleValue()), (int)nfSize.getLongValue(), period.intValue());
                else urs = new DataSourceAnimator(new UniformRandomSource(name, nfParam1.getDoubleValue(), nfParam2.getDoubleValue()), (int)nfSize.getLongValue(), period.intValue());
            }
            else {
                if (seed!=null) urs = new UniformRandomSource(name, seed.longValue(), nfParam1.getDoubleValue(), nfParam2.getDoubleValue(), nfSize.getLongValue());
                else urs = new UniformRandomSource(name, nfParam1.getDoubleValue(), nfParam2.getDoubleValue(), nfSize.getLongValue());
            }
            return urs;
           
        }
    }

}

TOP

Related Classes of jsynoptic.builtin.UniformRandomSource$OptionPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.