JAVA API (iii) Math class and Random class

Source: Internet
Author: User

1.Math class

The math class is a mathematical manipulation class that provides a number of static methods for mathematical calculations. There are two static constants pi and E in the math class, each representing the mathematical constants π and E.

In the list are some common methods of the math class:

Method declaration Function description
int abs (int a) Calculate the absolute value of a
Double Ceil (Double A) Rounding up to find the smallest integer greater than the parameter
Double floor (Double a) Rounding down to find the largest integer less than the parameter
Long round (double a) Represents rounding, the algorithm is Math.floor (a+0.5)
Double Max (double a,double b) Take a larger value of two numbers
Double min (double a,double b) Take a small value of two numbers
Double Random () Generate a random number from 0 to 1

Here are some examples of common methods in the math class.

Example01.java

 Public classEXAMPLE01 { Public Static voidMain (string[] args) {System.out.println ("The result of calculating absolute value:" + math.abs (-1)); System.out.println ("Ask for the smallest integer greater than the parameter:" + Math.ceil (5.6)); System.out.println ("To find the maximum integer less than the parameter:" + Math.floor (-4.2)); System.out.println ("Rounding the decimal results:" + math.round (-4.6) );//round () indicates rounding, the algorithm is Math.floor (x+0.5) System.out.println ("A larger value of two numbers:" + math.max (2.1,-2.1)); System.out.println ("A small value of two numbers:" + math.min (2.1,-2.1)); System.out.println ("Generate a random number greater than or equal to 0.0 less than 1.0:" +math.random ()); }}

The results of the operation are as follows:

2.Random class

The random class exists in the Java.util package and can produce random numbers within a specified range of values. Here's how it's constructed:

Method declaration Function description
Random () Construction method for creating a pseudo-random number generator
Random (Long Seed) Construction method, using a long seed seed to create a pseudo-random number generator

In the above constructor, the first constructed method is parameterless, and the random instance object created by it is randomly seeded each time, so that each object produces a different number of randomly generated numbers. If you want to create more than one random instance object that produces the same sequence of randomness, you can call the second construction method when you create the object, passing in the same seed. The following is an example of a construction method:

Example02.java

 Public class EXAMPLE02 {    publicstaticvoid  main (string[] args) {        new Random (); // do not pass        in seeds // randomly generates an integer        between 10 "0,100"  for (int i = 0; i < ten; i++) {            System.out.println (Random.nextint);}}}    

The first run results are as follows:

The second run results are as follows:

As can be seen from the above two results, the sequence of random numbers generated two times is different. This is because when you create an instance object of random, no seed is specified, and the system is seeded with the current timestamp, generating a random number.

Here's a little change to the code:

Example03.java

 Public class Example03 {    publicstaticvoid  main (string[] args) {        new Random (13); // incoming seed        when creating an object // randomly generates an integer        between 10 "0,100"  for (int i = 0; i < ten; i++) {            System.out.println (Random.nextint);}}}    

The first run results are as follows:

The second run results are as follows:

As you can see from the two run results, when you create an instance object of the random class, if you specify the same seed, the random number produced is the same each time.

The random class provides more methods to generate a variety of pseudo-random numbers, not only to generate random numbers of integer types, but also to generate random numbers of floating-point types.

Method declaration Function description
Boolean Nextboolean () Randomly generated random numbers of Boolean types
Double nextdouble () Randomly generate a random number of type double
Float Nextfloat () Randomly generate random numbers of type float
int Nextint () Randomly generate random numbers of type int
int nextint (int n) Randomly generated random numbers of type int between 0~n
Long Nextlong () Randomly generate a long type of random number

Let's look at the use of these methods in the following example:

Example04.java

 Public class Example04 {    publicstaticvoid  main (string[] args) {        new Random (); // Create a random instance object        System.out.println ("Generate random number of type float:" + r1.nextfloat ());        System.out.println ("produces a random number of type int between 0-100:" + r1.nextint (+));        System.out.println ("produces a double type of random number:" + r1.nextdouble ());}    }

The results of the operation are as follows:

As can be seen from the results above, different types of random numbers are produced by invoking the random class in separate ways.

JAVA API (iii) Math class and Random class

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.