// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Linq; namespace E { public class Program { private static readonly Random urng = new Random(); private static double NRNG(double e, double s) { double u1 = urng.NextDouble(); double u2 = urng.NextDouble(); double h1 = Math.Sqrt(-2 * Math.Log(u1)); double h2 = 2 * Math.PI * u2; double n1 = e + s * h1 * Math.Sin(h2); double n2 = e + s * h1 * Math.Cos(h2); return n1; } private const int SEQLEN = 20; private const int REP = 100000; private static void TestAny(string lbl, double e, double s, Func picker) { double mindrop = double.MaxValue; double maxdrop = double.MinValue; double avgdrop = 0; for(int i = 0; i < REP; i++) { double[] x = new double[SEQLEN]; for(int j = 0; j < x.Length; j++) x[j] = NRNG(e, s); double x1 = picker(x); double x2 = x[x.Length - 1]; double dx = x1 - x2; if(dx < mindrop) mindrop = dx; if(dx > maxdrop) maxdrop = dx; avgdrop += dx; } avgdrop /= REP; Console.WriteLine("{0} for N({1},{2}): drop of {3:F2} ({4:F2} to {5:F2})", lbl, e, s, avgdrop, mindrop, maxdrop); } public static void TestFirstLast(double e, double s) { TestAny("First to last", e, s, x => x[0]); } public static void TestHighLast(double e, double s) { TestAny("High to last", e, s, x => x.Max()); } public static void Test(double e, double s) { TestFirstLast(e, s); TestHighLast(e, s); } public static void Main(string[] args) { Test(10, 1); Test(10, 0.5); Test(10, 0.1); } } }