RandomTest03

package com.xdf;

import java.util.Random;

public class RandomTest03 {

/**
* Math 算数类
* math.random();
*
* Random 随机数的类
* random.nextDouble();
*
* between {@code 0.0} and {@code 1.0}
*
* 返回0.0到1.0之前的数字(浮点数)! 不包含1.0
*/
public static void main(String[] args) {
/**
实例化一个算术类对象
Math math = new Math();
01.编译报错
02.观看底层代码得知
private Math() {}
03.构造函数是私有的 ,其他类无法访问
04.因为Math类中所有的方法都是静态方法—— 由static关键字修饰的方法(静态方法)
05.静态方法的特点是通过类名可以直接访问
*/
double num = Math.random();
System.out.println("Math.random()随机数===》" + num);
/**
* 产生一个0-10之前的随机数
* 01. Math.random() 返回的是0-1之间的小数
* 02. Math.random()*10之后就变成了 0-10之间的小数
* 03. 把02的结果转换成int类型
*/
int result = (int) (Math.random() * 10);
System.out.println(" 0-10之间的随机数===》" + result);

System.out.println("********************************");
Random r = new Random();
num = r.nextDouble();
System.out.println("random.nextDouble()产生的随机数====》" + num);
}
}

原文地址:https://www.cnblogs.com/xiaoxiao1016/p/7856775.html