Java generates random numbers

Source: Internet
Author: User

Objective:

Each programming language basically has a random function, and Java produces a random number in the way eclectic. and the Random tool class also has a more in-depth application, but this article only compares 3 kinds of ways to generate random numbers, do not deeply expand the analysis of its internal tool class.

1) the System.currentmillis () function returns a long integer random number based on the current time;
2) Math.random () returns the floating-point number from 0 to 1, and belongs to left-closed right: [0,1];
3) Instantiate the object with the new random (). Nextint () and take advantage of the function to produce a random number of type int.

The code implementations in three different ways are as follows:

1  PackageRandom;2 3 ImportJava.util.Random;4 5 Importorg.junit.Test;6 7  Public classRandomtest {8 9      Public Static voidMain (string[] args) {Ten         Newrandomtest (). TESTRANDOM1 (); One         Newrandomtest (). TestRandom2 (); A         Newrandomtest (). TESTRANDOM3 (); -     } -  the     /* - * Returns a single long type of random number based on the current standard time -      */ - @Test +      Public voidtestRandom1 () { - System.out.println (System.currenttimemillis ()); +     } A  at     /* - * Use the math class to generate a random number, which returns a floating-point type with a range of: [0,1] -      */ - @Test -      Public voidtestRandom2 () { -          for(inti=0;i<10;i++) in System.out.println (Math.random ()); -     } to  +     /* - * Using the RANDOML tool class, generate 10 random numbers as seed seeds, the resulting 2 sequences are the same the      */ * @Test $      Public voidtestRandom3 () {Panax NotoginsengRandom random1 =NewRandom (1); -          for(inti = 0; I < 10; i++) { theSystem.out.print (Random1.nextint () + ""); +         } A System.out.println (); theRandom random2 =NewRandom (1); +          for(inti = 0; I < 10; i++) { -System.out.print (Random2.nextint () + ""); $         } $     } -}

In addition , considering that in some cases we need to generate random numbers in batches, we write the following procedure. Its function is to implement batch generation of random numbers in the range of n [0,max] and write txt files:

1  PackageRandom;2 3 ImportJava.io.File;4 ImportJava.io.PrintWriter;5 6  Public classRandomfactory {7 8     Final Static intn=1000000;//The number of random numbers produced9     Final Static intmax=10000;//generate a range of random numbers: [0,max]Ten     Final StaticString path= "D:/random100w.txt";//the resulting file path One      Public Static voidMain (string[] args)throwsexception{ A          -PrintWriter output =NewPrintWriter (NewFile (PATH)); -          for(inti=0;i<n;i++){ the             intX= (int) (Math.random () *1e4); - output.println (x); -         } -         //remember to close the character stream +         if(output!=NULL){ - output.close (); +         } ASystem.out.println ("--end--"); at     } -  -}



Java generates random numbers

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.