实用类介绍之 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));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值