Bill Manaris : JMUSIC / Stochastic C Major Java
This program demonstrates how to create a random melody in the C major scale within the 4th, 5th, and 6th octave.

import jm.JMC;
import jm.midi.*;
import jm.util.*;

 * This short example generates a random melody in C major scale
 * and writes to a MIDI file called 'stochy.C-Major.mid'.  It demonstrates
 * how to map a continuous range of real values to the corresponding
 * scale notes within a specified octave range.
 * @author Andrew Brown (modified by Bill Manaris)

public final class StochasticCMajor implements JMC
   public static void main(String[] args)
       final double TEMPO = 80;    // the higher, the faster the melody
       final int NUM_NOTES = 32;   // length of melody

       Score stochScore = new Score("Stochastic melody in C major", TEMPO);
       Part inst = new Part("Piano", PIANO, 0);
       Phrase phr = new Phrase(0.0);

       // Specify octave range for sonification
       // (Note: there are 11 MIDI octaves, i.e., 0 to 10)
       final int LOW_OCTAVE  = 4// start at 4th octave, and extend
       final int NUM_OCTAVES = 3// ...three octaves (i.e., C4..B4, C5..B5, C6..B6)

       // Use C major scale
       // (see
       final int scale[] = MAJOR_SCALE;       

       // number of distinct notes available for mapping data values
       final int MAX_NOTES = scale.length * NUM_OCTAVES;

       // specify range of data values (i.e., min and max)
       Double min = 0.0;   // minimum value in data
       Double max = 0.5;   // maximum value in data

       // create a phrase of NUM_NOTES eigth notes picking a random
       // scale note within the specified range.
       for (int i = 0; i < NUM_NOTES; i++)
           // Map a real value from the range 'min' to 'max'
           // to a scale note within the specified octave range.

           // first, get a data value (random) within the specified data range
           double datum = Math.min(Math.random(), max);

           // map data value to the range of available notes [0 to MAX_NOTES - 1]
           datum = (datum - min) * (MAX_NOTES - 1) / (max - min);
           // now, datum is a multiple of an index into scale[]

           // get corresponding chromatic tone from the scale
           int tone = scale[(int)datum % scale.length];

           // get octave of datum
           int octave = (int)datum / scale.length;

           // adjust pitch for octave (12 notes in an octave)
           int pitch = tone + (octave * 12);   

           // shift pitch by specified offset (12 notes in an octave)
           pitch += (LOW_OCTAVE * 12);   

           Note note = new Note(pitch, EN);   // create a note object
           phr.addNote(note);                 // add it to the phrase

       inst.addPhrase(phr);       // add the phrase to an instrument (part)
       stochScore.addPart(inst)// add the instrument to the score

       // create a MIDI file of the score
       Write.midi(stochScore, "stochy.C-Major.mid");

(Printable View of