java Math類

來源:互聯網
上載者:User

標籤:1.0   pretty   另一個   byte   2.0   todo   tostring   tail   數組   

 

原文  http://blog.csdn.net/chichoxian/article/details/20923501

Java中比較常用的幾個數學公式的總結:

//取整,返回小於目標函數的最大整數,如下將會返回-2Math.floor(-1.8);//取整,返回傳育目標數的最小整數Math.ceil()//四捨五入取整Math.round()//計算平方根Math.sqrt()//計算立方根Math.cbrt()//返回歐拉數e的n次冪Math.exp(3);//計算乘方,下面是計算3的2次方Math.pow(3,2);//計算自然對數Math.log();//計算絕對值Math.abs();//計算最大值Math.max(2.3,4.5);//計算最小值Math.min(,);//返回一個偽隨機數,該數大於等於0.0並且小於1.0Math.random

Random類專門用於產生一個偽隨機數,它有兩個構造器:一個構造器使用預設的種子(以目前時間作為種子),另一個構造器需要程式員顯示的傳入一個long型整數的種子。

Random比Math的random()方法提供了更多的方式來產生各種偽隨機數。

e.g

import java.util.Arrays;import java.util.Random;public class RandomTest {    /**     * @param args     */    public static void main(String[] args) {// TODO Auto-generated method stubRandom rand = new Random();System.out.println("隨機布爾數" + rand.nextBoolean());byte[] buffer = new byte[16];rand.nextBytes(buffer);//生產一個含有16個數組元素的隨機數數組System.out.println(Arrays.toString(buffer));System.out.println("rand.nextDouble()" + rand.nextDouble());System.out.println("Float浮點數" + rand.nextFloat());System.out.println("rand.nextGaussian" + rand.nextGaussian());System.out.println("" + rand.nextInt());//生產一個0~32之間的隨機整數System.out.println("rand.nextInt(32)" + rand.nextInt(32));System.out.println("rand.nextLong" + rand.nextLong());    }}

為了避免兩個Random對象產生相同的數字序列,通常推薦使用目前時間作為Random對象的種子,代碼如下:

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

在 java7 中引入了ThreadLocalRandom 

在多線程的情況下使用ThreadLocalRandom的方式與使用Random基本類似,如下程式·片段示範了ThreadLocalRandom的用法:

首先使用current()產生隨機序列之後使用nextCXxx()來產生想要的偽隨機序列 :

ThreadLocalRandom trand= ThreadLocalRandom.current();                int val = rand.nextInt(4,64);

產生4~64之間的偽隨機數 

public class MathDemo {      public static void main(String args[]){          /**          * abs求絕對值          */          System.out.println(Math.abs(-10.4));    //10.4          System.out.println(Math.abs(10.1));     //10.1                    /**          * ceil天花板的意思,就是返回大的值,注意一些特殊值          */          System.out.println(Math.ceil(-10.1));   //-10.0          System.out.println(Math.ceil(10.7));    //11.0          System.out.println(Math.ceil(-0.7));    //-0.0          System.out.println(Math.ceil(0.0));     //0.0          System.out.println(Math.ceil(-0.0));    //-0.0                    /**          * floor地板的意思,就是返回小的值          */          System.out.println(Math.floor(-10.1));  //-11.0          System.out.println(Math.floor(10.7));   //10.0          System.out.println(Math.floor(-0.7));   //-1.0          System.out.println(Math.floor(0.0));    //0.0          System.out.println(Math.floor(-0.0));   //-0.0                    /**          * max 兩個中返回大的值,min和它相反,就不舉例了          */          System.out.println(Math.max(-10.1, -10));   //-10.0          System.out.println(Math.max(10.7, 10));     //10.7          System.out.println(Math.max(0.0, -0.0));    //0.0                    /**          * random 取得一個大於或者等於0.0小於不等於1.0的隨機數          */          System.out.println(Math.random());  //0.08417657924317234          System.out.println(Math.random());  //0.43527904004403717                    /**          * rint 四捨五入,返回double值          * 注意.5的時候會取偶數          */          System.out.println(Math.rint(10.1));    //10.0          System.out.println(Math.rint(10.7));    //11.0          System.out.println(Math.rint(11.5));    //12.0          System.out.println(Math.rint(10.5));    //10.0          System.out.println(Math.rint(10.51));   //11.0          System.out.println(Math.rint(-10.5));   //-10.0          System.out.println(Math.rint(-11.5));   //-12.0          System.out.println(Math.rint(-10.51));  //-11.0          System.out.println(Math.rint(-10.6));   //-11.0          System.out.println(Math.rint(-10.2));   //-10.0                    /**          * round 四捨五入,float時返回int值,double時返回long值          */          System.out.println(Math.round(10.1));   //10          System.out.println(Math.round(10.7));   //11          System.out.println(Math.round(10.5));   //11          System.out.println(Math.round(10.51));  //11          System.out.println(Math.round(-10.5));  //-10          System.out.println(Math.round(-10.51)); //-11          System.out.println(Math.round(-10.6));  //-11          System.out.println(Math.round(-10.2));  //-10      }  }  

 

java Math類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.