码迷,mamicode.com
首页 > 其他好文 > 详细

Math类和Random类

时间:2018-10-02 14:19:56      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:exti   内容   oid   能力   运算   生成随机数   静态方法   返回值   pac   

Math类

java.lang.Math提供了一系列静态方法用于科学计算, 其方法的参数和返回值类型一般都为double型, 如果需要更加强大的数学运算能力计算高等数学中的相关内容, 可使用apache commons下面的Math类库

Math类的常用方法:

abs取绝对值
acos, asin, atan, cos, sin, tan三角函数
sqrt平方根
pow(double a, double b), a^b
max(double a, double b), 取最大值
min(double a, double b), 取最小值
ceil(double a), 大于a的最小整数
floor(double a), 小于a的最大整数
random(), 返回0.0到1.0的随机数
long round(double a), double型数据a转换为long型(四舍五入)
toDegrees(double angrad), 弧度转换为角度
roRadians(double angdeg), 角度转换为弧度

/**************示例程序****************/
public static void main(String[] args) {
    // 取正相关操作
    System.out.println(Math.ceil(3.1));
    System.out.println(Math.floor(3.4));
    System.out.println(Math.round(3.1));
    System.out.println(Math.round(3.8));
    System.out.println("##########################");
    
    // 绝对值, 开方, a的b次幂相关操作
    System.out.println(Math.abs(-1));
    System.out.println(Math.abs(-1.1));
    System.out.println(Math.sqrt(36));
    System.out.println(Math.pow(2, 4));
    System.out.println("##########################");
    
    // Math类中常用的常量
    System.out.println(Math.PI);
    System.out.println(Math.E);
    System.out.println("##########################");
    
    // 随机数
    System.out.println(Math.random());
}

/*
4.0
3.0
3
4
##########################
1
1.1
6.0
16.0
##########################
3.141592653589793
2.718281828459045
##########################
0.02732034556476759
*/

Random类

Math类中虽然有产生随机数的方法Math.random(), 但是通常需要的随机数的范围并不是[0,1)之间的double类型数据, 这时就需要对其进行一些复杂的运算. 如果使用Math.random()计算过于复杂的话, 可以使用另一种方式得到随机数, 即Random类, 这个类是专门用来生成随机数, 并且Math.random()底层就是调用的Random类的nextDouble()方法

/******************示例程序*************************/
import java.util.Random;
public static void main(String[] args) {
    Random rand = new Random();
    
    // 随机生成[0,1)之间的double类型的数据
    System.out.println(rand.nextDouble());
    System.out.println("#########################");
    
    // 随机生成int类型允许范围之内的整型数据
    System.out.println(rand.nextInt());
    System.out.println("#########################");
    
    // 随机生成[0,1)之间的float类型数据
    System.out.println(rand.nextFloat());
    System.out.println("#########################");
    
    // 随机生成false或true
    System.out.println(rand.nextBoolean());
    System.out.println("#########################");
    
    // 随机生成[0,10)之间的int类型的数据
    System.out.println(rand.nextInt(10));
    System.out.println("#########################");
    
    // 随机生成[20,30)之间的int类型的数据
    System.out.println(rand.nextInt(10) + 20);
    System.out.println("#########################");
    
    // 随机生成[20,30)之间的int类型的数据(此种方法计算较为复杂)
    System.out.println((int)(rand.nextDouble() * 10) + 20);
    System.out.println("#########################");
}

/*
0.18579466820637747
#########################
1695590674
#########################
0.8908015
#########################
false
#########################
9
#########################
21
#########################
25
#########################
*/

Math类和Random类

标签:exti   内容   oid   能力   运算   生成随机数   静态方法   返回值   pac   

原文地址:https://www.cnblogs.com/hesper/p/9736337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!