Random number generator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.