package jmsltestsuite; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import com.didkovsky.portview.PVFrame; import com.softsynth.jmsl.*; import com.softsynth.jmsl.view.MusicShapeEditor; import com.softsynth.jmsl.view.PVFrameAdapter; public class JMSLRandomTest { /** Exercise random number generating methods. Includes benchmarking Gauss, and building a Gaussian bell shape and displaying in Shape editor */ public static void main(String args[]) { JMSLRandom.randomize(); for (int i = 0; i < 5; i++) { System.out.println("Random doubles: " + JMSLRandom.choose()); } for (int i = 0; i < 5; i++) { System.out.println("Random integers: " + JMSLRandom.choose(10)); } int heads = 0; int tails = 0; for (int i = 0; i < 12000; i++) { if (JMSLRandom.choose(2) == 0) heads++; else tails++; } System.out.println("heads: " + heads + " tails: " + tails); System.out.println("Window choose() between [10..20)"); for (int i = 0; i < 150; i++) { System.out.print(" " + JMSLRandom.choose(10, 20)); } System.out.println(); double plusMinusRange = 1; for (int i = 0; i < 5; i++) { System.out.println("Random -1.. +1 : " + JMSLRandom.choosePlusMinus(plusMinusRange)); } double result; long timeBefore = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { result = JMSLRandom.gauss(16, 64); } long timeAfter = System.currentTimeMillis(); double seconds = (timeAfter - timeBefore) / 1000.0; System.out.println("10000 Gauss's took " + seconds + " seconds"); MusicShape gaussBell = new MusicShape(16); for (int i = 0; i < 100; i++) { double data[] = new double[gaussBell.dimension()]; gaussBell.add(data); } for (int j = 0; j < gaussBell.dimension(); j++) { double total = 0; double sigma = j + 1; gaussBell.setDimensionName(j, "sigma=" + sigma); for (int i = 0; i < 30000; i++) { double oneGauss = JMSLRandom.gauss(sigma, 50); total += oneGauss; if (Limits.within(oneGauss, 0, gaussBell.size() - 1)) gaussBell.sumSet(1.0, (int) oneGauss, j); // increment the place where it hit } System.out.println("total gauss over 30000 iters " + total); } for (int dim = 0; dim < gaussBell.dimension(); dim++) { double area = 0; for (int i = 0; i < gaussBell.size(); i++) { area += gaussBell.get(i, dim); } System.out.println("Area under dim " + dim + " = " + area); } double minGauss = Double.MAX_VALUE; double maxGauss = Double.MIN_VALUE; System.out.println("Check gauss range for a sequence of gauss(1.0, 0)"); for (int i = 0; i < 1000000; i++) { double g = JMSLRandom.gauss(1.0, 0); minGauss = Math.min(minGauss, g); maxGauss = Math.max(maxGauss, g); } System.out.println("gauss range: (" + minGauss + ", " + maxGauss + ")"); MusicShapeEditor se = new MusicShapeEditor(); se.addMusicShape(gaussBell); PVFrame f = new PVFrameAdapter("Close to Exit"); f.add(se.getComponent()); f.pack(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setVisible(true); } }