Math类和Random类

Math类

成员变量:编程

     (1)public static final double PI :圆周率dom

     (2) public static final double E :天然对数的底数code

成员方法:对象

     (1)public static int abs(int x):绝对值get

            注意:abs()方法的参数能够是int,float,double,long类型的数据,返回值相应类型的数据变量

     (2)public static double ceil(double x):向上取整,返回double类型随机数

     (3)public static double floor(double x):向下取整,返回double类型float

     (4)public static int max(int a,int b):最大值方法

            注意:max()方法的参数能够是int,float,double,long类型的数据,返回值相应类型的数据next

     (5)public static int min(int a,int b):最小值

            注意:min()方法的参数能够是int,float,double,long类型的数据,返回值相应类型的数据

     (6)public static double pow(double a,double b):a的b次幂

     (7)public static int round(float x):四舍五入

            注意:round()方法的参数能够是float,double类型的数据

     (8)public static double sqrt(double x):正平方根

     (9)public static double random():随机数,大于等于 0.0 且小于 1.0 的随机 double 值(0.0,1.0)

            获取随机数:0-1

                             int number = (int)(Math.random())

            获取随机数:1-100

                             int number = (int)(Math.random()*100) + 1

            获取随机数:start到end之间

                             public static int getRandom(int start,int end){

                               return (int)(Math.random()*(end-start+1))+start;

Random类

random类:用于产生随机数

注意:通常编程时,习惯使用Math类的random方法来生成随机数字

构造方法:

   (1)public Random():使用默认的种子,默认种子是当前时间的毫秒值

   (2)public Random(long seed):使用给定的种子。若是用相同的种子建立两个 Random 实例,则对每一个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。

成员方法:

       Random类中各方法生成的随机数字都是均匀分布的,也就是说区间内部的数字生成的概率是均等的。

     (1)public int nextInt():该方法的做用是生成一个随机的int值,该值介于int的区间,也就是-231到231-1之间。

     (2)public int nextInt(int n):该方法的做用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含0而不包含n。

     (3)public boolean nextBoolean():该方法的做用是生成一个随机的boolean值,生成true和false的值概率相等,也就是都是50%的概率。

     (4)public double nextDouble():该方法的做用是生成一个随机的double值,数值介于[0,1.0)之间。

     (5)public void setSeed(long seed):该方法的做用是从新设置Random对象中的种子数。设置完种子数之后的Random对象和相同种子数使用new关键字建立出的Random对象相同。

相关文章
相关标签/搜索