public class Random {private static final int a=48271;private static final int m=2147483647;private static final int q=m/a;private static final int r=m%a;public Random () {This ((int) (System.currentt Imemillis ()%integer. Max_value));} /*construct this b random object with specified inital state * */public Random (int initialvalue) {if (Initi alvalue<0) initialvalue+=m;state=initialvalue;if (state==0) state=1;} public int nextint () {int tmpstate=a* (STATE%Q)-r* (state/q), if (tmpstate>=0) state=tmpstate;elsestate=tmpstate+m; return state;} public int Nextintwrong () {return state = (A * state)% M;} Public double nextdouble () {return (double) nextint ()/M;} Public long Nextlong () {return (long) nextint () <<) + Nextint ();} public int Nextint (Int. Low,int High) {Double partitionsize = (double) M/(high-low + 1); return (int) (Nextint ()/partitionsize) + low;} public int Nextpoissson (Double excepctedvalue)//obey the random number generation of Poisson distribution {double limit=-excepctedvalue;double product =math.log (nextdouble ()); int C Ount;for (count=0;product>limit;count++) Product+=math.log (nextdouble ()); return count;} Public double nextnegexp (double exceptedvalue)//The generation of random numbers subjected to negative exponential distributions, negative exponent = distribution has the same mean and variance, used to characterize the time interval of random events {Return-ex Ceptedvalue*math.log (Nextdouble ()); }public static final <AnyType> void Permute (AnyType [] a) {random r = new Random (); for (int j = 1; j < A.length; J + +) Swapreferences (A, J, R.nextint (0, J));} private static final <AnyType> void Swapreferences (AnyType [] A, int index1, int index2) {AnyType TMP = a[ind EX1]; a[index1] = a[Index2]; a[INDEX2] = tmp;} private int state;//Test programpublic static void Main (String [] args) {Random r = new Random (); for (int i = 0; i <; i++) System.out.println (R.nextint ()); int [] dist = new int[10000]; Final intSAMPLES = 1000000; for (int i = 0; i < SAMPLES; i++) dist[R.nextpoissson (2)]++; for (int i = 0; i < i++) System.out.println (i + ":" + dist[i]/(double) SAMPLES); }}
Random number generator