Random Distributions

Summary

This demo illustrates the extensive random number generation capabilities of the JMSL Library.

Usage

Click on the different distribution names to generate a set of random numbers; the resulting values will be binned into a histogram which is plotted in a separate window. Set the number of samples to generate and the number of bins in the histogram by typing in the text areas above the list. Some summary information is printed below the list.

JMSL Library Math/Stat Classes

com.imsl.stat.Random - various methods of this class are used to generate the different distributions. The typical way to use this class is to instantiate the class, then use one of the "next" methods to retrieve a random number that fits a certain distribution. For example:

Random r = new Random();
val = r
.nextCauchy();

com.imsl.math.Cholesky - the output of this class is required for the nextMultivariateNormal() method of Random.

JMSL Library Charting Classes

The chart is a histogram created from com.imsl.chart.Bar. Of note is the construction of the labels for the x axis. Since the range of this axis is dynamic, labels could not be hard coded; however, informative range values for each bin was desired. The solution is to create a new String array for each chart and then construct the labels manually. The variables minv and maxv are the minimum and maximum extents of the set of random numbers.

// Setup the X axis for a labeled bar chart.
String
spacer = " : ";
String
labels[] = new String[numBins];
java
.text.DecimalFormat df = new java.text.DecimalFormat();
if
(randomK != 15) {
    df
.applyPattern("##0.00");
}
else { // special format for logNormal
    df
.applyPattern("0.0000");
}

labels[0] = String.valueOf(df.format(minv)) + spacer +
    String.valueOf(df.format(minv+wx));
for
(int i = 1; i < numBins-1; i++) {
    labels
[i] = String.valueOf(df.format(minv + i*wx)) + spacer +
        String.valueOf(df.format(minv + (i+1)*wx));
}

labels[numBins-1] = String.valueOf(df.format(maxv-wx)) + spacer +
        String.valueOf(df.format(maxv));
bar
.setLabels(labels, bar.BAR_TYPE_VERTICAL);
axis
.getAxisX().getAxisLabel().setTextAngle(270);

Java Code

The main class of this example extends JFrame instead of JFrameChart as many others do. JFrameChart nicely provides a menu bar with File | Print, File | Exit, and Help | About items. To maintain consistency, these elements of a JFrameChart are reproduced here. The user interface elements are created in the initComponents() method, with separate items to take care of each action:

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
    // File -> Print
    java.awt.print.PrinterJob printJob = java.awt.print.PrinterJob.getPrinterJob();
    Chart chart = displayArea.getChart();
    printJob.setPrintable(chart);
    if (printJob.printDialog()) {
        try {
            printJob.print();
        } catch (Exception e) {
            javax.swing.JOptionPane.showMessageDialog(this, e.getMessage(),
                "Exception", javax.swing.JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }
}


private
void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
    // File -> Exit

    System.exit(0);
}

private
void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
    // Help -> About

    String about = "Informative string\n";
    javax.swing.JOptionPane.showMessageDialog(this, about, "About",
        javax.swing.JOptionPane.INFORMATION_MESSAGE);
}

Link to Source Code

RandomGen.java This is the main class that extends JFrame. The chart is created in a separate JDialog holding a JPanelChart.

Running This Demo

Two alternatives are available to run this demo:

1) Use the source code in your development environment as any other Java code. More information is available in the How To.

2) An executable jar file containing all of the demos referenced in this guide is included in the jmsl/lib directory. On Windows, you may double-click the file to run it if files with a ".jar" extension are properly registered with javaw.exe. Alternatively, for both Windows and UNIX environments, the jar file may be executed from the command line using java -jar gallery.jar.

As list of buttons, one for each demo, is created. Demos can be subsetted as they relate to specific areas (Math, Stat, Finance, Charting) by choosing the appropriate selection on the JComboBox. To run the Additional Demos, select Quick Start in the JComboBox.