Math&Random&ThreadLocalRandom類

Math類

//絕對值值運算:
Math.abs(18.999);                 //返回19.999這個數的絕對值  
Math.abs(-12.58);                 // 返回-12.58這個數的絕對值,爲12.58

//取值運算:
Math.signum(x);                  //若是x大於0則返回1.0,小於0則返回-1.0,等於0則返回0 

//取整運算:                  
Math.ceil(-13.56);                //返回最近的且大於這個數的整數, 爲14.0  
Math.floor(-13.56);               //返回最近的且小於這個數的整數, 爲13.0
Math.rint(13.56);               ////返回最接近這個數的整數,若是恰好居中,則取偶數,爲14

//對數運算:
Math.log10(100);                //  以10爲底數的100的對數 ,爲2

//指數運算:
Math.expm1(x);                  //e的x次冪 -
Math.pow(a,b);                  //a的b次冪
Math.scalb(x, y);               //x*(2的y次冪)  

//二次方根運算:
Math.sqrt(x);                   //x的二次方根 

//三次方根運算:
Math.cbrt(x);                  //x的三次方根

//返回較大值和較小值:  
Math.max(x, y);                 //返回x、y中較大的數
Math.min(x, y);                 //返回x、y中較小的數

//三角函數運算:  
Math.sin(α);                    //sin(α)的值  
Math.cos(α);                    //cos(α)的值  
Math.tan(α);                    //tan(α)的值  

//求角運算:
Math.asin(x/z);                 //返回角度值[-π/2,π/2]  arc sin(x/z)  
Math.acos(y/z);                 //返回角度值[0~π]        arc cos(y/z)  
Math.atan(y/x);                 //返回角度值[-π/2,π/2]  arctan(y/x)

//隨機值運算:
Math.random();                 //隨機返回[0,1)之間的無符號double值



Random&ThreadLocalRandom類

Random類

Random 是一個線程安全類,理論上能夠經過它同時在多個線程中得到互不相同的隨機數,可是在多線程的場景下須要多個線程競爭同一個原子變量的更新操做,性能不佳。java

多線程性能參見:https://www.imooc.com/article/29739數組

有兩種構造方法:安全

  • Random():以當前系統時間做爲種子。
  • Random(long seed):使用單個 long 型整數做爲種子。
public static void main(String[] args) {
    Random rand = new Random();
    //隨機boolean值
    System.out.println(rand.nextBoolean());  //true

    //隨機填充byte數組
    byte[] buffer = new byte[16];
    rand.nextBytes(buffer);
    //[106, -85, 66, 108, 93, -22, 114, -67, -97, -99, 34, 126, 3, 66, -25, 59]
    System.out.println(Arrays.toString(buffer));

    //隨機[0.0, 1.0) 區間的double值
    System.out.println(rand.nextDouble());  //0.6032052834419511

    //隨機[0.0, 1.0) 區間的float值
    System.out.println(rand.nextFloat());  //0.19521767

    //隨機int範圍的整數
    System.out.println(rand.nextInt());  //-1557426129

    //隨機生成[0, 15)區間的整數
    System.out.println(rand.nextInt(15));  //6

    //隨機long範圍整數
    System.out.println(rand.nextLong());  //868994934892445287
}


ThreadLocalRandom類(推薦)

ThreadLocalRandon 類是 JDK 1.7 新增的一個類,它是 Random 的加強版,在併發訪問的環境下,使用它來代替 Random 能夠減小多線程資源競爭,最終保證系統具備更好的線程安全性。ThreadLocalRandom 類提供了一個靜態方法來獲取當前線程的隨機數生成器:多線程

ThreadLocalRandom rand = ThreadLocalRandom.current();

ThreadLocalRandom 方法的用法和 Random 基本相似,增長几個功能:併發

//返回一個boolean類型的隨機數
public boolean nextBoolean();

//隨機填充一個byte數組
public void nextBytes(byte[] bytes);

//返回一個[0.0,1.0)範圍的float隨機數
public float nextFloat();

//返回一個[0.0,1.0)範圍的double隨機數
public double nextDouble();

//返回一個[0.0-bound)之間的double隨機數
public double nextDouble(double bound);

//返回一個[origin-bound)之間的隨機數
public double nextDouble(double origin, double bound);

//返回一個整型的僞隨機數
public int nextInt();

//返回一個[0,bound)之間的隨機數
public int nextInt(int bound);

//返回一個[origin,bound)之間的隨機數
public int nextInt(int origin, int bound);

//返回一個long型隨機數
public long nextLong();

//返回一個[0,bound)之間的隨機數
public long nextLong(long bound);

//返回一個[origin,bound)之間的隨機數
public long nextLong(long origin, long bound);



參考

  1. https://blog.csdn.net/qq_33213136/article/details/76242273
  2. https://www.imooc.com/article/29739
相關文章
相關標籤/搜索