util包的常用类及其方法(上)

util包为JDK内置的一个包,与lang包不同的是,使用util包的类时需要引用:

import java.util.*

1.Random类

Random类用来生成随机数,构造器有:

Random(),无参数的构造器

Random(int seed),有参数的构造器

常用方法有:

int nextInt()返回下一个整型值

long nextLong()返回下一个长整型值

double nextDouble()返回下一个0到1.0的双精度浮点值

float nextFloat()返回下一个0到1.0的单精度浮点值

double nextGaussian()返回下一个高斯分布的双精度浮点值,中间值为0,标准差为1.0

eg:

//创建一个Random类对象

Random random=new Random();

//生成20个随机整数,并输出

for(int i=0;i<20;i++){

int num=random.nextInt();

System.out.println("第"+(i+1)+"个随机数是:"+num);

}

原文地址:https://www.cnblogs.com/hitnmg/p/9209184.html