栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

实用类介绍之 Random 类和 Math 类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实用类介绍之 Random 类和 Math 类

java.util 包提供了很多实用的类和接口,本篇将介绍其中的两个工具类——Random 类和 Math 类

一、Random 类

java.util.Random类是一个专门产生随机数的类

1. 利用 Random 类无参构造产生随机数的步骤:

  1. 创建一个 Random 类的对象
  2. 调用 nextInt() 方法产生一个随机整数(有正有负)
    调用 nextInt(整数n) 方法产生一个大于等于0,小于n的随机整数
    调用 nextDouble() 方法产生一个0~1之间的 Double 类型值
    调用 nextFloat() 方法产生一个0~1之间的 Float 类型值
public class TestRandom {
	public static void main(String[] args) {		
		//0-9的随机数  使用Random类
		Random r = new Random();
		System.out.println(r.nextInt(10));

        //0~1之间的小数
        System.out.println(r.nextDouble());
        System.out.println(r.nextFloat());
		
		//1000-9999
		System.out.println(r.nextInt(9000)+1000);
	}
}

2. Random 类还有一个有参构造:public Random(long seed){……}

利用 Random 类有参构造创建的对象所产生的一组随机数和参数相关,和对象实例以及程序运行次数无关,即便是不同的对象,只要参数相同,不管刷新多少次,调用同一方法得到的结果都相同

public class TestRandom {
	public static void main(String[] args) {
        System.out.println("无参构造产生的随机数作对比:"+new Random().nextInt(10));
		//带种子的
        System.out.println("参数为100:");
		Random r2 = new Random(100);
		for(int i=0;i<8;i++) {
			System.out.print(r2.nextInt(10)+"t");
		}
		
        System.out.println("n参数为1000的不同对象:");
		Random r3 = new Random(1000);
		Random r4 = new Random(1000);
		for(int i=0;i<8;i++) {
			System.out.print(r3.nextInt(10)+"--");
			System.out.print(r4.nextInt(10)+"t");
		}
	}
}

运行一次:

运行第二次:

二、Math 类

java.lang.Math类提供了常用的数学运算方法和两个静态常量E(自然对数的底数)和PI(圆周率)

静态常量可以直接用 类名.常量名 的方式调用:

public class Test {
	public static void main(String[] args) {
		System.out.println("自然对数的底数(E):"+Math.E);
		System.out.println("圆周率π(PI):"+Math.PI);
    }
}

静态方法也可以直接用 类名.方法名() 的方式调用:

1. 求绝对值:abs()

public class Test {
	public static void main(String[] args) {
		System.out.println("Math.abs(13)="+Math.abs(13));
		System.out.println("Math.abs(-14.0)="+Math.abs(-14.0));
    }
}

2. 抹零取整:round()

public class Test {
	public static void main(String[] args) {
		System.out.println("Math.round(12.78)="+Math.round(12.34));
		System.out.println("Math.round(-12.34)="+Math.round(-12.34));
    }
}

3. 向上取整:ceil()

public class Test {
	public static void main(String[] args) {
		System.out.println("Math.ceil(16.4)="+Math.ceil(16.4));
		System.out.println("Math.ceil(-16.4)="+Math.ceil(-16.4));
    }
}

4. 向下取整:floor()

public class Test {
	public static void main(String[] args) {
		System.out.println("Math.floor(16.5)="+Math.floor(16.5));
		System.out.println("Math.floor(-16.5)="+Math.floor(-16.5));
    }
}

5. 求 a,b 之间的最大值 / 最小值:max(a,b) / min(a,b)

public class Test {
	public static void main(String[] args) {
		System.out.println("Math.max(236, -928)="+Math.max(236, -928));
		System.out.println("Math.min(236, -928)="+Math.min(236, -928));
    }
}

6. a 的 b 次方:pow(a,b)  和 开平方:sqrt()

public class Test {
	public static void main(String[] args) {
		System.out.println("Math.pow(2, 3)="+Math.pow(2, 3));
		System.out.println("Math.sqrt(9)="+Math.sqrt(9));
    }
}

7. 生成大于等于 0 小于 1 的随机小数:random()
    生成大于 a 小于 b 的随机整数的公式:(int)((Math.random()*(b-a+1))+a)

public class Test {
	public static void main(String[] args) {
		System.out.println("Math.random()="+Math.random());
		System.out.println("生成0-9的随机整数:"+(int)(Math.random()*10));
		System.out.println("生成三位的随机整数:"+(int)((Math.random()*900)+100));
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/999890.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号