using System; namespace randplot { /// /// Long period (> 2 ¡¿ 1018) random number generator of L¡¯Ecuyer with Bays-Durham shuffle /// and added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of /// the endpoint values). Call with idum a negative integer to initialize; thereafter, do not alter /// idum between successive deviates in a sequence. RNMX should approximate the largest floating /// value that is less than 1. /// public class Ran2 { long IM1 = 2147483563; long IM2 = 2147483399; double AM = 1.0/2147483563.0; long IMM1 = 2147483562; long IA1 = 40014; long IA2 = 40692; long IQ1 = 53668; long IQ2 = 52774; long IR1 = 12211; long IR2 = 3791; int NTAB = 32; double NDIV = 1.0+2147483562.0/32.0; double RNMX = 1.0-1.2e-7; public Ran2() { } public Ran2(long a) { idum = a; } long iy=0; long idum2=123456789; long[] iv = new long[32]; private long idum = 1; public long Idum { get { return idum;} set { idum = value;} } public double ran2() { int j; long k; double temp; if (idum <= 0) { if (-(idum) < 1) idum=1; else idum = -(idum); idum2=(idum); for (j=NTAB+7;j>=0;j--) { k=(idum)/IQ1; idum=IA1*(idum-k*IQ1)-k*IR1; if (idum < 0) idum += IM1; if (j < NTAB) iv[j] = idum; } iy=iv[0]; } k=(idum)/IQ1; idum=IA1*(idum-k*IQ1)-k*IR1; if(idum < 0) idum += IM1; k=idum2/IQ2; idum2=IA2*(idum2-k*IQ2)-k*IR2; if (idum2 < 0) idum2 += IM2; j=Convert.ToInt32(iy/NDIV)%NTAB; iy=iv[j]-idum2; iv[j] = idum; if (iy < 1) iy += IMM1; if ((temp=AM*iy) > RNMX) return RNMX; else return temp; } } } /*private void button1_Click(object sender, System.EventArgs e) { NR.RandomNumbers.Ran2 dd = new NR.RandomNumbers.Ran2(); dd.Idum = 10; for(int k = 0; k < 100; k++) textBox1.Text += Convert.ToString(dd.ran2())+"\r\n"; } */