Java generates random numbers

Source: Internet
Author: User

For this problem, I only put the code on it, the code has comments, believe that smart you can understand!

  

ImportJava.util.Random;//need to use this class package/** Java generates a random number * time:2017/10/16 * method One: The random method in the Math class can be used to generate the number of randomly generated * the Math.random () method returns a double with a positive sign, which is greater than 0 less than 1. * Of course, we can also make changes: * (int) (Math.random () *10) returns the random number between 0-9 * (int) (math.random () *n) returns a random number between 0-n * (int) (Math.random () *100) +1 returns a random number between 0-100, preceded by 0-99, plus 1 is 0-100 * * Method Two: The Random class * must be written before generating an arbitrary number: random random=new random ();    * Random.nextint ();    Returns a random number in the range of type int * Random.nextint (10);    Returns a random number between 0-9 * random.nextint (100) +1;        Returns a random number between 1-100 * Random.nextint (n) +m; The return is the random number between M and m+n-1.*/ Public classRandomnum { Public Static voidMain (string[] args) {//Method One        DoubleNum=math.random ();//returns a number of type doubleSystem.out.println (num); //The following generates an int type if the method is not enclosed, only 0 is returned        intNum1= (int) (Math.random () *10);//returns the random number between 0-9System.out.println (NUM1); intNum2= (int) (Math.random () *100);//returns the expedited between 0-99System.out.println (num2); //Method TwoRandom random=NewRandom ();//method Two must write this sentence, random is just a name, take        intS=random.nextint ();//generates a random number of type intSystem.out.println ("Generated random number is:" +s); intS2=random.nextint (10);//generate a random number between 0-9System.out.println (S2); }}
View Code

The result of the operation is:

  

the random number generated by 0.3248983535648986691 is:-19098713886

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.