Java StrictMath random()方法

java.lang.StrictMath.random() 方法返回一个带正号double值,大于或等于0.0并且小于1.0.此方法正确同步以允许多个线程正确使用。

1 语法

public static double random()

2 参数

3 返回值

此方法返回伪随机大于或等于0.0且小于1.0的double值。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java StrictMath random()方法
 */
import java.lang.*;

public class StrictMathDemo {

  public static void main(String[] args) {
  
    /* returns a double value with a positive sign,
    greater than or equal to 0.0 and less than 1.0 */
    
    double randomValue = StrictMath.random();    
    System.out.println("1st randomly generated number = "
    + randomValue);

    System.out.println("2nd randomly generated number = "
    + StrictMath.random());

    System.out.println("3rd randomly generated number = "
    + StrictMath.random());
  }
}

输出结果为:

1st randomly generated number = 0.9920868657769093
2nd randomly generated number = 0.15952532993970858
3rd randomly generated number = 0.26097639244001813

 

热门文章

优秀文章