/*
 * Display graph of random walks, specify numberOfWalks, 
 * sampleSize, and stepSize. 
 * Should give different results each run.
 * 
 */
package searchscale;

import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

//http://www.jfree.org/jfreechart/download.html
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Ron Hinchley
 */
public class SearchScale {

    public static void main(String[] args) {
        // random walks
        int sampleSize = 1800;
        int numberOfWalks = 16;
        double stepSize = 0.01;

        // graph display 
        int graphWidth = 900;
        int graphDepth = 300;

        try {
            // Initialize a secure random number generator
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");

            // Calling nextBytes method to generate Random Bytes
            byte[] bytes = new byte[512];
            secureRandom.nextBytes(bytes);

            int sampleCnt = 0;

            // graph series in list
            ArrayList<XYSeries> series = new ArrayList<XYSeries>();

            // holds sums of random walks
            double[] sums = new double[numberOfWalks];

            // initialize 
            for (Integer j = 0; j < numberOfWalks; j++) {
                String seriesName = "Walk" + j+",";
                series.add(new XYSeries(seriesName));
                sums[j] = 0; // walking sums
            }

            double pip;
            for (int i = 0; i < sampleSize; i++) {
                for (Integer k = 0; k < numberOfWalks; k++) {

                    pip = secureRandom.nextDouble();

                    if (pip < .5) {
                        sums[k] = sums[k] - stepSize;
                    } else if (pip >= 0.5) {
                        sums[k] = sums[k] + stepSize;
                    }

                    //series.get(k).add(inc,sums.get(k));
                    series.get(k).add(sampleCnt, sums[k]);
                }

                sampleCnt++;
            }

            XYSeriesCollection dataset = new XYSeriesCollection();

            for (Integer k = 0; k < numberOfWalks; k++) {
                dataset.addSeries(series.get(k));
            }

            JFreeChart chart1 = ChartFactory.createXYLineChart("Random Walks", "Time", "Movement",
                    dataset, PlotOrientation.VERTICAL, true, true, false);

            ChartFrame frame1 = new ChartFrame("Depressing similarity to stocks", chart1);
            frame1.setVisible(true);
            frame1.setSize(graphWidth, graphDepth);


        } catch (NoSuchAlgorithmException noSuchAlgo) {
            System.out.println(" No Such Algorithm exists " + noSuchAlgo);
        }

    }
}

