Summary of common functions based on math class in Java _java

Source: Internet
Author: User
Tags current time natural logarithm rand root math square root

A summary of several commonly used mathematical formulas in Java:

Rounding, returns the largest integer less than the target function, which returns-2 
Math.floor ( -1.8);//Rounding 
, returning the smallest integer 
math.ceil () 
//Rounded rounding 
of the development target number. Math.Round () 
//calculates the square root math.sqrt () 
///computes the cubic root math.cbrt () 
//returns the N-Power math.exp of Euler number e 
(3); 
Calculate the powers, the following is calculated 3 of the 2 
Math.pow (3,2); 
Compute the natural logarithm 
Math.log (); 
Calculate absolute Value 
math.abs (); 
Calculate the maximum value 
Math.max (2.3,4.5); 
Calculate the minimum value 
math.min (,); 
Returns a pseudo-random number that is greater than or equal to 0.0 and less than 1.0 

The random class is designed to generate a pseudo-random number, which has two constructors: one constructor uses the default seed (at the current time as the seed), and another constructor requires that the programmer display the seed that passes in a long integer.

Random than the random () method of the math provides more ways to generate a variety of pseudo-random numbers.

e.g

Import Java.util.Arrays; 
Import Java.util.Random; 
 
public class Randomtest { 
 
  /** 
   * @param args */public 
  static void Main (string[] args) { 
    //TODO auto-g enerated method Stub 
    Random rand = new Random (); 
    System.out.println ("Random boolean number" + Rand.nextboolean ()); 
    byte[] buffer = new BYTE[16]; 
    Rand.nextbytes (buffer); 
    The production of an array of random numbers containing 16 elements of an array 
    System.out.println (arrays.tostring (buffer)); 
     
    System.out.println ("rand.nextdouble ()" + rand.nextdouble ()); 
     
    System.out.println ("Float floating-point number" + rand.nextfloat ()); 
     
    System.out.println ("Rand.nextgaussian" + Rand.nextgaussian ()); 
     
    System.out.println ("" + Rand.nextint ()); 
     
    The production of a random integer System.out.println between 0~32 
    ("Rand.nextint" + rand.nextint); 
     
    System.out.println ("Rand.nextlong" + Rand.nextlong ()); 
  } 
 

To prevent two random objects from generating the same number sequence, it is often recommended that the current time be used as the seed for the Random object, as follows:

Random rand = new Random (System.currenttimemillis ());

The introduction of Threadlocalrandom in the Java7

In the case of multithreading, the use of threadlocalrandom is similar to the use of random, and the following program fragments demonstrate the usage of threadlocalrandom:

First, a random sequence is generated using current () to produce the desired pseudo-random sequence using nextcxxx ():

Threadlocalrandom trand= threadlocalrandom.current (); 
   

Generating pseudo-random numbers between 4~64

The above is a small series for everyone based on the Java Math Class common function Summary of all the content, hope to help everyone, a lot of support cloud Habitat Community ~

Related Article

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.