Math.random()獲取的是0~1之間的double型隨機數,不包括1
如果想獲取0~9的隨機整數(int)(Math.random()*10)
如果想獲取1~10的隨機整數(int)(Math.random()*10 + 1)
/**
* @param args
* 使用Random類中的方法
*/
public static void main(String[] args)
{
Random r=new Random(); //實例化一個Random類
System.out.println("隨機產生一個整數:"+r.nextInt()); //隨機產生一個整數
System.out.println("隨機產生一個大於等於0小於10的整數:"+r.nextInt(10)); //隨機產生一個大於等於0小於10的整數
System.out.println("隨機產生一個布爾型的值:"+r.nextBoolean()); //隨機產生一個布爾型的值
System.out.println("隨機產生一個雙精度型的值:"+r.nextDouble()); //隨機產生一個雙精度型的值
System.out.println("隨機產生一個浮點型的值:"+r.nextFloat()); //隨機產生一個浮點型的值
System.out.println("隨機產生一個概率密度為高斯分布的雙精度值:"+r.nextGaussian()); //隨機產生一個概率密度為高斯分布的雙精度值
}