import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class Random { private double nextNextGaussian; private boolean haveNextNextGaussian = false; public double nextGaussian() { if (haveNextNextGaussian) { haveNextNextGaussian = false; return nextNextGaussian; } else { double v1, v2, s; do { v1 = 2 * Math.random() - 1; // between -1.0 and 1.0 v2 = 2 * Math.random() - 1; // between -1.0 and 1.0 s = v1 * v1 + v2 * v2; } while (s >= 1 || s == 0); double multiplier = StrictMath.sqrt(-2 * StrictMath.log(s)/s); nextNextGaussian = v2 * multiplier; haveNextNextGaussian = true; return v1 * multiplier; } } public static void main ( String[] args ) { String fileName = "default.dat"; int count = 100; double d = 0.0; Random r = new Random(); PrintWriter output = null; if ( args.length > 0 ) { fileName = args[0]; if ( args.length > 1 ) { try { count = Integer.parseInt(args[1]); } catch ( NumberFormatException nfe ) { System.err.println("Invalid number format for count, defaulting to " + count + "."); } if ( count <= 0 ) { System.err.println("Error: count must be positive."); System.exit(-1); } } } try { output = new PrintWriter( new FileOutputStream(fileName) ) ; } catch ( FileNotFoundException fnfe ) { System.err.println("Error: could not open " + fileName + " for writing."); System.exit(-1); } for ( int i = 0; i < count; i++ ) { output.println("" + r.nextGaussian()); } output.close(); } }