Java8 SE API - java.lang.Math 和 java.util.Random

常规方法(2个常量, 35个方法+9个省略的重载方法)

final double E = 2.7182818284590452354;
final double PI = 3.14159265358979323846;

int abs(int) //还有float, double, long的重载形式
int max(int, int) //以及float, double, long的重载
int min(int, int) //以及float, double, long的重载

//三角函数, 反三角函数, 双曲函数, 弧度表示
double cos(double)
double cosh(double)
double acos(double) //arccos, [0, π]
double sin(double)
double sinh(double)
double asin(double) //arcsin, [-π/2, π/2]
double tan(double)
double tanh(double)
double atan(double) //arctan, (-π/2, π/2)

double toDegress(double) //转换为角度, 即x * 180.0 / π
double toRadians(double) //转换为弧度, 即x / 180.0 * π

/*计算向量(x,y)和x轴的夹角, 但是为什么参数顺序是y, x? 为什么又叫atan2这个诡异的名字?*/
double atan2(double y, double x)

double pow(double a, double b) //=>a^b
double sqrt(double) //平方根
double cbrt(double) //cube root, 立方根


//取整函数
//返回刚好大于a的整数, 但是返回值类型为什么是double?例: Math.ceil(1.23) = 2.0
double ceil(double a) 
double floor(double) //返回刚好小于a的整数
double rint(double) //返回最接近的整数, 如果小数为0.5, 返回值为偶数的那个
double round(double) //返回最接近的整数, 四舍五入,算法为 Math.floor(x+0.5)
					 //即将原来的数字加上 0.5 后再向下取整
					 //所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11
float round(float) //为什么就它有重载

//根据sign的正负返回|a|或-|a|, 但是这个方法有什么用呢? 而且只有浮点型
double copySign(double a, double sign)
double copySign(float a, float sign)

//指数函数
double exp(double x)
/*Returns e^x -1. Note that for values of x near 0, the exact sum of expm1(x) + 1 is 
much closer to the true result of e^x than exp(x). */
double expm1(double x)

//对数函数
double log(double) //=>ln(x)
double log10(double) //=>log10(x)
double log1p(double) //=>ln(x + 1), 和上面的expm1并非对应关系 

double hypot(double x, double y) //=>sqrt(x²+y²), 很有用的方法!

//=>f1 - f2 * n, n是f1/f2的"整数商"
//或者说, Returns the remainder resulting from f1 by f2.
double IEEEremainder(double f1, double f2) 

double signum(double) //正数返回1.0, 负数返回-1.0
float signum(float)

double random() //返回(0, 1)之间的一个随机数


比较Math.random()和Random.nextInt()的区别

Math.random() 为 java.lang.Math 类中的静态方法;

random.nextInt() 为 java.util.Random类中的方法;

  1. Math.random()方法的本质,其实还是new的Random()方法,并且它的返回值类型是Double(),即
    Math.random() 等同于 random类的nextDouble()

  2. Math.random() 方法生成[0, 1)范围内的double类型随机数
    Random.nextInt(n)是生成[0, n)范围内的Int类型随机数

需要Double类型的随机数,用Math.random();
如果我们需要其余类型的随机数,用Random.nextInt()这种形式(此外还有.nextBoolean()、.nextBytes()、.nextFloat()、.nextLong()等各种基础数据类型)。

Random类

Random(long )中引入参数,其随机数产生是有规律的,是伪随机数;

如果要生成真正的随机数,Random()需要空参,借用当前时间随机,是真正的随机数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值