// File: random_example2.java // // Author: Rahul Simha // Created: Sept 5, 2000 // public class random_example2 { // Basic Lehmer generator - constants static final long m = 2147483647L; static final long a = 48271L; static final long q = 44488L; static final long r = 3399L; // "Global" variable - the seed, set to some // arbitrary non-zero value. static long r_seed = 12345678L; // Basic Lehmer generator - uniform[0,1] // For more information see Knuth, Vol. II. public static double uniform () { long hi = r_seed / q; long lo = r_seed - q * hi; long t = a * lo - r * hi; if (t > 0) r_seed = t; else r_seed = t + m; return ( (double) r_seed / (double) m ); } public static void main (String[] argv) { double[] A = new double [5]; int numArrays = 10000; int numOccurences = 0; for (int n=1; n<=numArrays; n++) { // Generate random values. for (int i=0; i<5; i++) A[i] = uniform(); // Check for property. boolean property = true; for (int i=1; i<5; i++) { if (A[i] < A[i-1]) property = false; } // Count. if (property) numOccurences++; } // Now estimate probability double prob = (double) numOccurences / (double) numArrays; System.out.println ("Probability estimate: " + prob); } }